Factor out mode transforms into Behaviour
This commit is contained in:
parent
1261c55913
commit
3cdfa37427
5 changed files with 306 additions and 62 deletions
|
|
@ -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);
|
||||
|
|
|
|||
101
lib/ace/mode/behaviour.js
Normal file
101
lib/ace/mode/behaviour.js
Normal file
|
|
@ -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 <chris.ag.spencer AT googlemail DOT com>
|
||||
*
|
||||
* 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;
|
||||
});
|
||||
166
lib/ace/mode/behaviour/cstyle.js
Normal file
166
lib/ace/mode/behaviour/cstyle.js
Normal file
|
|
@ -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 <chris.ag.spencer AT googlemail DOT com>
|
||||
*
|
||||
* 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;
|
||||
});
|
||||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
* Contributor(s):
|
||||
* Fabian Jakobs <fabian AT ajax DOT org>
|
||||
* Mihai Sucan <mihai DOT sucan AT gmail DOT com>
|
||||
* Chris Spencer <chris.ag.spencer AT googlemail DOT com>
|
||||
*
|
||||
* 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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue