From 3cdfa374278b24e51b8f005e2bad07926fce64c2 Mon Sep 17 00:00:00 2001 From: c-spencer Date: Sun, 22 May 2011 00:27:22 +0100 Subject: [PATCH] Factor out mode transforms into Behaviour --- lib/ace/editor.js | 17 +++- lib/ace/mode/behaviour.js | 101 +++++++++++++++++++ lib/ace/mode/behaviour/cstyle.js | 166 +++++++++++++++++++++++++++++++ lib/ace/mode/javascript.js | 2 + lib/ace/mode/text.js | 82 +++++---------- 5 files changed, 306 insertions(+), 62 deletions(-) create mode 100644 lib/ace/mode/behaviour.js create mode 100644 lib/ace/mode/behaviour/cstyle.js diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 14422369..4b1fdd29 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -412,8 +412,9 @@ var Editor =function(renderer, session) { var mode = session.getMode(); var cursor = this.getCursorPosition(); - var transform = mode.transformInsert(session.getState(cursor.row), this, session, text); + // Get a transform if the current mode wants one. + var transform = mode.transformInsert(session.getState(cursor.row), this, session, text); if (transform) text = transform.text; @@ -440,9 +441,17 @@ var Editor =function(renderer, session) { var end = session.insert(cursor, text); if (transform && transform.selection) { - this.selection.setSelectionRange( - new Range(cursor.row, start + transform.selection[0], - cursor.row, start + transform.selection[1])); + if (transform.selection.length == 2) { // Transform relative to the current column + this.selection.setSelectionRange( + new Range(cursor.row, start + transform.selection[0], + cursor.row, start + transform.selection[1])); + } else { // Transform relative to the current row. + this.selection.setSelectionRange( + new Range(cursor.row + transform.selection[0], + transform.selection[1], + cursor.row + transform.selection[2], + transform.selection[3])); + } } var lineState = session.getState(cursor.row); diff --git a/lib/ace/mode/behaviour.js b/lib/ace/mode/behaviour.js new file mode 100644 index 00000000..46886a21 --- /dev/null +++ b/lib/ace/mode/behaviour.js @@ -0,0 +1,101 @@ +/* vim:ts=4:sts=4:sw=4: + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Chris Spencer + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { + +var Behaviour = function() { + this.$behaviours = {}; +}; + +(function () { + + this.add = function (name, insert, remove) { + if (this.$behaviours === undefined) { + this.$behaviours = {}; + } + this.$behaviours[name] = { + insert: insert, + remove: remove + }; + } + + this.addBehaviours = function (behaviours) { + for (var key in behaviours) { + if (behaviours.hasOwnProperty(key)) { + this.addBehaviour(key, behaviours[key].insert, behaviours[key].remove); + } + } + } + + this.remove = function (name) { + if (this.$behaviours && this.$behaviours[name]) { + delete this.$behaviours[name]; + } + } + + this.inherit = function (mode) { + if (typeof mode === "function") { + var behaviours = new mode().getBehaviours(); + } else { + var behaviours = mode.getBehaviours(); + } + for (var key in behaviours) { + if (behaviours.hasOwnProperty(key)) { + this.add(key, behaviours[key].insert, behaviours[key].remove); + } + } + } + + this.getBehaviours = function (filter) { + if (!filter) { + return this.$behaviours; + } else { + var ret = {} + for (var i = 0; i < filter.length; i++) { + if (this.$behaviours[filter[i]]) { + ret[filter[i]] = this.$behaviours[filter[i]]; + } + } + return ret; + } + } + +}).call(Behaviour.prototype); + +exports.Behaviour = Behaviour; +}); \ No newline at end of file diff --git a/lib/ace/mode/behaviour/cstyle.js b/lib/ace/mode/behaviour/cstyle.js new file mode 100644 index 00000000..56a77581 --- /dev/null +++ b/lib/ace/mode/behaviour/cstyle.js @@ -0,0 +1,166 @@ +/* vim:ts=4:sts=4:sw=4: + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Chris Spencer + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { + +var oop = require("pilot/oop"); +var Behaviour = require('ace/mode/behaviour').Behaviour; + +var CstyleBehaviour = function () { + + this.add("brace_insert", function (state, editor, session, text) { + if (text == '{') { + var selection = editor.getSelectionRange(); + var selected = session.doc.getTextRange(selection); + if (selected !== "") { + return { + text: '{' + selected + '}', + selection: false + } + } else { + return { + text: '{}', + selection: [1,1] + } + } + } else if (text == '}') { // This should do some matching checks + var cursor = editor.getCursorPosition(); + var rightChar = session.doc.getLine(cursor.row).substring(cursor.column, cursor.column+1); + if (rightChar == '}') { + return { + text: '', + selection: [1,1] + } + } + } else if (text == "\n") { + var cursor = editor.getCursorPosition(); + var rightChar = session.doc.getLine(cursor.row).substring(cursor.column, cursor.column+1); + if (rightChar == '}') { + var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column}); + + var indent = this.getNextLineIndent(state, session.doc.getLine(cursor.row).substring(0, session.doc.getLine(cursor.row).length-1), " "); + var next_indent = this.$getIndent(session.doc.getLine(openBracePos.row)); + + return { + text: '\n'+indent+'\n'+next_indent, + selection: [1,indent.length,1,indent.length] + } + } + } + return false; + }, function (state, editor, session, range) { + var selected = session.doc.getTextRange(range); + if (!range.isMultiLine() && selected == '{') { + var rightChar = session.doc.getLine(range.start.row).substring(range.start.column+1, range.start.column+2); + if (rightChar == '}') { + return new Range(range.start.row, range.start.column, range.start.row, range.end.column+1); + } + } + return false; + }); + + this.add("parens_insert", function (state, editor, session, text) { + if (text == '(') { + var selection = editor.getSelectionRange(); + var selected = session.doc.getTextRange(selection); + if (selected !== "") { + return { + text: '(' + selected + ')', + selection: false + } + } else { + return { + text: '()', + selection: [1,1] + } + } + } else if (text == ')') { // This should do some matching checks + var cursor = editor.getCursorPosition(); + var rightChar = session.doc.getLine(cursor.row).substring(cursor.column, cursor.column+1); + if (rightChar == ')') { + return { + text: '', + selection: [1,1] + } + } + } + return false; + }, function (state, editor, session, range) { + var selected = session.doc.getTextRange(range); + if (!range.isMultiLine() && selected == '(') { + var rightChar = session.doc.getLine(range.start.row).substring(range.start.column+1, range.start.column+2); + if (rightChar == ')') { + return new Range(range.start.row, range.start.column, range.start.row, range.end.column+1); + } + } + return false; + }); + + this.add("string_dquotes", function (state, editor, session, text) { + if (text == '"') { + var selection = editor.getSelectionRange(); + var selected = session.doc.getTextRange(selection); + if (selected !== "") { + return { + text: '"' + selected + '"', + selection: false + } + } else { + return { + text: '""', + selection: [1,1] + } + } + } + return false; + }, function (state, editor, session, range) { + var selected = session.doc.getTextRange(range); + if (!range.isMultiLine() && selected == '"') { + var rightChar = session.doc.getLine(range.start.row).substring(range.start.column+1, range.start.column+2); + if (rightChar == '"') { + return new Range(range.start.row, range.start.column, range.start.row, range.end.column+1); + } + } + return false; + }); + +} +oop.inherits(CstyleBehaviour, Behaviour); + +exports.CstyleBehaviour = CstyleBehaviour; +}); \ No newline at end of file diff --git a/lib/ace/mode/javascript.js b/lib/ace/mode/javascript.js index a7634bf6..f7b934fa 100644 --- a/lib/ace/mode/javascript.js +++ b/lib/ace/mode/javascript.js @@ -44,10 +44,12 @@ var JavaScriptHighlightRules = require("ace/mode/javascript_highlight_rules").Ja var MatchingBraceOutdent = require("ace/mode/matching_brace_outdent").MatchingBraceOutdent; var Range = require("ace/range").Range; var WorkerClient = require("ace/worker/worker_client").WorkerClient; +var CstyleBehaviour = require("ace/mode/behaviour/cstyle").CstyleBehaviour; var Mode = function() { this.$tokenizer = new Tokenizer(new JavaScriptHighlightRules().getRules()); this.$outdent = new MatchingBraceOutdent(); + this.$behaviour = new CstyleBehaviour(); }; oop.inherits(Mode, TextMode); diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index 819b45aa..620364a7 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -22,6 +22,7 @@ * Contributor(s): * Fabian Jakobs * Mihai Sucan + * Chris Spencer * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -41,9 +42,11 @@ define(function(require, exports, module) { var Tokenizer = require("ace/tokenizer").Tokenizer; var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightRules; +var Behaviour = require("ace/mode/behaviour").Behaviour; var Mode = function() { this.$tokenizer = new Tokenizer(new TextHighlightRules().getRules()); + this.$behaviour = new Behaviour(); }; (function() { @@ -150,7 +153,7 @@ var Mode = function() { this.$modes[this.$embeds[i]] = new mapping[this.$embeds[i]](); } - var delegations = ['toggleCommentLines', 'getNextLineIndent', 'checkOutdent', 'autoOutdent', 'transformInput', 'transformRemove']; + var delegations = ['toggleCommentLines', 'getNextLineIndent', 'checkOutdent', 'autoOutdent', 'transformInsert', 'transformRemove']; for (var i = 0; i < delegations.length; i++) { (function(scope) { @@ -179,69 +182,32 @@ var Mode = function() { return defaultHandler ? defaultHandler.apply(this, args) : undefined; }; - this.addBehaviour = function (name, insert, remove) { - if (this.$behaviours === undefined) { - this.$behaviours = {}; - } - this.$behaviours[name] = { - insert: insert, - remove: remove - }; - } - - this.removeBehaviour = function (name) { - if (this.$behaviours && this.$behaviours[name]) { - delete this.$behaviours[name]; - } - } - - this.inheritBehaviours = function (mode) { - if (typeof mode === "function") { - var behaviours = new mode().getBehaviours(); - } else { - var behaviours = mode.getBehaviours(); - } - for (var key in behaviours) { - if (behaviours.hasOwnProperty(key)) { - this.addBehaviour(key, behaviours[key].insert, behaviours[key].remove); - } - } - } - - this.getBehaviours = function (filter) { - if (!filter) { - return this.$behaviours; - } else { - var ret = {} - for (var i = 0; i < filter.length; i++) { - if (this.$behaviours[filter[i]]) { - ret[filter[i]] = this.$behaviours[filter[i]]; - } - } - return ret; - } - } - this.transformInsert = function(state, editor, session, text) { - for (var key in this.$behaviours) { - if (this.$behaviours.hasOwnProperty(key)) { - var ret = this.$behaviours[key].insert.apply(this, arguments); - if (ret !== false) { - return ret; - } - } + if (this.$behaviour) { + var behaviours = this.$behaviour.getBehaviours(); + for (var key in behaviours) { + if (behaviours.hasOwnProperty(key)) { + var ret = behaviours[key].insert.apply(this, arguments); + if (ret !== false) { + return ret; + } + } + } } return false; }; this.transformRemove = function(state, editor, session, range) { - for (var key in this.$behaviours) { - if (this.$behaviours.hasOwnProperty(key)) { - var ret = this.$behaviours[key].remove.apply(this, arguments); - if (ret !== false) { - return ret; - } - } + if (this.$behaviour) { + var behaviours = this.$behaviour.getBehaviours(); + for (var key in behaviours) { + if (behaviours.hasOwnProperty(key)) { + var ret = behaviours[key].remove.apply(this, arguments); + if (ret !== false) { + return ret; + } + } + } } return range; };