From deb5a01693036bbdaf2e112fbaf44e5973ea5da9 Mon Sep 17 00:00:00 2001 From: c-spencer Date: Sun, 22 May 2011 13:27:36 +0100 Subject: [PATCH] Improved Cstyle behaviour --- lib/ace/mode/behaviour/cstyle.js | 60 ++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/lib/ace/mode/behaviour/cstyle.js b/lib/ace/mode/behaviour/cstyle.js index dd432ea6..801f81da 100644 --- a/lib/ace/mode/behaviour/cstyle.js +++ b/lib/ace/mode/behaviour/cstyle.js @@ -58,14 +58,17 @@ var CstyleBehaviour = function () { selection: [1, 1] } } - } else if (text == '}') { // This should do some matching checks + } 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 == '}') { - return { - text: '', - selection: [1, 1] + var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row}); + if (matching !== null) { + return { + text: '', + selection: [1, 1] + } } } } else if (text == "\n") { @@ -113,14 +116,17 @@ var CstyleBehaviour = function () { selection: [1, 1] } } - } else if (text == ')') { // This should do some matching checks + } 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 == ')') { - return { - text: '', - selection: [1, 1] + var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row}); + if (matching !== null) { + return { + text: '', + selection: [1, 1] + } } } } @@ -148,9 +154,41 @@ var CstyleBehaviour = function () { selection: false } } else { - return { - text: '""', - selection: [1,1] + 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 { + text: '""', + 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] + } + } } } }