Simplify document.insert
This commit is contained in:
parent
bea0289a77
commit
831ef0745a
2 changed files with 24 additions and 31 deletions
|
|
@ -174,39 +174,18 @@ var Document = function(text) {
|
||||||
if (this.getLength() <= 1)
|
if (this.getLength() <= 1)
|
||||||
this.$detectNewLine(text);
|
this.$detectNewLine(text);
|
||||||
|
|
||||||
var newLines = this.$split(text);
|
var lines = this.$split(text);
|
||||||
|
var firstLine = lines.splice(0, 1)[0];
|
||||||
|
var lastLine = lines.length == 0 ? null
|
||||||
|
: lines.splice(lines.length - 1, 1)[0];
|
||||||
|
|
||||||
if (this.isNewLine(text)) {
|
position = this.insertInLine(position, firstLine);
|
||||||
var end = this.insertNewLine(position);
|
if (lastLine !== null) {
|
||||||
|
position = this.insertNewLine(position); // terminate first line
|
||||||
|
position = this.insertLines(position.row, lines);
|
||||||
|
position = this.insertInLine(position, lastLine || "");
|
||||||
}
|
}
|
||||||
else if (newLines.length == 1) {
|
return position;
|
||||||
var end = this.insertInLine(position, text);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (newLines[0].length > 0) {
|
|
||||||
var end = this.insertInLine(position, newLines[0]);
|
|
||||||
this.insertNewLine(end);
|
|
||||||
}
|
|
||||||
// If we are inserting at the end of the document, we don't need to
|
|
||||||
// use insertInLine (concorde depends on this optimization!)
|
|
||||||
if (position.row + 1 == this.getLength()) {
|
|
||||||
this.insertLines(position.row + 1,
|
|
||||||
newLines.slice(1, newLines.length));
|
|
||||||
var end = {
|
|
||||||
row: position.row + newLines.length - 1,
|
|
||||||
column: position.column + newLines[newLines.length - 1].length
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
if (newLines.length > 2)
|
|
||||||
this.insertLines(position.row + 1,
|
|
||||||
newLines.slice(1, newLines.length - 1));
|
|
||||||
var end = this.insertInLine({
|
|
||||||
row: position.row + newLines.length - 1,
|
|
||||||
column: 0
|
|
||||||
}, newLines[newLines.length - 1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return end;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.insertLines = function(row, lines) {
|
this.insertLines = function(row, lines) {
|
||||||
|
|
|
||||||
|
|
@ -325,6 +325,20 @@ var Test = {
|
||||||
assert.equal(session.$wrapData.length, 2);
|
assert.equal(session.$wrapData.length, 2);
|
||||||
assert.equal(session.$wrapData[0].length, 1);
|
assert.equal(session.$wrapData[0].length, 1);
|
||||||
assert.equal(session.$wrapData[1].length, 1);
|
assert.equal(session.$wrapData[1].length, 1);
|
||||||
|
},
|
||||||
|
|
||||||
|
"test first line blank with wrap": function() {
|
||||||
|
var session = new EditSession("\nfoo");
|
||||||
|
session.setUseWrapMode(true);
|
||||||
|
assert.equal(session.doc.getValue(), ["", "foo"].join("\n"));
|
||||||
|
},
|
||||||
|
|
||||||
|
"test first line blank with wrap 2" : function() {
|
||||||
|
var session = new EditSession("");
|
||||||
|
session.setUseWrapMode(true);
|
||||||
|
session.setValue("\nfoo");
|
||||||
|
|
||||||
|
assert.equal(session.doc.getValue(), ["", "foo"].join("\n"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue