fix pairing of quotes in cstyle behavior

This commit is contained in:
nightwing 2014-11-21 00:58:25 +04:00
commit 852e0673ca

View file

@ -260,48 +260,38 @@ var CstyleBehaviour = function() {
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row); var line = session.doc.getLine(cursor.row);
var leftChar = line.substring(cursor.column-1, cursor.column); var leftChar = line.substring(cursor.column-1, cursor.column);
var rightChar = line.substring(cursor.column, cursor.column + 1);
var token = session.getTokenAt(cursor.row, cursor.column);
var rightToken = session.getTokenAt(cursor.row, cursor.column + 1);
// We're escaped. // We're escaped.
if (leftChar == '\\') { if (leftChar == "\\" && token && /escape/.test(token.type))
return null; return null;
}
// Find what token we're inside. var stringBefore = token && /string/.test(token.type);
var tokens = session.getTokens(selection.start.row); var stringAfter = !rightToken || /string/.test(rightToken.type);
var col = 0, token;
var quotepos = -1; // Track whether we're inside an open quote.
for (var x = 0; x < tokens.length; x++) { var pair;
token = tokens[x]; if (rightChar == quote) {
if (token.type == "string") { pair = stringBefore !== stringAfter;
quotepos = -1; } else {
} else if (quotepos < 0) { if (stringBefore && !stringAfter)
quotepos = token.value.indexOf(quote); return null; // wrap string with different quote
} var wordRe = session.$mode.tokenRe;
if ((token.value.length + col) > selection.start.column) { wordRe.lastIndex = 0;
break; var isWordBefore = wordRe.test(leftChar);
} wordRe.lastIndex = 0;
col += tokens[x].value.length; var isWordAfter = wordRe.test(leftChar);
} if (isWordBefore || isWordAfter)
return null; // before or after alphanumeric
// Try and be smart about when we auto insert. if (rightChar && !/[\s;,.})\]\\]/.test(rightChar))
if (!token || (quotepos < 0 && token.type !== "comment" && (token.type !== "string" || ((selection.start.column !== token.value.length+col-1) && token.value.lastIndexOf(quote) === token.value.length-1)))) { return null; // there is rightChar and it isn't closing
if (!CstyleBehaviour.isSaneInsertion(editor, session)) pair = true;
return;
return {
text: quote + quote,
selection: [1,1]
};
} else if (token && token.type === "string") {
// Ignore input and move right one if we're typing over the closing quote.
var rightChar = line.substring(cursor.column, cursor.column + 1);
if (rightChar == quote) {
return {
text: '',
selection: [1, 1]
};
}
} }
return {
text: pair ? quote + quote : "",
selection: [1,1]
};
} }
} }
}); });