Improved Cstyle behaviour

This commit is contained in:
c-spencer 2011-05-22 13:27:36 +01:00
commit deb5a01693

View file

@ -58,16 +58,19 @@ var CstyleBehaviour = function () {
selection: [1, 1] selection: [1, 1]
} }
} }
} else if (text == '}') { // This should do some matching checks } else if (text == '}') {
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row); 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});
if (matching !== null) {
return { return {
text: '', text: '',
selection: [1, 1] selection: [1, 1]
} }
} }
}
} else if (text == "\n") { } else if (text == "\n") {
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row); var line = session.doc.getLine(cursor.row);
@ -113,17 +116,20 @@ var CstyleBehaviour = function () {
selection: [1, 1] selection: [1, 1]
} }
} }
} else if (text == ')') { // This should do some matching checks } else if (text == ')') {
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row); 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});
if (matching !== null) {
return { return {
text: '', text: '',
selection: [1, 1] selection: [1, 1]
} }
} }
} }
}
return false; return false;
}, function (state, editor, session, range) { }, function (state, editor, session, range) {
var selected = session.doc.getTextRange(range); var selected = session.doc.getTextRange(range);
@ -148,10 +154,42 @@ var CstyleBehaviour = function () {
selection: false selection: false
} }
} else { } else {
var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row);
var leftChar = line.substring(cursor.column-1, cursor.column);
// We're escaped.
if (leftChar == '\\') {
return false;
}
// Find what token we're inside.
var tokens = session.getTokens(selection.start.row, selection.start.row)[0].tokens;
var col = 0, token;
for (var x in tokens) {
token = tokens[x].type;
if ((tokens[x].value.length + col) > selection.start.column) {
break;
}
col += tokens[x].value.length;
}
// Try and be smart about when we auto insert.
if (token === "text" || !token || (token !== "comment" && selection.end.column === line.length)) {
return { return {
text: '""', text: '""',
selection: [1,1] selection: [1,1]
} }
} else if (token === "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 == '"') {
return {
text: '',
selection: [1, 1]
}
}
}
} }
} }
return false; return false;