CstyleBehaviour: keeping selections after insertion
This commit is contained in:
parent
60c639a34b
commit
a5c619bda8
1 changed files with 17 additions and 16 deletions
|
|
@ -63,6 +63,19 @@ var initContext = function(editor) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var getWrapped = function(selection, selected, opening, closing) {
|
||||||
|
var rowDiff = selection.end.row - selection.start.row;
|
||||||
|
return {
|
||||||
|
text: opening + selected + closing,
|
||||||
|
selection: [
|
||||||
|
0,
|
||||||
|
selection.start.column + 1,
|
||||||
|
rowDiff,
|
||||||
|
selection.end.column + (rowDiff ? 0 : 1)
|
||||||
|
]
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
var CstyleBehaviour = function() {
|
var CstyleBehaviour = function() {
|
||||||
this.add("braces", "insertion", function(state, action, editor, session, text) {
|
this.add("braces", "insertion", function(state, action, editor, session, text) {
|
||||||
var cursor = editor.getCursorPosition();
|
var cursor = editor.getCursorPosition();
|
||||||
|
|
@ -72,10 +85,7 @@ var CstyleBehaviour = function() {
|
||||||
var selection = editor.getSelectionRange();
|
var selection = editor.getSelectionRange();
|
||||||
var selected = session.doc.getTextRange(selection);
|
var selected = session.doc.getTextRange(selection);
|
||||||
if (selected !== "" && selected !== "{" && editor.getWrapBehavioursEnabled()) {
|
if (selected !== "" && selected !== "{" && editor.getWrapBehavioursEnabled()) {
|
||||||
return {
|
return getWrapped(selection, selected, '{', '}');
|
||||||
text: '{' + selected + '}',
|
|
||||||
selection: false
|
|
||||||
};
|
|
||||||
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||||
if (/[\]\}\)]/.test(line[cursor.column]) || editor.inMultiSelectMode) {
|
if (/[\]\}\)]/.test(line[cursor.column]) || editor.inMultiSelectMode) {
|
||||||
CstyleBehaviour.recordAutoInsert(editor, session, "}");
|
CstyleBehaviour.recordAutoInsert(editor, session, "}");
|
||||||
|
|
@ -155,10 +165,7 @@ var CstyleBehaviour = function() {
|
||||||
var selection = editor.getSelectionRange();
|
var selection = editor.getSelectionRange();
|
||||||
var selected = session.doc.getTextRange(selection);
|
var selected = session.doc.getTextRange(selection);
|
||||||
if (selected !== "" && editor.getWrapBehavioursEnabled()) {
|
if (selected !== "" && editor.getWrapBehavioursEnabled()) {
|
||||||
return {
|
return getWrapped(selection, selected, '(', ')');
|
||||||
text: '(' + selected + ')',
|
|
||||||
selection: false
|
|
||||||
};
|
|
||||||
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||||
CstyleBehaviour.recordAutoInsert(editor, session, ")");
|
CstyleBehaviour.recordAutoInsert(editor, session, ")");
|
||||||
return {
|
return {
|
||||||
|
|
@ -203,10 +210,7 @@ var CstyleBehaviour = function() {
|
||||||
var selection = editor.getSelectionRange();
|
var selection = editor.getSelectionRange();
|
||||||
var selected = session.doc.getTextRange(selection);
|
var selected = session.doc.getTextRange(selection);
|
||||||
if (selected !== "" && editor.getWrapBehavioursEnabled()) {
|
if (selected !== "" && editor.getWrapBehavioursEnabled()) {
|
||||||
return {
|
return getWrapped(selection, selected, '[', ']');
|
||||||
text: '[' + selected + ']',
|
|
||||||
selection: false
|
|
||||||
};
|
|
||||||
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||||
CstyleBehaviour.recordAutoInsert(editor, session, "]");
|
CstyleBehaviour.recordAutoInsert(editor, session, "]");
|
||||||
return {
|
return {
|
||||||
|
|
@ -252,10 +256,7 @@ var CstyleBehaviour = function() {
|
||||||
var selection = editor.getSelectionRange();
|
var selection = editor.getSelectionRange();
|
||||||
var selected = session.doc.getTextRange(selection);
|
var selected = session.doc.getTextRange(selection);
|
||||||
if (selected !== "" && selected !== "'" && selected != '"' && editor.getWrapBehavioursEnabled()) {
|
if (selected !== "" && selected !== "'" && selected != '"' && editor.getWrapBehavioursEnabled()) {
|
||||||
return {
|
return getWrapped(selection, selected, quote, quote);
|
||||||
text: quote + selected + quote,
|
|
||||||
selection: false
|
|
||||||
};
|
|
||||||
} else if (!selected) {
|
} else if (!selected) {
|
||||||
var cursor = editor.getCursorPosition();
|
var cursor = editor.getCursorPosition();
|
||||||
var line = session.doc.getLine(cursor.row);
|
var line = session.doc.getLine(cursor.row);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue