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 TextMode = require("ace/mode/text").Mode;
var Range = require("ace/range").Range;
var Document = require("ace/document").Document;
var NO_CHANGE_DELTAS = {};
var EditSession = function(text, mode) {
this.modified = true;
this.$lines = [];
this.selection = new Selection(this);
this.$breakpoints = [];
@ -58,10 +57,10 @@ var EditSession = function(text, mode) {
this.setMode(mode);
}
if (Array.isArray(text)) {
this.$insertLines(0, text);
if (text instanceof Document) {
this.setDocument(text)
} else {
this.$insert({row: 0, column: 0}, text);
this.setDocument(new Document(text));
}
};
@ -70,42 +69,35 @@ var EditSession = function(text, mode) {
oop.implement(this, EventEmitter);
this.$undoManager = null;
// check for IE split bug
if ("aaa".split(/a/).length == 0)
this.$split = function(text) {
return text.replace(/\r\n|\r/g, "\n").split("\n");
}
else
this.$split = function(text) {
return text.split(/\r\n|\r|\n/);
};
this.setValue = function(text) {
var args = [0, this.$lines.length];
args.push.apply(args, this.$split(text));
this.$lines.splice.apply(this.$lines, args);
this.modified = true;
this.fireChangeEvent(0);
};
this.toString = function() {
return this.$lines.join(this.$getNewLineCharacter());
this.setDocument = function(doc) {
if (this.doc)
throw new Error("Document is already set");
this.doc = doc;
doc.on("change", this.onChange.bind(this));
};
this.getValue = this.toString;
this.getSelection = function() {
return this.selection;
this.onChange = function(e) {
var delta = e.data;
this.$modified = true;
if (!this.$fromUndo && this.$undoManager) {
this.$deltas.push(delta);
this.$informUndoManager.schedule();
}
this._dispatchEvent("change", e);
};
this.fireChangeEvent = function(firstRow, lastRow) {
var data = {
firstRow: firstRow,
lastRow: lastRow
};
this._dispatchEvent("change", { data: data});
this.setValue = function(text) {
this.doc.setValue(text);
};
this.getValue =
this.toString = function() {
return this.doc.getValue();
};
this.getSelection = function() {
return this.selection;
};
this.setUndoManager = function(undoManager) {
@ -161,7 +153,7 @@ var EditSession = function(text, mode) {
this.setTabSize = function(tabSize) {
if (isNaN(tabSize) || this.$tabSize === tabSize) return;
this.modified = true;
this.$modified = true;
this.$tabSize = tabSize;
this._dispatchEvent("changeTabSize");
};
@ -244,29 +236,12 @@ var EditSession = function(text, mode) {
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) {
if (this.$newLineMode === newLineMode) return;
this.$newLineMode = newLineMode;
this.doc.setNewLineMode(newLineMode);
};
this.getNewLineMode = function() {
return this.$newLineMode;
return this.doc.getNewLineMode();
};
this.$mode = null;
@ -307,10 +282,10 @@ var EditSession = function(text, mode) {
};
this.$computeWidth = function() {
if (this.modified) {
this.modified = false;
if (this.$modified) {
this.$modified = false;
var lines = this.$lines;
var lines = this.doc.getAllLines();
var longestLine = 0;
var longestScreenLine = 0;
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
*/
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) {
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) {
return this.$lines.slice(firstRow, lastRow+1);
return this.doc.getLines(firstRow, lastRow);
};
this.getLength = function() {
return this.$lines.length;
return this.doc.getLength();
};
this.getTextRange = function(range) {
if (range.start.row == range.end.row) {
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());
}
return this.doc.getTextRange(range);
};
this.findMatchingBracket = function(position) {
@ -459,25 +424,20 @@ var EditSession = function(text, mode) {
return null;
};
this.insert = function(position, text, fromUndo) {
var end = this.$insert(position, text, fromUndo);
this.fireChangeEvent(position.row, position.row == end.row ? position.row
: undefined);
return end;
this.insert = function(position, text) {
return this.doc.insert(position, text);
};
/**
* @param rows Array[Integer] sorted list of rows
*/
this.multiRowInsert = function(rows, column, text) {
var lines = this.$lines;
this.multiRowInsert = function(rows, column, text) {
for (var i=rows.length-1; i>=0; i--) {
var row = rows[i];
if (row >= lines.length)
if (row >= this.doc.getLength())
continue;
var diff = column - lines[row].length;
var diff = column - this.doc.getLine(row).length;
if ( diff > 0) {
var padded = lang.stringRepeat(" ", diff) + text;
var offset = -diff;
@ -487,124 +447,17 @@ var EditSession = function(text, mode) {
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 {
rows: end.row - rows[0],
columns: end.column - column
}
}
else {
return {
rows: 0,
columns: 0
}
return {
rows: end ? end.row - rows[0] : 0,
columns: end ? end.column - column : 0
}
};
this.$insertLines = function(row, lines, fromUndo) {
if (lines.length == 0)
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.remove = function(range) {
return this.doc.remove(range);
};
this.multiRowRemove = function(rows, range) {
@ -614,53 +467,16 @@ var EditSession = function(text, mode) {
var height = range.end.row - rows[0];
for (var i=rows.length-1; i>=0; i--) {
var row = rows[i];
if (row >= this.$lines.length)
if (row >= this.doc.getLength())
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.selection.clearSelection();
this.$fromUndo = true;
for (var i=deltas.length-1; i>=0; i--) {
var delta = deltas[i];
if (delta.action == "insertText") {
@ -671,10 +487,13 @@ var EditSession = function(text, mode) {
this.selection.clearSelection();
}
}
this.$fromUndo = false;
},
this.redoChanges = function(deltas) {
this.selection.clearSelection();
this.$fromUndo = true;
for (var i=0; i<deltas.length; i++) {
var delta = deltas[i];
if (delta.action == "insertText") {
@ -685,41 +504,18 @@ var EditSession = function(text, mode) {
this.selection.moveCursorToPosition(delta.range.start);
}
}
this.$fromUndo = false;
},
this.replace = function(range, text) {
// Shortcut: Nothing to replace in this case.
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;
return this.doc.replace(range, text);
};
this.indentRows = function(startRow, endRow, indentString) {
indentString = indentString.replace("\t", this.getTabString());
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;
};
@ -747,31 +543,24 @@ var EditSession = function(text, mode) {
range.start.column -= deleteRange.end.column - deleteRange.start.column;
if (i == range.end.row)
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;
}
this.moveLinesUp = function(firstRow, lastRow) {
if (firstRow <= 0) return 0;
var removed = this.$lines.slice(firstRow, lastRow + 1);
this.$remove(new Range(firstRow-1, this.$lines[firstRow-1].length, lastRow, this.$lines[lastRow].length));
this.$insertLines(firstRow - 1, removed);
this.fireChangeEvent(firstRow - 1, lastRow);
var removed = this.doc.removeLines(firstRow, lastRow);
this.doc.insertLines(firstRow - 1, removed);
return -1;
};
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);
this.$remove(new Range(firstRow, 0, lastRow + 1, 0));
this.$insertLines(firstRow+1, removed);
this.fireChangeEvent(firstRow, lastRow + 1);
var removed = this.doc.removeLines(firstRow, lastRow);
this.doc.insertLines(firstRow+1, removed);
return 1;
};
@ -780,16 +569,14 @@ var EditSession = function(text, mode) {
var lastRow = this.$clipRowToDocument(lastRow);
var lines = this.getLines(firstRow, lastRow);
this.$insertLines(firstRow, lines);
this.doc.insertLines(firstRow, lines);
var addedRows = lastRow - firstRow + 1;
this.fireChangeEvent(firstRow);
return addedRows;
};
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) {

View file

@ -37,7 +37,7 @@
define(function(require, exports, module) {
var Document = require("../document").Document,
var EditSession = require("../edit_session").EditSession,
UndoManager = require("../undomanager").UndoManager,
MockRenderer = require("./mockrenderer"),
Range = require("../range").Range,
@ -47,242 +47,201 @@ var Document = require("../document").Document,
var Test = {
"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(doc.findMatchingBracket({row: 1, column: 2}), 1, 0);
assert.position(doc.findMatchingBracket({row: 1, column: 3}), 0, 3);
assert.position(doc.findMatchingBracket({row: 1, column: 4}), 0, 0);
assert.equal(doc.findMatchingBracket({row: 1, column: 5}), null);
assert.position(session.findMatchingBracket({row: 0, column: 3}), 0, 1);
assert.position(session.findMatchingBracket({row: 1, column: 2}), 1, 0);
assert.position(session.findMatchingBracket({row: 1, column: 3}), 0, 3);
assert.position(session.findMatchingBracket({row: 1, column: 4}), 0, 0);
assert.equal(session.findMatchingBracket({row: 1, column: 5}), null);
},
"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(doc.findMatchingBracket({row: 1, column: 1}), 1, 1);
assert.position(doc.findMatchingBracket({row: 0, column: 4}), 1, 2);
assert.position(doc.findMatchingBracket({row: 0, column: 2}), 0, 2);
assert.position(doc.findMatchingBracket({row: 0, column: 1}), 1, 3);
assert.equal(doc.findMatchingBracket({row: 0, column: 0}), null);
assert.position(session.findMatchingBracket({row: 1, column: 1}), 1, 1);
assert.position(session.findMatchingBracket({row: 1, column: 1}), 1, 1);
assert.position(session.findMatchingBracket({row: 0, column: 4}), 1, 2);
assert.position(session.findMatchingBracket({row: 0, column: 2}), 0, 2);
assert.position(session.findMatchingBracket({row: 0, column: 1}), 1, 3);
assert.equal(session.findMatchingBracket({row: 0, column: 0}), null);
},
"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(doc.findMatchingBracket({row: 0, column: 2}), 1, 2);
assert.position(doc.findMatchingBracket({row: 0, column: 3}), 1, 1);
assert.position(session.findMatchingBracket({row: 0, column: 1}), 1, 0);
assert.position(session.findMatchingBracket({row: 0, column: 2}), 1, 2);
assert.position(session.findMatchingBracket({row: 0, column: 3}), 1, 1);
assert.position(doc.findMatchingBracket({row: 1, column: 1}), 0, 0);
assert.position(doc.findMatchingBracket({row: 1, column: 2}), 0, 2);
assert.position(doc.findMatchingBracket({row: 1, column: 3}), 0, 1);
assert.position(session.findMatchingBracket({row: 1, column: 1}), 0, 0);
assert.position(session.findMatchingBracket({row: 1, column: 2}), 0, 2);
assert.position(session.findMatchingBracket({row: 1, column: 3}), 0, 1);
},
"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);
assert.equal(doc.toString(), ["a3", "a1", "a2", "a4"].join("\n"));
session.moveLinesDown(0, 1);
assert.equal(session.getValue(), ["a3", "a1", "a2", "a4"].join("\n"));
doc.moveLinesDown(1, 2);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n"));
session.moveLinesDown(1, 2);
assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesDown(2, 3);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n"));
session.moveLinesDown(2, 3);
assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesDown(2, 2);
assert.equal(doc.toString(), ["a3", "a4", "a2", "a1"].join("\n"));
session.moveLinesDown(2, 2);
assert.equal(session.getValue(), ["a3", "a4", "a2", "a1"].join("\n"));
},
"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);
assert.equal(doc.toString(), ["a1", "a3", "a4", "a2"].join("\n"));
session.moveLinesUp(2, 3);
assert.equal(session.getValue(), ["a1", "a3", "a4", "a2"].join("\n"));
doc.moveLinesUp(1, 2);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n"));
session.moveLinesUp(1, 2);
assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesUp(0, 1);
assert.equal(doc.toString(), ["a3", "a4", "a1", "a2"].join("\n"));
session.moveLinesUp(0, 1);
assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
doc.moveLinesUp(2, 2);
assert.equal(doc.toString(), ["a3", "a1", "a4", "a2"].join("\n"));
session.moveLinesUp(2, 2);
assert.equal(session.getValue(), ["a3", "a1", "a4", "a2"].join("\n"));
},
"test: duplicate lines" : function() {
var doc = new Document(["1", "2", "3", "4"]);
var session = new EditSession(["1", "2", "3", "4"]);
doc.duplicateLines(1, 2);
assert.equal(doc.toString(), ["1", "2", "3", "2", "3", "4"].join("\n"));
session.duplicateLines(1, 2);
assert.equal(session.getValue(), ["1", "2", "3", "2", "3", "4"].join("\n"));
},
"test: duplicate last line" : function() {
var doc = new Document(["1", "2", "3"]);
var session = new EditSession(["1", "2", "3"]);
doc.duplicateLines(2, 2);
assert.equal(doc.toString(), ["1", "2", "3", "3"].join("\n"));
session.duplicateLines(2, 2);
assert.equal(session.getValue(), ["1", "2", "3", "3"].join("\n"));
},
"test: duplicate first line" : function() {
var doc = new Document(["1", "2", "3"]);
var session = new EditSession(["1", "2", "3"]);
doc.duplicateLines(0, 0);
assert.equal(doc.toString(), ["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());
session.duplicateLines(0, 0);
assert.equal(session.getValue(), ["1", "1", "2", "3"].join("\n"));
},
"test: convert document to screen coordinates" : function() {
var doc = new Document("01234\t567890\t1234");
doc.setTabSize(4);
var session = new EditSession("01234\t567890\t1234");
session.setTabSize(4);
assert.equal(doc.documentToScreenColumn(0, 0), 0);
assert.equal(doc.documentToScreenColumn(0, 4), 4);
assert.equal(doc.documentToScreenColumn(0, 5), 5);
assert.equal(doc.documentToScreenColumn(0, 6), 9);
assert.equal(doc.documentToScreenColumn(0, 12), 15);
assert.equal(doc.documentToScreenColumn(0, 13), 19);
assert.equal(session.documentToScreenColumn(0, 0), 0);
assert.equal(session.documentToScreenColumn(0, 4), 4);
assert.equal(session.documentToScreenColumn(0, 5), 5);
assert.equal(session.documentToScreenColumn(0, 6), 9);
assert.equal(session.documentToScreenColumn(0, 12), 15);
assert.equal(session.documentToScreenColumn(0, 13), 19);
doc.setTabSize(2);
session.setTabSize(2);
assert.equal(doc.documentToScreenColumn(0, 0), 0);
assert.equal(doc.documentToScreenColumn(0, 4), 4);
assert.equal(doc.documentToScreenColumn(0, 5), 5);
assert.equal(doc.documentToScreenColumn(0, 6), 7);
assert.equal(doc.documentToScreenColumn(0, 12), 13);
assert.equal(doc.documentToScreenColumn(0, 13), 15);
assert.equal(session.documentToScreenColumn(0, 0), 0);
assert.equal(session.documentToScreenColumn(0, 4), 4);
assert.equal(session.documentToScreenColumn(0, 5), 5);
assert.equal(session.documentToScreenColumn(0, 6), 7);
assert.equal(session.documentToScreenColumn(0, 12), 13);
assert.equal(session.documentToScreenColumn(0, 13), 15);
},
"test: convert document to scrren coordinates with leading tabs": function() {
var doc = new Document("\t\t123");
doc.setTabSize(4);
var session = new EditSession("\t\t123");
session.setTabSize(4);
assert.equal(doc.documentToScreenColumn(0, 0), 0);
assert.equal(doc.documentToScreenColumn(0, 1), 4);
assert.equal(doc.documentToScreenColumn(0, 2), 8);
assert.equal(doc.documentToScreenColumn(0, 3), 9);
assert.equal(session.documentToScreenColumn(0, 0), 0);
assert.equal(session.documentToScreenColumn(0, 1), 4);
assert.equal(session.documentToScreenColumn(0, 2), 8);
assert.equal(session.documentToScreenColumn(0, 3), 9);
},
"test: convert screen to document coordinates" : function() {
var doc = new Document("01234\t567890\t1234");
doc.setTabSize(4);
var session = new EditSession("01234\t567890\t1234");
session.setTabSize(4);
assert.equal(doc.screenToDocumentColumn(0, 0), 0);
assert.equal(doc.screenToDocumentColumn(0, 4), 4);
assert.equal(doc.screenToDocumentColumn(0, 5), 5);
assert.equal(doc.screenToDocumentColumn(0, 6), 5);
assert.equal(doc.screenToDocumentColumn(0, 7), 5);
assert.equal(doc.screenToDocumentColumn(0, 8), 5);
assert.equal(doc.screenToDocumentColumn(0, 9), 6);
assert.equal(doc.screenToDocumentColumn(0, 15), 12);
assert.equal(doc.screenToDocumentColumn(0, 19), 13);
assert.equal(session.screenToDocumentColumn(0, 0), 0);
assert.equal(session.screenToDocumentColumn(0, 4), 4);
assert.equal(session.screenToDocumentColumn(0, 5), 5);
assert.equal(session.screenToDocumentColumn(0, 6), 5);
assert.equal(session.screenToDocumentColumn(0, 7), 5);
assert.equal(session.screenToDocumentColumn(0, 8), 5);
assert.equal(session.screenToDocumentColumn(0, 9), 6);
assert.equal(session.screenToDocumentColumn(0, 15), 12);
assert.equal(session.screenToDocumentColumn(0, 19), 13);
},
"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.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() {
var doc = new Document(["12", "", "abcd"]);
var session = new EditSession(["12", "", "abcd"]);
var undoManager = new UndoManager();
doc.setUndoManager(undoManager);
session.setUndoManager(undoManager);
doc.multiRowInsert([0, 1, 2], 2, "juhu 1");
doc.$informUndoManager.call();
assert.equal(doc.toString(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n"));
session.multiRowInsert([0, 1, 2], 2, "juhu 1");
session.$informUndoManager.call();
assert.equal(session.getValue(), ["12juhu 1", " juhu 1", "abjuhu 1cd"].join("\n"));
undoManager.undo();
assert.equal(doc.toString(), ["12", "", "abcd"].join("\n"));
assert.equal(session.getValue(), ["12", "", "abcd"].join("\n"));
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() {
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(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() {
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(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() {
var doc = new Document(["12", "", "abcd"]);
var session = new EditSession(["12", "", "abcd"]);
doc.multiRowRemove([0, 1, 2], new Range(0, 2, 0, 3));
assert.equal(doc.toString(), ["12", "", "abd"].join("\n"));
session.multiRowRemove([0, 1, 2], new Range(0, 2, 0, 3));
assert.equal(session.getValue(), ["12", "", "abd"].join("\n"));
},
"test: undo remove right in multiple rows" : function() {
var doc = new Document(["12", "", "abcd"]);
var session = new EditSession(["12", "", "abcd"]);
var undoManager = new UndoManager();
doc.setUndoManager(undoManager);
session.setUndoManager(undoManager);
doc.multiRowRemove([0, 1, 2], new Range(0, 1, 0, 3));
doc.$informUndoManager.call();
assert.equal(doc.toString(), ["1", "", "ad"].join("\n"));
session.multiRowRemove([0, 1, 2], new Range(0, 1, 0, 3));
session.$informUndoManager.call();
assert.equal(session.getValue(), ["1", "", "ad"].join("\n"));
undoManager.undo();
assert.equal(doc.toString(), ["12", "", "abcd"].join("\n"));
assert.equal(session.getValue(), ["12", "", "abcd"].join("\n"));
undoManager.redo();
assert.equal(doc.toString(), ["1", "", "ad"].join("\n"));
assert.equal(session.getValue(), ["1", "", "ad"].join("\n"));
}
};