Don't insert a closing bracket for { until a newline is added

This commit is contained in:
Lennart Kats 2012-11-11 11:24:41 +01:00 committed by nightwing
commit 99e45e0127
2 changed files with 39 additions and 7 deletions

View file

@ -37,10 +37,10 @@ var Range = require("../range").Range;
function BracketMatch() {
this.findMatchingBracket = function(position) {
this.findMatchingBracket = function(position, char) {
if (position.column == 0) return null;
var charBeforeCursor = this.getLine(position.row).charAt(position.column-1);
var charBeforeCursor = char || this.getLine(position.row).charAt(position.column-1);
if (charBeforeCursor == "") return null;
var match = charBeforeCursor.match(/([\(\[\{])|([\)\]\}])/);

View file

@ -43,6 +43,10 @@ var SAFE_INSERT_BEFORE_TOKENS =
var autoInsertedBrackets = 0;
var autoInsertedRow = -1;
var autoInsertedLineEnd = "";
var maybeInsertedBrackets = 0;
var maybeInsertedRow = -1;
var maybeInsertedLineEnd = "";
var maybeInserted = "";
var CstyleBehaviour = function () {
@ -79,6 +83,17 @@ var CstyleBehaviour = function () {
autoInsertedBrackets++;
};
CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row);
if (!this.isMaybeInsertedClosing(cursor, line, maybeInsertedLineEnd[0]))
maybeInsertedBrackets = 0;
maybeInsertedRow = cursor.row;
maybeInsertedLineEnd = line.substr(cursor.column);
maybeInsertedBrackets++;
maybeInserted = bracket + maybeInserted;
};
CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
return autoInsertedBrackets > 0 &&
cursor.row === autoInsertedRow &&
@ -86,10 +101,21 @@ var CstyleBehaviour = function () {
line.substr(cursor.column) === autoInsertedLineEnd;
};
CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
return maybeInsertedBrackets > 0 &&
cursor.row === maybeInsertedRow &&
line.substr(cursor.column) === maybeInsertedLineEnd;
};
CstyleBehaviour.popAutoInsertedClosing = function() {
autoInsertedLineEnd = autoInsertedLineEnd.substr(1);
autoInsertedBrackets--;
};
CstyleBehaviour.clearMaybeInsertedClosing = function() {
maybeInsertedBrackets = 0;
maybeInserted = "";
};
this.add("braces", "insertion", function (state, action, editor, session, text) {
if (text == '{') {
@ -101,9 +127,9 @@ var CstyleBehaviour = function () {
selection: false
};
} else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
CstyleBehaviour.recordAutoInsert(editor, session, "}");
CstyleBehaviour.recordMaybeInsert(editor, session, "}");
return {
text: '{}',
text: '{',
selection: [1, 1]
};
}
@ -124,9 +150,15 @@ 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;
CstyleBehaviour.clearMaybeInsertedClosing();
}
var rightChar = line.substring(cursor.column, cursor.column + 1);
if (rightChar == '}') {
var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column + 1});
if (rightChar == '}' || closing !== "") {
var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column + 1}, '}');
if (!openBracePos)
return null;
@ -134,7 +166,7 @@ var CstyleBehaviour = function () {
var next_indent = this.$getIndent(session.doc.getLine(openBracePos.row));
return {
text: '\n' + indent + '\n' + next_indent,
text: '\n' + indent + '\n' + next_indent + closing,
selection: [1, indent.length, 1, indent.length]
};
}