Insert a closing bracket for { inside other brackets, e.g. ({})
This commit is contained in:
parent
99e45e0127
commit
05a3dce9d4
2 changed files with 29 additions and 20 deletions
2
build
2
build
|
|
@ -1 +1 @@
|
|||
Subproject commit 6149ca6b148e878d4c1341d4675ca3597d78dbdd
|
||||
Subproject commit 86abb02339499ac96f95649ad5e088e66b6b9a9c
|
||||
|
|
@ -34,19 +34,21 @@ define(function(require, exports, module) {
|
|||
var oop = require("../../lib/oop");
|
||||
var Behaviour = require("../behaviour").Behaviour;
|
||||
var TokenIterator = require("../../token_iterator").TokenIterator;
|
||||
var lang = require("ace/lib/lang");
|
||||
|
||||
var SAFE_INSERT_IN_TOKENS =
|
||||
["text", "paren.rparen", "punctuation.operator"];
|
||||
var SAFE_INSERT_BEFORE_TOKENS =
|
||||
["text", "paren.rparen", "punctuation.operator", "comment"];
|
||||
|
||||
|
||||
var autoInsertedBrackets = 0;
|
||||
var autoInsertedRow = -1;
|
||||
var autoInsertedLineEnd = "";
|
||||
var maybeInsertedBrackets = 0;
|
||||
var maybeInsertedRow = -1;
|
||||
var maybeInsertedLineStart = "";
|
||||
var maybeInsertedLineEnd = "";
|
||||
var maybeInserted = "";
|
||||
|
||||
var CstyleBehaviour = function () {
|
||||
|
||||
|
|
@ -86,12 +88,12 @@ var CstyleBehaviour = function () {
|
|||
CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
|
||||
var cursor = editor.getCursorPosition();
|
||||
var line = session.doc.getLine(cursor.row);
|
||||
if (!this.isMaybeInsertedClosing(cursor, line, maybeInsertedLineEnd[0]))
|
||||
if (!this.isMaybeInsertedClosing(cursor, line))
|
||||
maybeInsertedBrackets = 0;
|
||||
maybeInsertedRow = cursor.row;
|
||||
maybeInsertedLineStart = line.substr(0, cursor.column) + bracket;
|
||||
maybeInsertedLineEnd = line.substr(cursor.column);
|
||||
maybeInsertedBrackets++;
|
||||
maybeInserted = bracket + maybeInserted;
|
||||
};
|
||||
|
||||
CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
|
||||
|
|
@ -104,7 +106,8 @@ var CstyleBehaviour = function () {
|
|||
CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
|
||||
return maybeInsertedBrackets > 0 &&
|
||||
cursor.row === maybeInsertedRow &&
|
||||
line.substr(cursor.column) === maybeInsertedLineEnd;
|
||||
line.substr(cursor.column) === maybeInsertedLineEnd &&
|
||||
line.substr(0, cursor.column) == maybeInsertedLineStart;
|
||||
};
|
||||
|
||||
CstyleBehaviour.popAutoInsertedClosing = function() {
|
||||
|
|
@ -114,10 +117,12 @@ var CstyleBehaviour = function () {
|
|||
|
||||
CstyleBehaviour.clearMaybeInsertedClosing = function() {
|
||||
maybeInsertedBrackets = 0;
|
||||
maybeInserted = "";
|
||||
maybeInsertedRow = -1;
|
||||
};
|
||||
|
||||
this.add("braces", "insertion", function (state, action, editor, session, text) {
|
||||
var cursor = editor.getCursorPosition();
|
||||
var line = session.doc.getLine(cursor.row);
|
||||
if (text == '{') {
|
||||
var selection = editor.getSelectionRange();
|
||||
var selected = session.doc.getTextRange(selection);
|
||||
|
|
@ -127,15 +132,20 @@ var CstyleBehaviour = function () {
|
|||
selection: false
|
||||
};
|
||||
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||
CstyleBehaviour.recordMaybeInsert(editor, session, "}");
|
||||
return {
|
||||
text: '{',
|
||||
selection: [1, 1]
|
||||
};
|
||||
if (/[\]\}\)]/.test(line[cursor.column])) {
|
||||
return {
|
||||
text: '{}',
|
||||
selection: [1, 1]
|
||||
};
|
||||
} else {
|
||||
CstyleBehaviour.recordMaybeInsert(editor, session, "{");
|
||||
return {
|
||||
text: '{',
|
||||
selection: [1, 1]
|
||||
};
|
||||
}
|
||||
}
|
||||
} else if (text == '}') {
|
||||
var cursor = editor.getCursorPosition();
|
||||
var line = session.doc.getLine(cursor.row);
|
||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||
if (rightChar == '}') {
|
||||
var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
|
||||
|
|
@ -148,22 +158,19 @@ var CstyleBehaviour = function () {
|
|||
}
|
||||
}
|
||||
} else if (text == "\n" || text == "\r\n") {
|
||||
var cursor = editor.getCursorPosition();
|
||||
var line = session.doc.getLine(cursor.row);
|
||||
var closing = "";
|
||||
if (CstyleBehaviour.isMaybeInsertedClosing(cursor, line)) {
|
||||
console.log("doit");
|
||||
closing = maybeInserted;
|
||||
closing = lang.stringRepeat("}", maybeInsertedBrackets);
|
||||
CstyleBehaviour.clearMaybeInsertedClosing();
|
||||
}
|
||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||
if (rightChar == '}' || closing !== "") {
|
||||
var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column + 1}, '}');
|
||||
var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column}, '}');
|
||||
if (!openBracePos)
|
||||
return null;
|
||||
|
||||
var indent = this.getNextLineIndent(state, line.substring(0, line.length - 1), session.getTabString());
|
||||
var next_indent = this.$getIndent(session.doc.getLine(openBracePos.row));
|
||||
var indent = this.getNextLineIndent(state, line.substring(0, cursor.column), session.getTabString());
|
||||
var next_indent = this.$getIndent(line);
|
||||
|
||||
return {
|
||||
text: '\n' + indent + '\n' + next_indent + closing,
|
||||
|
|
@ -181,6 +188,8 @@ var CstyleBehaviour = function () {
|
|||
if (rightChar == '}') {
|
||||
range.end.column++;
|
||||
return range;
|
||||
} else {
|
||||
maybeInsertedBrackets--;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue