From 5c9ebf6ce85de75b131d73f8a5afba3dc265f922 Mon Sep 17 00:00:00 2001 From: nightwing Date: Wed, 1 May 2013 17:50:47 +0400 Subject: [PATCH] insert/removeLines at the document edges create deltas with wrong range --- lib/ace/anchor_test.js | 11 +++++++++++ lib/ace/document.js | 25 ++++++++++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/ace/anchor_test.js b/lib/ace/anchor_test.js index b9c5cd66..2d7fcb63 100644 --- a/lib/ace/anchor_test.js +++ b/lib/ace/anchor_test.js @@ -167,6 +167,17 @@ module.exports = { }); doc.remove(new Range(2, 0, 2, 1)); + }, + + "test insert/remove lines at the end of the document": function() { + var doc = new Document("juhu\nkinners\n123"); + var anchor = new Anchor(doc, 2, 4); + + doc.removeLines(0, 3); + assert.position(anchor.getPosition(), 0, 0); + doc.insertLines(0, ["a", "b", "c"]); + assert.position(anchor.getPosition(), 3, 0); + assert.equal(doc.getValue(), "a\nb\nc\n"); } }; diff --git a/lib/ace/document.js b/lib/ace/document.js index 4d599688..8a073f86 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -59,7 +59,7 @@ var Document = function(text) { if (text.length == 0) { this.$lines = [""]; } else if (Array.isArray(text)) { - this.insertLines(0, text); + this._insertLines(0, text); } else { this.insert({row: 0, column:0}, text); } @@ -257,7 +257,7 @@ var Document = function(text) { position = this.insertInLine(position, firstLine); if (lastLine !== null) { position = this.insertNewLine(position); // terminate first line - position = this.insertLines(position.row, lines); + position = this._insertLines(position.row, lines); position = this.insertInLine(position, lastLine || ""); } return position; @@ -301,13 +301,18 @@ var Document = function(text) { * **/ this.insertLines = function(row, lines) { + if (row >= this.getLength()) + return this.insert({row: row, column: 0}, "\n" + lines.join("\n")); + return this._insertLines(Math.max(row, 0), lines); + }; + this._insertLines = function(row, lines) { if (lines.length == 0) return {row: row, column: 0}; // apply doesn't work for big arrays (smallest threshold is on safari 0xFFFF) // to circumvent that we have to break huge inserts into smaller chunks here if (lines.length > 0xFFFF) { - var end = this.insertLines(row, lines.slice(0xFFFF)); + var end = this._insertLines(row, lines.slice(0xFFFF)); lines = lines.slice(0, 0xFFFF); } @@ -415,7 +420,7 @@ var Document = function(text) { this.removeInLine(lastRow, 0, range.end.column); if (lastFullRow >= firstFullRow) - this.removeLines(firstFullRow, lastFullRow); + this._removeLines(firstFullRow, lastFullRow); if (firstFullRow != firstRow) { this.removeInLine(firstRow, range.start.column, this.getLine(firstRow).length); @@ -463,6 +468,12 @@ var Document = function(text) { * **/ this.removeLines = function(firstRow, lastRow) { + if (firstRow < 0 || lastRow >= this.getLength()) + return this.remove(new Range(firstRow, 0, lastRow + 1, 0)); + return this._removeLines(firstRow, lastRow); + }; + + this._removeLines = function(firstRow, lastRow) { var range = new Range(firstRow, 0, lastRow + 1, 0); var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1); @@ -541,7 +552,7 @@ var Document = function(text) { else if (delta.action == "insertText") this.insert(range.start, delta.text); else if (delta.action == "removeLines") - this.removeLines(range.start.row, range.end.row - 1); + this._removeLines(range.start.row, range.end.row - 1); else if (delta.action == "removeText") this.remove(range); } @@ -557,11 +568,11 @@ var Document = function(text) { var range = Range.fromPoints(delta.range.start, delta.range.end); if (delta.action == "insertLines") - this.removeLines(range.start.row, range.end.row - 1); + this._removeLines(range.start.row, range.end.row - 1); else if (delta.action == "insertText") this.remove(range); else if (delta.action == "removeLines") - this.insertLines(range.start.row, delta.lines); + this._insertLines(range.start.row, delta.lines); else if (delta.action == "removeText") this.insert(range.start, delta.text); }