From d9e7970d2abf311718c4b49e85593a4ef4ad90dc Mon Sep 17 00:00:00 2001 From: c-spencer Date: Wed, 25 May 2011 16:56:40 +0100 Subject: [PATCH] Improve string behaviour, start on Xml behaviours, disable behaviours by default --- lib/ace/editor.js | 2 +- lib/ace/mode/behaviour.js | 6 ++--- lib/ace/mode/behaviour/cstyle.js | 17 +++++++++---- lib/ace/mode/behaviour/xml.js | 41 ++++++++++++++++++++++++++++++++ lib/ace/mode/xml.js | 2 ++ 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 lib/ace/mode/behaviour/xml.js diff --git a/lib/ace/editor.js b/lib/ace/editor.js index ea8ff9d4..f15024db 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -611,7 +611,7 @@ var Editor =function(renderer, session) { return this.$readOnly; }; - this.$modeBehaviours = true; + this.$modeBehaviours = false; this.setBehavioursEnabled = function (enabled) { this.$modeBehaviours = enabled; } diff --git a/lib/ace/mode/behaviour.js b/lib/ace/mode/behaviour.js index 6b57d3d3..6c145d22 100644 --- a/lib/ace/mode/behaviour.js +++ b/lib/ace/mode/behaviour.js @@ -72,11 +72,11 @@ var Behaviour = function() { } } - this.inherit = function (mode) { + this.inherit = function (mode, filter) { if (typeof mode === "function") { - var behaviours = new mode().getBehaviours(); + var behaviours = new mode().getBehaviours(filter); } else { - var behaviours = mode.getBehaviours(); + var behaviours = mode.getBehaviours(filter); } this.addBehaviours(behaviours); } diff --git a/lib/ace/mode/behaviour/cstyle.js b/lib/ace/mode/behaviour/cstyle.js index cbbbb283..d685ad06 100644 --- a/lib/ace/mode/behaviour/cstyle.js +++ b/lib/ace/mode/behaviour/cstyle.js @@ -170,21 +170,28 @@ var CstyleBehaviour = function () { // 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) { + var quotepos = -1; // Track whether we're inside an open quote. + + for (var x = 0; x < tokens.length; x++) { + token = tokens[x]; + if (token.type == "string") { + quotepos = -1; + } else if (quotepos < 0) { + quotepos = token.value.indexOf('"'); + } + if ((token.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)) { + if (quotepos < 0 && token.type !== "comment" && (token.type !== "string" || ((selection.start.column !== token.value.length+col-1) && token.value.lastIndexOf('"') === token.value.length-1))) { return { text: '""', selection: [1,1] } - } else if (token === "string") { + } 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 == '"') { diff --git a/lib/ace/mode/behaviour/xml.js b/lib/ace/mode/behaviour/xml.js new file mode 100644 index 00000000..7f1b02d9 --- /dev/null +++ b/lib/ace/mode/behaviour/xml.js @@ -0,0 +1,41 @@ +define(function(require, exports, module) { + +var oop = require("pilot/oop"); +var Behaviour = require('ace/mode/behaviour').Behaviour; +var CstyleBehaviour = require('ace/mode/behaviour/cstyle').CstyleBehaviour; + +var XmlBehaviour = function () { + + this.inherit(CstyleBehaviour, ["string_dquotes"]); // Get string behaviour + + this.add("brackets", "insertion", function (state, action, editor, session, text) { + if (text == '<') { + var selection = editor.getSelectionRange(); + var selected = session.doc.getTextRange(selection); + if (selected !== "") { + return false; + } else { + return { + text: '<>', + selection: [1, 1] + } + } + } 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 == '>') { // need some kind of matching check here + return { + text: '', + selection: [1, 1] + } + } + } + return false; + }); + +} +oop.inherits(XmlBehaviour, Behaviour); + +exports.XmlBehaviour = XmlBehaviour; +}); \ No newline at end of file diff --git a/lib/ace/mode/xml.js b/lib/ace/mode/xml.js index edaa5ffb..a6d5fa9c 100644 --- a/lib/ace/mode/xml.js +++ b/lib/ace/mode/xml.js @@ -41,9 +41,11 @@ var oop = require("pilot/oop"); var TextMode = require("ace/mode/text").Mode; var Tokenizer = require("ace/tokenizer").Tokenizer; var XmlHighlightRules = require("ace/mode/xml_highlight_rules").XmlHighlightRules; +var XmlBehaviour = require("ace/mode/behaviour/xml").XmlBehaviour; var Mode = function() { this.$tokenizer = new Tokenizer(new XmlHighlightRules().getRules()); + this.$behaviour = new XmlBehaviour(); }; oop.inherits(Mode, TextMode);