insert/removeLines at the document edges create deltas with wrong range

This commit is contained in:
nightwing 2013-05-01 17:50:47 +04:00
commit 5c9ebf6ce8
2 changed files with 29 additions and 7 deletions

View file

@ -167,6 +167,17 @@ module.exports = {
}); });
doc.remove(new Range(2, 0, 2, 1)); 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");
} }
}; };

View file

@ -59,7 +59,7 @@ var Document = function(text) {
if (text.length == 0) { if (text.length == 0) {
this.$lines = [""]; this.$lines = [""];
} else if (Array.isArray(text)) { } else if (Array.isArray(text)) {
this.insertLines(0, text); this._insertLines(0, text);
} else { } else {
this.insert({row: 0, column:0}, text); this.insert({row: 0, column:0}, text);
} }
@ -257,7 +257,7 @@ var Document = function(text) {
position = this.insertInLine(position, firstLine); position = this.insertInLine(position, firstLine);
if (lastLine !== null) { if (lastLine !== null) {
position = this.insertNewLine(position); // terminate first line position = this.insertNewLine(position); // terminate first line
position = this.insertLines(position.row, lines); position = this._insertLines(position.row, lines);
position = this.insertInLine(position, lastLine || ""); position = this.insertInLine(position, lastLine || "");
} }
return position; return position;
@ -301,13 +301,18 @@ var Document = function(text) {
* *
**/ **/
this.insertLines = function(row, lines) { 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) if (lines.length == 0)
return {row: row, column: 0}; return {row: row, column: 0};
// apply doesn't work for big arrays (smallest threshold is on safari 0xFFFF) // 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 // to circumvent that we have to break huge inserts into smaller chunks here
if (lines.length > 0xFFFF) { 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); lines = lines.slice(0, 0xFFFF);
} }
@ -415,7 +420,7 @@ var Document = function(text) {
this.removeInLine(lastRow, 0, range.end.column); this.removeInLine(lastRow, 0, range.end.column);
if (lastFullRow >= firstFullRow) if (lastFullRow >= firstFullRow)
this.removeLines(firstFullRow, lastFullRow); this._removeLines(firstFullRow, lastFullRow);
if (firstFullRow != firstRow) { if (firstFullRow != firstRow) {
this.removeInLine(firstRow, range.start.column, this.getLine(firstRow).length); this.removeInLine(firstRow, range.start.column, this.getLine(firstRow).length);
@ -463,6 +468,12 @@ var Document = function(text) {
* *
**/ **/
this.removeLines = function(firstRow, lastRow) { 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 range = new Range(firstRow, 0, lastRow + 1, 0);
var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1); var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1);
@ -541,7 +552,7 @@ var Document = function(text) {
else if (delta.action == "insertText") else if (delta.action == "insertText")
this.insert(range.start, delta.text); this.insert(range.start, delta.text);
else if (delta.action == "removeLines") 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") else if (delta.action == "removeText")
this.remove(range); this.remove(range);
} }
@ -557,11 +568,11 @@ var Document = function(text) {
var range = Range.fromPoints(delta.range.start, delta.range.end); var range = Range.fromPoints(delta.range.start, delta.range.end);
if (delta.action == "insertLines") 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") else if (delta.action == "insertText")
this.remove(range); this.remove(range);
else if (delta.action == "removeLines") else if (delta.action == "removeLines")
this.insertLines(range.start.row, delta.lines); this._insertLines(range.start.row, delta.lines);
else if (delta.action == "removeText") else if (delta.action == "removeText")
this.insert(range.start, delta.text); this.insert(range.start, delta.text);
} }