remove lines returns the removed lines

This commit is contained in:
Fabian Jakobs 2011-01-19 08:14:28 +01:00
commit 2db0785544
2 changed files with 23 additions and 0 deletions

View file

@ -123,6 +123,14 @@ var Document = function(text) {
this.getLines = function(firstRow, lastRow) {
return this.$lines.slice(firstRow, lastRow+1);
};
/**
* Returns all lines in the document as string array. Warning: The caller
* should not modify this array!
*/
this.getAllLines = function() {
return this.$lines;
};
this.getLength = function() {
return this.$lines.length;
@ -287,6 +295,13 @@ var Document = function(text) {
return range.start;
};
/**
* Removes a range of full lines
*
* @param firstRow {Integer} The first row to be removed
* @param firstRow {Integer} The first row to be removed
* @return {String[]} The removed lines
*/
this.removeLines = function(firstRow, lastRow) {
var range = new Range(firstRow, 0, lastRow, this.$lines[lastRow].length);
var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1);
@ -298,6 +313,7 @@ var Document = function(text) {
lines: removed
};
this._dispatchEvent("change", { data: delta });
return removed;
};
this.removeNewLine = function(row) {

View file

@ -231,6 +231,13 @@ var Test = {
doc.remove(new Range(1, 0, 3, 0));
assert.equal(doc.getValue(), ["1234", ""].join("\n"));
},
"test: remove lines should return the removed lines" : function() {
var doc = new Document(["1234", "5678", "abcd"]);
var removed = doc.removeLines(1, 2);
assert.equal(removed.join("\n"), ["5678", "abcd"].join("\n"));
},
"test: should handle unix style new lines" : function() {
var doc = new Document(["1", "2", "3"]);