strip document handling from the edit session

This commit is contained in:
Fabian Jakobs 2011-01-19 08:15:03 +01:00
commit 8a8cf3265e
2 changed files with 174 additions and 428 deletions

View file

@ -43,13 +43,12 @@ var EventEmitter = require("pilot/event_emitter").EventEmitter;
var Selection = require("ace/selection").Selection; var Selection = require("ace/selection").Selection;
var TextMode = require("ace/mode/text").Mode; var TextMode = require("ace/mode/text").Mode;
var Range = require("ace/range").Range; var Range = require("ace/range").Range;
var Document = require("ace/document").Document;
var NO_CHANGE_DELTAS = {}; var NO_CHANGE_DELTAS = {};
var EditSession = function(text, mode) { var EditSession = function(text, mode) {
this.modified = true; this.modified = true;
this.$lines = [];
this.selection = new Selection(this); this.selection = new Selection(this);
this.$breakpoints = []; this.$breakpoints = [];
@ -58,10 +57,10 @@ var EditSession = function(text, mode) {
this.setMode(mode); this.setMode(mode);
} }
if (Array.isArray(text)) { if (text instanceof Document) {
this.$insertLines(0, text); this.setDocument(text)
} else { } else {
this.$insert({row: 0, column: 0}, text); this.setDocument(new Document(text));
} }
}; };
@ -70,44 +69,37 @@ var EditSession = function(text, mode) {
oop.implement(this, EventEmitter); oop.implement(this, EventEmitter);
this.$undoManager = null; this.setDocument = function(doc) {
if (this.doc)
throw new Error("Document is already set");
// check for IE split bug this.doc = doc;
if ("aaa".split(/a/).length == 0) doc.on("change", this.onChange.bind(this));
this.$split = function(text) { };
return text.replace(/\r\n|\r/g, "\n").split("\n");
this.onChange = function(e) {
var delta = e.data;
this.$modified = true;
if (!this.$fromUndo && this.$undoManager) {
this.$deltas.push(delta);
this.$informUndoManager.schedule();
} }
else this._dispatchEvent("change", e);
this.$split = function(text) {
return text.split(/\r\n|\r|\n/);
}; };
this.setValue = function(text) { this.setValue = function(text) {
var args = [0, this.$lines.length]; this.doc.setValue(text);
args.push.apply(args, this.$split(text));
this.$lines.splice.apply(this.$lines, args);
this.modified = true;
this.fireChangeEvent(0);
}; };
this.getValue =
this.toString = function() { this.toString = function() {
return this.$lines.join(this.$getNewLineCharacter()); return this.doc.getValue();
}; };
this.getValue = this.toString;
this.getSelection = function() { this.getSelection = function() {
return this.selection; return this.selection;
}; };
this.fireChangeEvent = function(firstRow, lastRow) {
var data = {
firstRow: firstRow,
lastRow: lastRow
};
this._dispatchEvent("change", { data: data});
};
this.setUndoManager = function(undoManager) { this.setUndoManager = function(undoManager) {
this.$undoManager = undoManager; this.$undoManager = undoManager;
this.$deltas = []; this.$deltas = [];
@ -161,7 +153,7 @@ var EditSession = function(text, mode) {
this.setTabSize = function(tabSize) { this.setTabSize = function(tabSize) {
if (isNaN(tabSize) || this.$tabSize === tabSize) return; if (isNaN(tabSize) || this.$tabSize === tabSize) return;
this.modified = true; this.$modified = true;
this.$tabSize = tabSize; this.$tabSize = tabSize;
this._dispatchEvent("changeTabSize"); this._dispatchEvent("changeTabSize");
}; };
@ -244,29 +236,12 @@ var EditSession = function(text, mode) {
return new Range(row, start, row, end); return new Range(row, start, row, end);
}; };
this.$getNewLineCharacter = function() {
switch (this.$newLineMode) {
case "windows":
return "\r\n";
case "unix":
return "\n";
case "auto":
return this.$autoNewLine;
}
},
this.$autoNewLine = "\n";
this.$newLineMode = "auto";
this.setNewLineMode = function(newLineMode) { this.setNewLineMode = function(newLineMode) {
if (this.$newLineMode === newLineMode) return; this.doc.setNewLineMode(newLineMode);
this.$newLineMode = newLineMode;
}; };
this.getNewLineMode = function() { this.getNewLineMode = function() {
return this.$newLineMode; return this.doc.getNewLineMode();
}; };
this.$mode = null; this.$mode = null;
@ -307,10 +282,10 @@ var EditSession = function(text, mode) {
}; };
this.$computeWidth = function() { this.$computeWidth = function() {
if (this.modified) { if (this.$modified) {
this.modified = false; this.$modified = false;
var lines = this.$lines; var lines = this.doc.getAllLines();
var longestLine = 0; var longestLine = 0;
var longestScreenLine = 0; var longestScreenLine = 0;
var tabSize = this.getTabSize(); var tabSize = this.getTabSize();
@ -334,7 +309,7 @@ var EditSession = function(text, mode) {
* Get a verbatim copy of the given line as it is in the document * Get a verbatim copy of the given line as it is in the document
*/ */
this.getLine = function(row) { this.getLine = function(row) {
return this.$lines[row] || ""; return this.doc.getLine(row);
}; };
/** /**
@ -342,29 +317,19 @@ var EditSession = function(text, mode) {
*/ */
this.getDisplayLine = function(row) { this.getDisplayLine = function(row) {
var tab = new Array(this.getTabSize()+1).join(" "); var tab = new Array(this.getTabSize()+1).join(" ");
return this.$lines[row].replace(/\t/g, tab); return this.doc.getLine(row).replace(/\t/g, tab);
}; };
this.getLines = function(firstRow, lastRow) { this.getLines = function(firstRow, lastRow) {
return this.$lines.slice(firstRow, lastRow+1); return this.doc.getLines(firstRow, lastRow);
}; };
this.getLength = function() { this.getLength = function() {
return this.$lines.length; return this.doc.getLength();
}; };
this.getTextRange = function(range) { this.getTextRange = function(range) {
if (range.start.row == range.end.row) { return this.doc.getTextRange(range);
return this.$lines[range.start.row].substring(range.start.column,
range.end.column);
}
else {
var lines = [];
lines.push(this.$lines[range.start.row].substring(range.start.column));
lines.push.apply(lines, this.getLines(range.start.row+1, range.end.row-1));
lines.push(this.$lines[range.end.row].substring(0, range.end.column));
return lines.join(this.$getNewLineCharacter());
}
}; };
this.findMatchingBracket = function(position) { this.findMatchingBracket = function(position) {
@ -459,25 +424,20 @@ var EditSession = function(text, mode) {
return null; return null;
}; };
this.insert = function(position, text, fromUndo) { this.insert = function(position, text) {
var end = this.$insert(position, text, fromUndo); return this.doc.insert(position, text);
this.fireChangeEvent(position.row, position.row == end.row ? position.row
: undefined);
return end;
}; };
/** /**
* @param rows Array[Integer] sorted list of rows * @param rows Array[Integer] sorted list of rows
*/ */
this.multiRowInsert = function(rows, column, text) { this.multiRowInsert = function(rows, column, text) {
var lines = this.$lines;
for (var i=rows.length-1; i>=0; i--) { for (var i=rows.length-1; i>=0; i--) {
var row = rows[i]; var row = rows[i];
if (row >= lines.length) if (row >= this.doc.getLength())
continue; continue;
var diff = column - lines[row].length; var diff = column - this.doc.getLine(row).length;
if ( diff > 0) { if ( diff > 0) {
var padded = lang.stringRepeat(" ", diff) + text; var padded = lang.stringRepeat(" ", diff) + text;
var offset = -diff; var offset = -diff;
@ -487,124 +447,17 @@ var EditSession = function(text, mode) {
offset = 0; offset = 0;
} }
var end = this.$insert({row: row, column: column+offset}, padded, false); var end = this.insert({row: row, column: column+offset}, padded);
} }
if (end) {
this.fireChangeEvent(rows[0], rows[rows.length-1] + end.row - rows[0]);
return { return {
rows: end.row - rows[0], rows: end ? end.row - rows[0] : 0,
columns: end.column - column columns: end ? end.column - column : 0
}
}
else {
return {
rows: 0,
columns: 0
}
} }
}; };
this.$insertLines = function(row, lines, fromUndo) { this.remove = function(range) {
if (lines.length == 0) return this.doc.remove(range);
return;
var args = [row, 0];
args.push.apply(args, lines);
this.$lines.splice.apply(this.$lines, args);
var nl = this.$getNewLineCharacter();
var delta = {
action: "insertText",
range: new Range(row, 0, row + lines.length, 0),
text: lines.join(nl) + nl
};
if (!fromUndo && this.$undoManager) {
this.$deltas.push(delta);
this.$informUndoManager.schedule();
}
if (fromUndo !== NO_CHANGE_DELTAS) {
this._dispatchEvent("changeDelta", { data: delta });
}
},
this.$insert = function(position, text, fromUndo) {
if (text.length == 0)
return position;
this.modified = true;
if (this.$lines.length <= 1) {
this.$detectNewLine(text);
}
var newLines = this.$split(text);
if (this.$isNewLine(text)) {
var line = this.$lines[position.row] || "";
this.$lines[position.row] = line.substring(0, position.column);
this.$lines.splice(position.row + 1, 0, line.substring(position.column));
var end = {
row : position.row + 1,
column : 0
};
}
else if (newLines.length == 1) {
var line = this.$lines[position.row] || "";
this.$lines[position.row] = line.substring(0, position.column) + text
+ line.substring(position.column);
var end = {
row : position.row,
column : position.column + text.length
};
}
else {
var line = this.$lines[position.row] || "";
var firstLine = line.substring(0, position.column) + newLines[0];
var lastLine = newLines[newLines.length - 1] + line.substring(position.column);
this.$lines[position.row] = firstLine;
this.$insertLines(position.row + 1, [lastLine], NO_CHANGE_DELTAS);
if (newLines.length > 2) {
this.$insertLines(position.row + 1, newLines.slice(1, -1), NO_CHANGE_DELTAS);
}
var end = {
row : position.row + newLines.length - 1,
column : newLines[newLines.length - 1].length
};
}
var delta = {
action: "insertText",
range: Range.fromPoints(position, end),
text: text
};
if (!fromUndo && this.$undoManager) {
this.$deltas.push(delta);
this.$informUndoManager.schedule();
}
this._dispatchEvent("changeDelta", { data: delta });
return end;
};
this.$isNewLine = function(text) {
return (text == "\r\n" || text == "\r" || text == "\n");
};
this.remove = function(range, fromUndo) {
if (range.isEmpty())
return range.start;
this.$remove(range, fromUndo);
this.fireChangeEvent(range.start.row, range.isMultiLine() ? undefined : range.start.row);
return range.start;
}; };
this.multiRowRemove = function(rows, range) { this.multiRowRemove = function(rows, range) {
@ -614,53 +467,16 @@ var EditSession = function(text, mode) {
var height = range.end.row - rows[0]; var height = range.end.row - rows[0];
for (var i=rows.length-1; i>=0; i--) { for (var i=rows.length-1; i>=0; i--) {
var row = rows[i]; var row = rows[i];
if (row >= this.$lines.length) if (row >= this.doc.getLength())
continue; continue;
var end = this.$remove(new Range(row, range.start.column, row+height, range.end.column), false); var end = this.remove(new Range(row, range.start.column, row+height, range.end.column));
} }
if (end) {
if (height < 0)
this.fireChangeEvent(rows[0]+height, undefined);
else
this.fireChangeEvent(rows[0], height == 0 ? rows[rows.length-1] : undefined);
}
};
this.$remove = function(range, fromUndo) {
if (range.isEmpty())
return;
var delta = {
action: "removeText",
range: range.clone(),
text: this.getTextRange(range)
};
if (!fromUndo && this.$undoManager) {
this.$deltas.push(delta);
this.$informUndoManager.schedule();
}
this.modified = true;
var firstRow = range.start.row;
var lastRow = range.end.row;
var row = this.getLine(firstRow).substring(0, range.start.column)
+ this.getLine(lastRow).substring(range.end.column);
if (row != "")
this.$lines.splice(firstRow, lastRow - firstRow + 1, row);
else
this.$lines.splice(firstRow, lastRow - firstRow + 1, "");
this._dispatchEvent("changeDelta", { data: delta });
return range.start;
}; };
this.undoChanges = function(deltas) { this.undoChanges = function(deltas) {
this.selection.clearSelection(); this.selection.clearSelection();
this.$fromUndo = true;
for (var i=deltas.length-1; i>=0; i--) { for (var i=deltas.length-1; i>=0; i--) {
var delta = deltas[i]; var delta = deltas[i];
if (delta.action == "insertText") { if (delta.action == "insertText") {
@ -671,10 +487,13 @@ var EditSession = function(text, mode) {
this.selection.clearSelection(); this.selection.clearSelection();
} }
} }
this.$fromUndo = false;
}, },
this.redoChanges = function(deltas) { this.redoChanges = function(deltas) {
this.selection.clearSelection(); this.selection.clearSelection();
this.$fromUndo = true;
for (var i=0; i<deltas.length; i++) { for (var i=0; i<deltas.length; i++) {
var delta = deltas[i]; var delta = deltas[i];
if (delta.action == "insertText") { if (delta.action == "insertText") {
@ -685,41 +504,18 @@ var EditSession = function(text, mode) {
this.selection.moveCursorToPosition(delta.range.start); this.selection.moveCursorToPosition(delta.range.start);
} }
} }
this.$fromUndo = false;
}, },
this.replace = function(range, text) { this.replace = function(range, text) {
// Shortcut: Nothing to replace in this case. return this.doc.replace(range, text);
if (text.length == 0 && range.isEmpty()) {
return range.start;
} else
// Shortcut: If the text we want to insert is the same as it is already
// in the document, we don't have to replace anything.
if (text == this.getTextRange(range)) {
return range.end;
}
this.$remove(range);
if (text) {
var end = this.$insert(range.start, text);
}
else {
end = range.start;
}
var lastRemoved = range.end.column == 0 ? range.end.column - 1
: range.end.column;
this.fireChangeEvent(range.start.row, lastRemoved == end.row ? lastRemoved
: undefined);
return end;
}; };
this.indentRows = function(startRow, endRow, indentString) { this.indentRows = function(startRow, endRow, indentString) {
indentString = indentString.replace("\t", this.getTabString()); indentString = indentString.replace("\t", this.getTabString());
for (var row=startRow; row<=endRow; row++) { for (var row=startRow; row<=endRow; row++) {
this.$insert({row: row, column:0}, indentString); this.insert({row: row, column:0}, indentString);
} }
this.fireChangeEvent(startRow, endRow);
return indentString.length; return indentString.length;
}; };
@ -747,31 +543,24 @@ var EditSession = function(text, mode) {
range.start.column -= deleteRange.end.column - deleteRange.start.column; range.start.column -= deleteRange.end.column - deleteRange.start.column;
if (i == range.end.row) if (i == range.end.row)
range.end.column -= deleteRange.end.column - deleteRange.start.column; range.end.column -= deleteRange.end.column - deleteRange.start.column;
this.$remove(deleteRange); this.remove(deleteRange);
} }
this.fireChangeEvent(range.start.row, range.end.row);
return range; return range;
} }
this.moveLinesUp = function(firstRow, lastRow) { this.moveLinesUp = function(firstRow, lastRow) {
if (firstRow <= 0) return 0; if (firstRow <= 0) return 0;
var removed = this.$lines.slice(firstRow, lastRow + 1); var removed = this.doc.removeLines(firstRow, lastRow);
this.$remove(new Range(firstRow-1, this.$lines[firstRow-1].length, lastRow, this.$lines[lastRow].length)); this.doc.insertLines(firstRow - 1, removed);
this.$insertLines(firstRow - 1, removed);
this.fireChangeEvent(firstRow - 1, lastRow);
return -1; return -1;
}; };
this.moveLinesDown = function(firstRow, lastRow) { this.moveLinesDown = function(firstRow, lastRow) {
if (lastRow >= this.$lines.length-1) return 0; if (lastRow >= this.doc.getLength()-1) return 0;
var removed = this.$lines.slice(firstRow, lastRow + 1); var removed = this.doc.removeLines(firstRow, lastRow);
this.$remove(new Range(firstRow, 0, lastRow + 1, 0)); this.doc.insertLines(firstRow+1, removed);
this.$insertLines(firstRow+1, removed);
this.fireChangeEvent(firstRow, lastRow + 1);
return 1; return 1;
}; };
@ -780,16 +569,14 @@ var EditSession = function(text, mode) {
var lastRow = this.$clipRowToDocument(lastRow); var lastRow = this.$clipRowToDocument(lastRow);
var lines = this.getLines(firstRow, lastRow); var lines = this.getLines(firstRow, lastRow);
this.$insertLines(firstRow, lines); this.doc.insertLines(firstRow, lines);
var addedRows = lastRow - firstRow + 1; var addedRows = lastRow - firstRow + 1;
this.fireChangeEvent(firstRow);
return addedRows; return addedRows;
}; };
this.$clipRowToDocument = function(row) { this.$clipRowToDocument = function(row) {
return Math.max(0, Math.min(row, this.$lines.length-1)); return Math.max(0, Math.min(row, this.doc.getLength()-1));
}; };
this.documentToScreenColumn = function(row, docColumn) { this.documentToScreenColumn = function(row, docColumn) {

View file

@ -37,7 +37,7 @@
define(function(require, exports, module) { define(function(require, exports, module) {
var Document = require("../document").Document, var EditSession = require("../edit_session").EditSession,
UndoManager = require("../undomanager").UndoManager, UndoManager = require("../undomanager").UndoManager,
MockRenderer = require("./mockrenderer"), MockRenderer = require("./mockrenderer"),
Range = require("../range").Range, Range = require("../range").Range,
@ -47,242 +47,201 @@ var Document = require("../document").Document,
var Test = { var Test = {
"test: find matching opening bracket" : function() { "test: find matching opening bracket" : function() {
var doc = new Document(["(()(", "())))"]); var session = new EditSession(["(()(", "())))"]);
assert.position(doc.findMatchingBracket({row: 0, column: 3}), 0, 1); assert.position(session.findMatchingBracket({row: 0, column: 3}), 0, 1);
assert.position(doc.findMatchingBracket({row: 1, column: 2}), 1, 0); assert.position(session.findMatchingBracket({row: 1, column: 2}), 1, 0);
assert.position(doc.findMatchingBracket({row: 1, column: 3}), 0, 3); assert.position(session.findMatchingBracket({row: 1, column: 3}), 0, 3);
assert.position(doc.findMatchingBracket({row: 1, column: 4}), 0, 0); assert.position(session.findMatchingBracket({row: 1, column: 4}), 0, 0);
assert.equal(doc.findMatchingBracket({row: 1, column: 5}), null); assert.equal(session.findMatchingBracket({row: 1, column: 5}), null);
}, },
"test: find matching closing bracket" : function() { "test: find matching closing bracket" : function() {
var doc = new Document(["(()(", "())))"]); var session = new EditSession(["(()(", "())))"]);
assert.position(doc.findMatchingBracket({row: 1, column: 1}), 1, 1); assert.position(session.findMatchingBracket({row: 1, column: 1}), 1, 1);
assert.position(doc.findMatchingBracket({row: 1, column: 1}), 1, 1); assert.position(session.findMatchingBracket({row: 1, column: 1}), 1, 1);
assert.position(doc.findMatchingBracket({row: 0, column: 4}), 1, 2); assert.position(session.findMatchingBracket({row: 0, column: 4}), 1, 2);
assert.position(doc.findMatchingBracket({row: 0, column: 2}), 0, 2); assert.position(session.findMatchingBracket({row: 0, column: 2}), 0, 2);
assert.position(doc.findMatchingBracket({row: 0, column: 1}), 1, 3); assert.position(session.findMatchingBracket({row: 0, column: 1}), 1, 3);
assert.equal(doc.findMatchingBracket({row: 0, column: 0}), null); assert.equal(session.findMatchingBracket({row: 0, column: 0}), null);
}, },
"test: match different bracket types" : function() { "test: match different bracket types" : function() {
var doc = new Document(["({[", ")]}"]); var session = new EditSession(["({[", ")]}"]);
assert.position(doc.findMatchingBracket({row: 0, column: 1}), 1, 0); assert.position(session.findMatchingBracket({row: 0, column: 1}), 1, 0);
assert.position(doc.findMatchingBracket({row: 0, column: 2}), 1, 2); assert.position(session.findMatchingBracket({row: 0, column: 2}), 1, 2);
assert.position(doc.findMatchingBracket({row: 0, column: 3}), 1, 1); assert.position(session.findMatchingBracket({row: 0, column: 3}), 1, 1);
assert.position(doc.findMatchingBracket({row: 1, column: 1}), 0, 0); assert.position(session.findMatchingBracket({row: 1, column: 1}), 0, 0);
assert.position(doc.findMatchingBracket({row: 1, column: 2}), 0, 2); assert.position(session.findMatchingBracket({row: 1, column: 2}), 0, 2);
assert.position(doc.findMatchingBracket({row: 1, column: 3}), 0, 1); assert.position(session.findMatchingBracket({row: 1, column: 3}), 0, 1);
}, },
"test: move lines down" : function() { "test: move lines down" : function() {
var doc = new Document(["a1", "a2", "a3", "a4"]); var session = new EditSession(["a1", "a2", "a3", "a4"]);
doc.moveLinesDown(0, 1); session.moveLinesDown(0, 1);
assert.equal(doc.toString(), ["a3", "a1", "a2", "a4"].join("\n")); assert.equal(session.getValue(), ["a3", "a1", "a2", "a4"].join("\n"));
doc.moveLinesDown(1, 2); session.moveLinesDown(1, 2);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n")); assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesDown(2, 3); session.moveLinesDown(2, 3);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n")); assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesDown(2, 2); session.moveLinesDown(2, 2);
assert.equal(doc.toString(), ["a3", "a4", "a2", "a1"].join("\n")); assert.equal(session.getValue(), ["a3", "a4", "a2", "a1"].join("\n"));
}, },
"test: move lines up" : function() { "test: move lines up" : function() {
var doc = new Document(["a1", "a2", "a3", "a4"]); var session = new EditSession(["a1", "a2", "a3", "a4"]);
doc.moveLinesUp(2, 3); session.moveLinesUp(2, 3);
assert.equal(doc.toString(), ["a1", "a3", "a4", "a2"].join("\n")); assert.equal(session.getValue(), ["a1", "a3", "a4", "a2"].join("\n"));
doc.moveLinesUp(1, 2); session.moveLinesUp(1, 2);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n")); assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesUp(0, 1); session.moveLinesUp(0, 1);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n")); assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesUp(2, 2); session.moveLinesUp(2, 2);
assert.equal(doc.toString(), ["a3", "a1", "a4", "a2"].join("\n")); assert.equal(session.getValue(), ["a3", "a1", "a4", "a2"].join("\n"));
}, },
"test: duplicate lines" : function() { "test: duplicate lines" : function() {
var doc = new Document(["1", "2", "3", "4"]); var session = new EditSession(["1", "2", "3", "4"]);
doc.duplicateLines(1, 2); session.duplicateLines(1, 2);
assert.equal(doc.toString(), ["1", "2", "3", "2", "3", "4"].join("\n")); assert.equal(session.getValue(), ["1", "2", "3", "2", "3", "4"].join("\n"));
}, },
"test: duplicate last line" : function() { "test: duplicate last line" : function() {
var doc = new Document(["1", "2", "3"]); var session = new EditSession(["1", "2", "3"]);
doc.duplicateLines(2, 2); session.duplicateLines(2, 2);
assert.equal(doc.toString(), ["1", "2", "3", "3"].join("\n")); assert.equal(session.getValue(), ["1", "2", "3", "3"].join("\n"));
}, },
"test: duplicate first line" : function() { "test: duplicate first line" : function() {
var doc = new Document(["1", "2", "3"]); var session = new EditSession(["1", "2", "3"]);
doc.duplicateLines(0, 0); session.duplicateLines(0, 0);
assert.equal(doc.toString(), ["1", "1", "2", "3"].join("\n")); assert.equal(session.getValue(), ["1", "1", "2", "3"].join("\n"));
},
"test: should handle unix style new lines" : function() {
var doc = new Document(["1", "2", "3"]);
assert.equal(doc.toString(), ["1", "2", "3"].join("\n"));
},
"test: should handle windows style new lines" : function() {
var doc = new Document(["1", "2", "3"].join("\r\n"));
doc.setNewLineMode("unix");
assert.equal(doc.toString(), ["1", "2", "3"].join("\n"));
},
"test: set new line mode to 'windows' should use '\r\n' as new lines": function() {
var doc = new Document(["1", "2", "3"].join("\n"));
doc.setNewLineMode("windows");
assert.equal(doc.toString(), ["1", "2", "3"].join("\r\n"));
},
"test: set new line mode to 'unix' should use '\n' as new lines": function() {
var doc = new Document(["1", "2", "3"].join("\r\n"));
doc.setNewLineMode("unix");
assert.equal(doc.toString(), ["1", "2", "3"].join("\n"));
},
"test: set new line mode to 'auto' should detect the incoming nl type": function() {
var doc = new Document(["1", "2", "3"].join("\n"));
doc.setNewLineMode("auto");
assert.equal(doc.toString(), ["1", "2", "3"].join("\n"));
var doc = new Document(["1", "2", "3"].join("\r\n"));
doc.setNewLineMode("auto");
assert.equal(doc.toString(), ["1", "2", "3"].join("\r\n"));
doc.replace(new Range(0, 0, 2, 1), ["4", "5", "6"].join("\n"));
assert.equal(["4", "5", "6"].join("\n"), doc.toString());
}, },
"test: convert document to screen coordinates" : function() { "test: convert document to screen coordinates" : function() {
var doc = new Document("01234\t567890\t1234"); var session = new EditSession("01234\t567890\t1234");
doc.setTabSize(4); session.setTabSize(4);
assert.equal(doc.documentToScreenColumn(0, 0), 0); assert.equal(session.documentToScreenColumn(0, 0), 0);
assert.equal(doc.documentToScreenColumn(0, 4), 4); assert.equal(session.documentToScreenColumn(0, 4), 4);
assert.equal(doc.documentToScreenColumn(0, 5), 5); assert.equal(session.documentToScreenColumn(0, 5), 5);
assert.equal(doc.documentToScreenColumn(0, 6), 9); assert.equal(session.documentToScreenColumn(0, 6), 9);
assert.equal(doc.documentToScreenColumn(0, 12), 15); assert.equal(session.documentToScreenColumn(0, 12), 15);
assert.equal(doc.documentToScreenColumn(0, 13), 19); assert.equal(session.documentToScreenColumn(0, 13), 19);
doc.setTabSize(2); session.setTabSize(2);
assert.equal(doc.documentToScreenColumn(0, 0), 0); assert.equal(session.documentToScreenColumn(0, 0), 0);
assert.equal(doc.documentToScreenColumn(0, 4), 4); assert.equal(session.documentToScreenColumn(0, 4), 4);
assert.equal(doc.documentToScreenColumn(0, 5), 5); assert.equal(session.documentToScreenColumn(0, 5), 5);
assert.equal(doc.documentToScreenColumn(0, 6), 7); assert.equal(session.documentToScreenColumn(0, 6), 7);
assert.equal(doc.documentToScreenColumn(0, 12), 13); assert.equal(session.documentToScreenColumn(0, 12), 13);
assert.equal(doc.documentToScreenColumn(0, 13), 15); assert.equal(session.documentToScreenColumn(0, 13), 15);
}, },
"test: convert document to scrren coordinates with leading tabs": function() { "test: convert document to scrren coordinates with leading tabs": function() {
var doc = new Document("\t\t123"); var session = new EditSession("\t\t123");
doc.setTabSize(4); session.setTabSize(4);
assert.equal(doc.documentToScreenColumn(0, 0), 0); assert.equal(session.documentToScreenColumn(0, 0), 0);
assert.equal(doc.documentToScreenColumn(0, 1), 4); assert.equal(session.documentToScreenColumn(0, 1), 4);
assert.equal(doc.documentToScreenColumn(0, 2), 8); assert.equal(session.documentToScreenColumn(0, 2), 8);
assert.equal(doc.documentToScreenColumn(0, 3), 9); assert.equal(session.documentToScreenColumn(0, 3), 9);
}, },
"test: convert screen to document coordinates" : function() { "test: convert screen to document coordinates" : function() {
var doc = new Document("01234\t567890\t1234"); var session = new EditSession("01234\t567890\t1234");
doc.setTabSize(4); session.setTabSize(4);
assert.equal(doc.screenToDocumentColumn(0, 0), 0); assert.equal(session.screenToDocumentColumn(0, 0), 0);
assert.equal(doc.screenToDocumentColumn(0, 4), 4); assert.equal(session.screenToDocumentColumn(0, 4), 4);
assert.equal(doc.screenToDocumentColumn(0, 5), 5); assert.equal(session.screenToDocumentColumn(0, 5), 5);
assert.equal(doc.screenToDocumentColumn(0, 6), 5); assert.equal(session.screenToDocumentColumn(0, 6), 5);
assert.equal(doc.screenToDocumentColumn(0, 7), 5); assert.equal(session.screenToDocumentColumn(0, 7), 5);
assert.equal(doc.screenToDocumentColumn(0, 8), 5); assert.equal(session.screenToDocumentColumn(0, 8), 5);
assert.equal(doc.screenToDocumentColumn(0, 9), 6); assert.equal(session.screenToDocumentColumn(0, 9), 6);
assert.equal(doc.screenToDocumentColumn(0, 15), 12); assert.equal(session.screenToDocumentColumn(0, 15), 12);
assert.equal(doc.screenToDocumentColumn(0, 19), 13); assert.equal(session.screenToDocumentColumn(0, 19), 13);
}, },
"test: insert text in multiple rows": function() { "test: insert text in multiple rows": function() {
var doc = new Document(["12", "", "abcd"]); var session = new EditSession(["12", "", "abcd"]);
var inserted = doc.multiRowInsert([0, 1, 2], 2, "juhu 1"); var inserted = session.multiRowInsert([0, 1, 2], 2, "juhu 1");
assert.equal(inserted.rows, 0); assert.equal(inserted.rows, 0);
assert.equal(inserted.columns, 6); assert.equal(inserted.columns, 6);
assert.equal(doc.toString(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n")); assert.equal(session.getValue(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n"));
}, },
"test: undo insert text in multiple rows": function() { "test: undo insert text in multiple rows": function() {
var doc = new Document(["12", "", "abcd"]); var session = new EditSession(["12", "", "abcd"]);
var undoManager = new UndoManager(); var undoManager = new UndoManager();
doc.setUndoManager(undoManager); session.setUndoManager(undoManager);
doc.multiRowInsert([0, 1, 2], 2, "juhu 1"); session.multiRowInsert([0, 1, 2], 2, "juhu 1");
doc.$informUndoManager.call(); session.$informUndoManager.call();
assert.equal(doc.toString(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n")); assert.equal(session.getValue(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n"));
undoManager.undo(); undoManager.undo();
assert.equal(doc.toString(), ["12", "", "abcd"].join("\n")); assert.equal(session.getValue(), ["12", "", "abcd"].join("\n"));
undoManager.redo(); undoManager.redo();
assert.equal(doc.toString(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n")); assert.equal(session.getValue(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n"));
}, },
"test: insert new line in multiple rows": function() { "test: insert new line in multiple rows": function() {
var doc = new Document(["12", "", "abcd"]); var session = new EditSession(["12", "", "abcd"]);
var inserted = doc.multiRowInsert([0, 1, 2], 2, "\n"); var inserted = session.multiRowInsert([0, 1, 2], 2, "\n");
assert.equal(inserted.rows, 1); assert.equal(inserted.rows, 1);
assert.equal(doc.toString(), ["12\n", " \n", "ab\ncd"].join("\n")); assert.equal(session.getValue(), ["12\n", " \n", "ab\ncd"].join("\n"));
}, },
"test: insert multi line text in multiple rows": function() { "test: insert multi line text in multiple rows": function() {
var doc = new Document(["12", "", "abcd"]); var session = new EditSession(["12", "", "abcd"]);
var inserted = doc.multiRowInsert([0, 1, 2], 2, "juhu\n12"); var inserted = session.multiRowInsert([0, 1, 2], 2, "juhu\n12");
assert.equal(inserted.rows, 1); assert.equal(inserted.rows, 1);
assert.equal(doc.toString(), ["12juhu\n12", " juhu\n12", "abjuhu\n12cd"].join("\n")); assert.equal(session.getValue(), ["12juhu\n12", " juhu\n12", "abjuhu\n12cd"].join("\n"));
}, },
"test: remove right in multiple rows" : function() { "test: remove right in multiple rows" : function() {
var doc = new Document(["12", "", "abcd"]); var session = new EditSession(["12", "", "abcd"]);
doc.multiRowRemove([0, 1, 2], new Range(0, 2, 0, 3)); session.multiRowRemove([0, 1, 2], new Range(0, 2, 0, 3));
assert.equal(doc.toString(), ["12", "", "abd"].join("\n")); assert.equal(session.getValue(), ["12", "", "abd"].join("\n"));
}, },
"test: undo remove right in multiple rows" : function() { "test: undo remove right in multiple rows" : function() {
var doc = new Document(["12", "", "abcd"]); var session = new EditSession(["12", "", "abcd"]);
var undoManager = new UndoManager(); var undoManager = new UndoManager();
doc.setUndoManager(undoManager); session.setUndoManager(undoManager);
doc.multiRowRemove([0, 1, 2], new Range(0, 1, 0, 3)); session.multiRowRemove([0, 1, 2], new Range(0, 1, 0, 3));
doc.$informUndoManager.call(); session.$informUndoManager.call();
assert.equal(doc.toString(), ["1", "", "ad"].join("\n")); assert.equal(session.getValue(), ["1", "", "ad"].join("\n"));
undoManager.undo(); undoManager.undo();
assert.equal(doc.toString(), ["12", "", "abcd"].join("\n")); assert.equal(session.getValue(), ["12", "", "abcd"].join("\n"));
undoManager.redo(); undoManager.redo();
assert.equal(doc.toString(), ["1", "", "ad"].join("\n")); assert.equal(session.getValue(), ["1", "", "ad"].join("\n"));
} }
}; };