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 oop = require("../../lib/oop");
|
||||||
var Behaviour = require("../behaviour").Behaviour;
|
var Behaviour = require("../behaviour").Behaviour;
|
||||||
var TokenIterator = require("../../token_iterator").TokenIterator;
|
var TokenIterator = require("../../token_iterator").TokenIterator;
|
||||||
|
var lang = require("ace/lib/lang");
|
||||||
|
|
||||||
var SAFE_INSERT_IN_TOKENS =
|
var SAFE_INSERT_IN_TOKENS =
|
||||||
["text", "paren.rparen", "punctuation.operator"];
|
["text", "paren.rparen", "punctuation.operator"];
|
||||||
var SAFE_INSERT_BEFORE_TOKENS =
|
var SAFE_INSERT_BEFORE_TOKENS =
|
||||||
["text", "paren.rparen", "punctuation.operator", "comment"];
|
["text", "paren.rparen", "punctuation.operator", "comment"];
|
||||||
|
|
||||||
|
|
||||||
var autoInsertedBrackets = 0;
|
var autoInsertedBrackets = 0;
|
||||||
var autoInsertedRow = -1;
|
var autoInsertedRow = -1;
|
||||||
var autoInsertedLineEnd = "";
|
var autoInsertedLineEnd = "";
|
||||||
var maybeInsertedBrackets = 0;
|
var maybeInsertedBrackets = 0;
|
||||||
var maybeInsertedRow = -1;
|
var maybeInsertedRow = -1;
|
||||||
|
var maybeInsertedLineStart = "";
|
||||||
var maybeInsertedLineEnd = "";
|
var maybeInsertedLineEnd = "";
|
||||||
var maybeInserted = "";
|
|
||||||
|
|
||||||
var CstyleBehaviour = function () {
|
var CstyleBehaviour = function () {
|
||||||
|
|
||||||
|
|
@ -86,12 +88,12 @@ var CstyleBehaviour = function () {
|
||||||
CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
|
CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
|
||||||
var cursor = editor.getCursorPosition();
|
var cursor = editor.getCursorPosition();
|
||||||
var line = session.doc.getLine(cursor.row);
|
var line = session.doc.getLine(cursor.row);
|
||||||
if (!this.isMaybeInsertedClosing(cursor, line, maybeInsertedLineEnd[0]))
|
if (!this.isMaybeInsertedClosing(cursor, line))
|
||||||
maybeInsertedBrackets = 0;
|
maybeInsertedBrackets = 0;
|
||||||
maybeInsertedRow = cursor.row;
|
maybeInsertedRow = cursor.row;
|
||||||
|
maybeInsertedLineStart = line.substr(0, cursor.column) + bracket;
|
||||||
maybeInsertedLineEnd = line.substr(cursor.column);
|
maybeInsertedLineEnd = line.substr(cursor.column);
|
||||||
maybeInsertedBrackets++;
|
maybeInsertedBrackets++;
|
||||||
maybeInserted = bracket + maybeInserted;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
|
CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
|
||||||
|
|
@ -104,7 +106,8 @@ var CstyleBehaviour = function () {
|
||||||
CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
|
CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
|
||||||
return maybeInsertedBrackets > 0 &&
|
return maybeInsertedBrackets > 0 &&
|
||||||
cursor.row === maybeInsertedRow &&
|
cursor.row === maybeInsertedRow &&
|
||||||
line.substr(cursor.column) === maybeInsertedLineEnd;
|
line.substr(cursor.column) === maybeInsertedLineEnd &&
|
||||||
|
line.substr(0, cursor.column) == maybeInsertedLineStart;
|
||||||
};
|
};
|
||||||
|
|
||||||
CstyleBehaviour.popAutoInsertedClosing = function() {
|
CstyleBehaviour.popAutoInsertedClosing = function() {
|
||||||
|
|
@ -114,10 +117,12 @@ var CstyleBehaviour = function () {
|
||||||
|
|
||||||
CstyleBehaviour.clearMaybeInsertedClosing = function() {
|
CstyleBehaviour.clearMaybeInsertedClosing = function() {
|
||||||
maybeInsertedBrackets = 0;
|
maybeInsertedBrackets = 0;
|
||||||
maybeInserted = "";
|
maybeInsertedRow = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
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 line = session.doc.getLine(cursor.row);
|
||||||
if (text == '{') {
|
if (text == '{') {
|
||||||
var selection = editor.getSelectionRange();
|
var selection = editor.getSelectionRange();
|
||||||
var selected = session.doc.getTextRange(selection);
|
var selected = session.doc.getTextRange(selection);
|
||||||
|
|
@ -127,15 +132,20 @@ var CstyleBehaviour = function () {
|
||||||
selection: false
|
selection: false
|
||||||
};
|
};
|
||||||
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
|
||||||
CstyleBehaviour.recordMaybeInsert(editor, session, "}");
|
if (/[\]\}\)]/.test(line[cursor.column])) {
|
||||||
|
return {
|
||||||
|
text: '{}',
|
||||||
|
selection: [1, 1]
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
CstyleBehaviour.recordMaybeInsert(editor, session, "{");
|
||||||
return {
|
return {
|
||||||
text: '{',
|
text: '{',
|
||||||
selection: [1, 1]
|
selection: [1, 1]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if (text == '}') {
|
} else if (text == '}') {
|
||||||
var cursor = editor.getCursorPosition();
|
|
||||||
var line = session.doc.getLine(cursor.row);
|
|
||||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||||
if (rightChar == '}') {
|
if (rightChar == '}') {
|
||||||
var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
|
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") {
|
} else if (text == "\n" || text == "\r\n") {
|
||||||
var cursor = editor.getCursorPosition();
|
|
||||||
var line = session.doc.getLine(cursor.row);
|
|
||||||
var closing = "";
|
var closing = "";
|
||||||
if (CstyleBehaviour.isMaybeInsertedClosing(cursor, line)) {
|
if (CstyleBehaviour.isMaybeInsertedClosing(cursor, line)) {
|
||||||
console.log("doit");
|
closing = lang.stringRepeat("}", maybeInsertedBrackets);
|
||||||
closing = maybeInserted;
|
|
||||||
CstyleBehaviour.clearMaybeInsertedClosing();
|
CstyleBehaviour.clearMaybeInsertedClosing();
|
||||||
}
|
}
|
||||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||||
if (rightChar == '}' || closing !== "") {
|
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)
|
if (!openBracePos)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var indent = this.getNextLineIndent(state, line.substring(0, line.length - 1), session.getTabString());
|
var indent = this.getNextLineIndent(state, line.substring(0, cursor.column), session.getTabString());
|
||||||
var next_indent = this.$getIndent(session.doc.getLine(openBracePos.row));
|
var next_indent = this.$getIndent(line);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
text: '\n' + indent + '\n' + next_indent + closing,
|
text: '\n' + indent + '\n' + next_indent + closing,
|
||||||
|
|
@ -181,6 +188,8 @@ var CstyleBehaviour = function () {
|
||||||
if (rightChar == '}') {
|
if (rightChar == '}') {
|
||||||
range.end.column++;
|
range.end.column++;
|
||||||
return range;
|
return range;
|
||||||
|
} else {
|
||||||
|
maybeInsertedBrackets--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue