diff --git a/lib/ace/anchor.js b/lib/ace/anchor.js new file mode 100644 index 00000000..7991038a --- /dev/null +++ b/lib/ace/anchor.js @@ -0,0 +1,138 @@ +/* ***** 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): + * Fabian Jakobs + * + * 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 EventEmitter = require("pilot/event_emitter").EventEmitter; + +/** + * An Anchor is a floating pointer in the document. Whenever text is inserted or + * deleted before the cursor, the position of the cursor is updated + */ +var Anchor = exports.Anchor = function(doc, row, column) { + this.document = doc; + this.pos = { + row: row, + column: column + }; + doc.on("change", this.onChange.bind(this)); +}; + +(function() { + + oop.implement(this, EventEmitter); + + this.getPosition = function() { + return { + row: this.pos.row, + column: this.pos.column + }; + }; + + this.getDocument = function() { + return this.document; + }; + + this.onChange = function(e) { + var delta = e.data; + var range = delta.range; + var pos = this.getPosition(); + + if (delta.action === "insertText") { + if (range.start.row === pos.row && range.start.column <= pos.column) { + if (range.start.row === range.end.row) { + pos.column += range.end.column - range.start.column; + } + else { + pos.column -= range.start.column; + pos.row += range.end.row - range.start.row; + } + } + else if (range.start.row !== range.end.row && range.start.row < pos.row) { + pos.row += range.end.row - range.start.row; + } + } else if (delta.action === "insertLines") { + if (range.start.row <= pos.row) { + pos.row += range.end.row - range.start.row; + } + } + else if (delta.action == "removeText") { + if (range.start.row == pos.row && range.start.column <= pos.column) { + pos.column -= Math.max(0, range.end.column - range.start.column); + } else if (range.start.row !== range.end.row && range.start.row < pos.row) { + if (range.end.row == pos.row) { + pos.column = Math.max(0, pos.column - range.end.column) + range.start.column; + } + pos.row -= (range.end.row - range.start.row); + } + else if (range.end.row == pos.row) { + pos.row -= range.end.row - range.start.row; + pos.column = pos.column - range.end.column + range.start.column; + } + } else if (delta.action == "removeLines") { + if (range.start.row <= pos.row) { + if (range.end.row <= pos.row) + pos.row -= range.end.row - range.start.row; + else { + pos.row = range.start.row; + pos.column = 0; + } + } + } + + // clip + var len = this.document.getLength(); + if (pos.row > len) { + pos.row = len - 1; + pos.column = this.document.getLine(len-1).length; + } + + this.setPosition(pos); + }; + + this.setPosition = function(pos) { + if (this.pos.row == pos.row && this.pos.column == pos.column) + return; + + this.pos = pos; + this._dispatchEvent("change"); + }; + +}).call(Anchor.prototype); + +}); diff --git a/lib/ace/test/all.js b/lib/ace/test/all.js index 46b5b55e..ad1bf2c3 100644 --- a/lib/ace/test/all.js +++ b/lib/ace/test/all.js @@ -40,6 +40,7 @@ require("../../../support/paths"); var async = require("asyncjs"); async.concat( + require("./anchor_test"), require("./change_document_test"), require("./document_test"), require("./edit_session_test"), diff --git a/lib/ace/test/anchor_test.js b/lib/ace/test/anchor_test.js new file mode 100644 index 00000000..1fcd32b1 --- /dev/null +++ b/lib/ace/test/anchor_test.js @@ -0,0 +1,155 @@ +/* ***** 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): + * Fabian Jakobs + * + * 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 ***** */ + +var Document = require("ace/document").Document; +var Anchor = require("ace/anchor").Anchor; +var Range = require("ace/range").Range; +var assert = require("./assertions"); +var async = require("asyncjs"); + +var Test = { + + "test create anchor" : function() { + var doc = new Document("juhu"); + var anchor = new Anchor(doc, 0, 0); + + assert.position(anchor.getPosition(), 0, 0); + assert.equal(anchor.getDocument(), doc); + }, + + "test insert text in same row before cursor should move anchor column": function() { + var doc = new Document("juhu\nkinners"); + var anchor = new Anchor(doc, 1, 4); + + doc.insert({row: 1, column: 1}, "123"); + assert.position(anchor.getPosition(), 1, 7); + }, + + "test insert lines before cursor should move anchor row": function() { + var doc = new Document("juhu\nkinners"); + var anchor = new Anchor(doc, 1, 4); + + doc.insertLines(1, ["123", "456"]); + assert.position(anchor.getPosition(), 3, 4); + }, + + "test insert new line before cursor should move anchor column": function() { + var doc = new Document("juhu\nkinners"); + var anchor = new Anchor(doc, 1, 4); + + doc.insertNewLine({row: 0, column: 0}); + assert.position(anchor.getPosition(), 2, 4); + }, + + "test insert new line in anchor line before anchor should move anchor column and row": function() { + var doc = new Document("juhu\nkinners"); + var anchor = new Anchor(doc, 1, 4); + + doc.insertNewLine({row: 1, column: 2}); + assert.position(anchor.getPosition(), 2, 2); + }, + + "test delete text in anchor line before anchor should move anchor column": function() { + var doc = new Document("juhu\nkinners"); + var anchor = new Anchor(doc, 1, 4); + + doc.remove(new Range(1, 1, 1, 3)); + assert.position(anchor.getPosition(), 1, 2); + }, + + "test delete lines in anchor line before anchor should move anchor row": function() { + var doc = new Document("juhu\n1\n2\nkinners"); + var anchor = new Anchor(doc, 3, 4); + + doc.removeLines(1, 2); + assert.position(anchor.getPosition(), 1, 4); + }, + + "test remove new line before the cursor": function() { + var doc = new Document("juhu\nkinners"); + var anchor = new Anchor(doc, 1, 4); + + doc.removeNewLine(0); + assert.position(anchor.getPosition(), 0, 8); + }, + + "test delete range which contains the anchor should move anchor to the end of the range": function() { + var doc = new Document("juhu\nkinners"); + var anchor = new Anchor(doc, 1, 4); + + doc.remove(new Range(0, 2, 1, 2)); + assert.position(anchor.getPosition(), 0, 4); + }, + + "test delete line which contains the anchor should move anchor to the end of the range": function() { + var doc = new Document("juhu\nkinners\n123"); + var anchor = new Anchor(doc, 1, 5); + + doc.removeLines(1, 1); + assert.position(anchor.getPosition(), 1, 0); + }, + + "test anchor changes triggered by document changes should emit change event": function(next) { + var doc = new Document("juhu\nkinners\n123"); + var anchor = new Anchor(doc, 1, 5); + + anchor.on("change", function(e) { + assert.position(anchor.getPosition(), 0, 0); + next(); + }); + + doc.remove(new Range(0, 0, 2, 1)); + }, + + "test only fire change event if position changes": function() { + var doc = new Document("juhu\nkinners\n123"); + var anchor = new Anchor(doc, 1, 5); + + anchor.on("change", function(e) { + assert.fail(); + }); + + doc.remove(new Range(2, 0, 2, 1)); + } +}; + +module.exports = require("asyncjs/test").testcase(Test); + +if (module === require.main) { + require("../../../support/paths"); + exports.exec() +}