the document's lines array is no longer public

This commit is contained in:
Fabian Jakobs 2011-01-12 15:32:53 +01:00
commit ed9b0d4f0a
5 changed files with 45 additions and 44 deletions

View file

@ -42,7 +42,7 @@ var EventEmitter = require("pilot/event_emitter").EventEmitter;
var BackgroundTokenizer = function(tokenizer, editor) { var BackgroundTokenizer = function(tokenizer, editor) {
this.running = false; this.running = false;
this.textLines = []; this.doc = [];
this.lines = []; this.lines = [];
this.currentLine = 0; this.currentLine = 0;
this.tokenizer = tokenizer; this.tokenizer = tokenizer;
@ -54,12 +54,13 @@ var BackgroundTokenizer = function(tokenizer, editor) {
var workerStart = new Date(); var workerStart = new Date();
var startLine = self.currentLine; var startLine = self.currentLine;
var textLines = self.textLines; var doc = self.doc;
var processedLines = 0; var processedLines = 0;
var lastVisibleRow = editor.getLastVisibleRow(); var lastVisibleRow = editor.getLastVisibleRow();
while (self.currentLine < textLines.length) { var len = doc.getLength();
while (self.currentLine < len) {
self.lines[self.currentLine] = self.$tokenizeRows(self.currentLine, self.currentLine)[0]; self.lines[self.currentLine] = self.$tokenizeRows(self.currentLine, self.currentLine)[0];
self.currentLine++; self.currentLine++;
@ -76,7 +77,7 @@ var BackgroundTokenizer = function(tokenizer, editor) {
self.running = false; self.running = false;
self.fireUpdateEvent(startLine, textLines.length - 1); self.fireUpdateEvent(startLine, len - 1);
}; };
}; };
@ -91,8 +92,8 @@ var BackgroundTokenizer = function(tokenizer, editor) {
this.start(0); this.start(0);
}; };
this.setLines = function(textLines) { this.setDocument = function(doc) {
this.textLines = textLines; this.doc = doc;
this.lines = []; this.lines = [];
this.stop(); this.stop();
@ -108,7 +109,7 @@ var BackgroundTokenizer = function(tokenizer, editor) {
this.start = function(startRow) { this.start = function(startRow) {
this.currentLine = Math.min(startRow || 0, this.currentLine, this.currentLine = Math.min(startRow || 0, this.currentLine,
this.textLines.length); this.doc.getLength());
// remove all cached items below this line // remove all cached items below this line
this.lines.splice(this.currentLine, this.lines.length); this.lines.splice(this.currentLine, this.lines.length);
@ -143,9 +144,10 @@ var BackgroundTokenizer = function(tokenizer, editor) {
doCache = true; doCache = true;
} }
var lines = this.doc.getLines(firstRow, lastRow);
for (var row=firstRow; row<=lastRow; row++) { for (var row=firstRow; row<=lastRow; row++) {
if (!this.lines[row]) { if (!this.lines[row]) {
var tokens = this.tokenizer.getLineTokens(this.textLines[row] || "", state); var tokens = this.tokenizer.getLineTokens(lines[row-firstRow] || "", state);
var state = tokens.state; var state = tokens.state;
rows.push(tokens); rows.push(tokens);

View file

@ -47,7 +47,7 @@ var Range = require("ace/range").Range;
var Document = function(text, mode) { var Document = function(text, mode) {
this.modified = true; this.modified = true;
this.lines = []; this.$lines = [];
this.selection = new Selection(this); this.selection = new Selection(this);
this.$breakpoints = []; this.$breakpoints = [];
@ -81,15 +81,15 @@ var Document = function(text, mode) {
}; };
this.setValue = function(text) { this.setValue = function(text) {
var args = [0, this.lines.length]; var args = [0, this.$lines.length];
args.push.apply(args, this.$split(text)); args.push.apply(args, this.$split(text));
this.lines.splice.apply(this.lines, args); this.$lines.splice.apply(this.$lines, args);
this.modified = true; this.modified = true;
this.fireChangeEvent(0); this.fireChangeEvent(0);
}; };
this.toString = function() { this.toString = function() {
return this.lines.join(this.$getNewLineCharacter()); return this.$lines.join(this.$getNewLineCharacter());
}; };
this.getSelection = function() { this.getSelection = function() {
@ -306,7 +306,7 @@ var Document = function(text, mode) {
if (this.modified) { if (this.modified) {
this.modified = false; this.modified = false;
var lines = this.lines; var lines = this.$lines;
var longestLine = 0; var longestLine = 0;
var longestScreenLine = 0; var longestScreenLine = 0;
var tabSize = this.getTabSize(); var tabSize = this.getTabSize();
@ -330,7 +330,7 @@ var Document = 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.$lines[row] || "";
}; };
/** /**
@ -338,27 +338,27 @@ var Document = 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.$lines[row].replace(/\t/g, tab);
}; };
this.getLines = function(firstRow, lastRow) { this.getLines = function(firstRow, lastRow) {
return this.lines.slice(firstRow, lastRow+1); return this.$lines.slice(firstRow, lastRow+1);
}; };
this.getLength = function() { this.getLength = function() {
return this.lines.length; return this.$lines.length;
}; };
this.getTextRange = function(range) { this.getTextRange = function(range) {
if (range.start.row == range.end.row) { if (range.start.row == range.end.row) {
return this.lines[range.start.row].substring(range.start.column, return this.$lines[range.start.row].substring(range.start.column,
range.end.column); range.end.column);
} }
else { else {
var lines = []; var lines = [];
lines.push(this.lines[range.start.row].substring(range.start.column)); 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.apply(lines, this.getLines(range.start.row+1, range.end.row-1));
lines.push(this.lines[range.end.row].substring(0, range.end.column)); lines.push(this.$lines[range.end.row].substring(0, range.end.column));
return lines.join(this.$getNewLineCharacter()); return lines.join(this.$getNewLineCharacter());
} }
}; };
@ -466,7 +466,7 @@ var Document = function(text, mode) {
* @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; 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];
@ -507,7 +507,7 @@ var Document = function(text, mode) {
var args = [row, 0]; var args = [row, 0];
args.push.apply(args, lines); args.push.apply(args, lines);
this.lines.splice.apply(this.lines, args); this.$lines.splice.apply(this.$lines, args);
if (!fromUndo && this.$undoManager) { if (!fromUndo && this.$undoManager) {
var nl = this.$getNewLineCharacter(); var nl = this.$getNewLineCharacter();
@ -525,16 +525,16 @@ var Document = function(text, mode) {
return position; return position;
this.modified = true; this.modified = true;
if (this.lines.length <= 1) { if (this.$lines.length <= 1) {
this.$detectNewLine(text); this.$detectNewLine(text);
} }
var newLines = this.$split(text); var newLines = this.$split(text);
if (this.$isNewLine(text)) { if (this.$isNewLine(text)) {
var line = this.lines[position.row] || ""; var line = this.$lines[position.row] || "";
this.lines[position.row] = line.substring(0, position.column); this.$lines[position.row] = line.substring(0, position.column);
this.lines.splice(position.row + 1, 0, line.substring(position.column)); this.$lines.splice(position.row + 1, 0, line.substring(position.column));
var end = { var end = {
row : position.row + 1, row : position.row + 1,
@ -542,8 +542,8 @@ var Document = function(text, mode) {
}; };
} }
else if (newLines.length == 1) { else if (newLines.length == 1) {
var line = this.lines[position.row] || ""; var line = this.$lines[position.row] || "";
this.lines[position.row] = line.substring(0, position.column) + text this.$lines[position.row] = line.substring(0, position.column) + text
+ line.substring(position.column); + line.substring(position.column);
var end = { var end = {
@ -552,11 +552,11 @@ var Document = function(text, mode) {
}; };
} }
else { else {
var line = this.lines[position.row] || ""; var line = this.$lines[position.row] || "";
var firstLine = line.substring(0, position.column) + newLines[0]; var firstLine = line.substring(0, position.column) + newLines[0];
var lastLine = newLines[newLines.length - 1] + line.substring(position.column); var lastLine = newLines[newLines.length - 1] + line.substring(position.column);
this.lines[position.row] = firstLine; this.$lines[position.row] = firstLine;
this.$insertLines(position.row + 1, [lastLine], true); this.$insertLines(position.row + 1, [lastLine], true);
if (newLines.length > 2) { if (newLines.length > 2) {
@ -603,7 +603,7 @@ var Document = 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.$lines.length)
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), false);
@ -640,9 +640,9 @@ var Document = function(text, mode) {
+ this.getLine(lastRow).substring(range.end.column); + this.getLine(lastRow).substring(range.end.column);
if (row != "") if (row != "")
this.lines.splice(firstRow, lastRow - firstRow + 1, row); this.$lines.splice(firstRow, lastRow - firstRow + 1, row);
else else
this.lines.splice(firstRow, lastRow - firstRow + 1, ""); this.$lines.splice(firstRow, lastRow - firstRow + 1, "");
return range.start; return range.start;
}; };
@ -743,8 +743,8 @@ var Document = function(text, mode) {
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.$lines.slice(firstRow, lastRow + 1);
this.$remove(new Range(firstRow-1, this.lines[firstRow-1].length, lastRow, this.lines[lastRow].length)); this.$remove(new Range(firstRow-1, this.$lines[firstRow-1].length, lastRow, this.$lines[lastRow].length));
this.$insertLines(firstRow - 1, removed); this.$insertLines(firstRow - 1, removed);
this.fireChangeEvent(firstRow - 1, lastRow); this.fireChangeEvent(firstRow - 1, lastRow);
@ -752,9 +752,9 @@ var Document = function(text, mode) {
}; };
this.moveLinesDown = function(firstRow, lastRow) { this.moveLinesDown = function(firstRow, lastRow) {
if (lastRow >= this.lines.length-1) return 0; if (lastRow >= this.$lines.length-1) return 0;
var removed = this.lines.slice(firstRow, lastRow + 1); var removed = this.$lines.slice(firstRow, lastRow + 1);
this.$remove(new Range(firstRow, 0, lastRow + 1, 0)); this.$remove(new Range(firstRow, 0, lastRow + 1, 0));
this.$insertLines(firstRow+1, removed); this.$insertLines(firstRow+1, removed);
@ -776,7 +776,7 @@ var Document = function(text, mode) {
}; };
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.$lines.length-1));
}; };
this.documentToScreenColumn = function(row, docColumn) { this.documentToScreenColumn = function(row, docColumn) {

View file

@ -151,7 +151,7 @@ var Editor =function(renderer, doc) {
this.selection.addEventListener("changeSelection", this.$onSelectionChange); this.selection.addEventListener("changeSelection", this.$onSelectionChange);
this.onDocumentModeChange(); this.onDocumentModeChange();
this.bgTokenizer.setLines(this.doc.lines); this.bgTokenizer.setDocument(doc);
this.bgTokenizer.start(0); this.bgTokenizer.start(0);
this.onCursorChange(); this.onCursorChange();

View file

@ -70,7 +70,7 @@ MockRenderer.prototype.getMouseEventTarget = function() {
}; };
MockRenderer.prototype.setDocument = function(doc) { MockRenderer.prototype.setDocument = function(doc) {
this.lines = doc.lines; this.doc = doc;
}; };
MockRenderer.prototype.setTokenizer = function() { MockRenderer.prototype.setTokenizer = function() {
@ -91,7 +91,7 @@ MockRenderer.prototype.scrollCursorIntoView = function() {
}; };
MockRenderer.prototype.scrollToRow = function(row) { MockRenderer.prototype.scrollToRow = function(row) {
var row = Math.min(this.lines.length - this.visibleRowCount, Math.max(0, var row = Math.min(this.doc.getLength() - this.visibleRowCount, Math.max(0,
row)); row));
this.layerConfig.firstVisibleRow = row; this.layerConfig.firstVisibleRow = row;
this.layerConfig.lastVisibleRow = row + this.visibleRowCount; this.layerConfig.lastVisibleRow = row + this.visibleRowCount;

View file

@ -134,7 +134,6 @@ var VirtualRenderer = function(container, theme) {
oop.implement(this, EventEmitter); oop.implement(this, EventEmitter);
this.setDocument = function(doc) { this.setDocument = function(doc) {
this.lines = doc.lines;
this.doc = doc; this.doc = doc;
this.$cursorLayer.setDocument(doc); this.$cursorLayer.setDocument(doc);
this.$markerLayer.setDocument(doc); this.$markerLayer.setDocument(doc);
@ -396,7 +395,7 @@ var VirtualRenderer = function(container, theme) {
var lineCount = Math.ceil(minHeight / this.lineHeight); var lineCount = Math.ceil(minHeight / this.lineHeight);
var firstRow = Math.max(0, Math.round((this.scrollTop - offset) / this.lineHeight)); var firstRow = Math.max(0, Math.round((this.scrollTop - offset) / this.lineHeight));
var lastRow = Math.max(0, Math.min(this.lines.length, firstRow + lineCount) - 1); var lastRow = Math.max(0, Math.min(this.doc.getLength(), firstRow + lineCount) - 1);
var layerConfig = this.layerConfig = { var layerConfig = this.layerConfig = {
width : longestLine, width : longestLine,
@ -531,7 +530,7 @@ var VirtualRenderer = function(container, theme) {
}; };
this.scrollToY = function(scrollTop) { this.scrollToY = function(scrollTop) {
var maxHeight = this.lines.length * this.lineHeight - this.$size.scrollerHeight; var maxHeight = this.doc.getLength() * this.lineHeight - this.$size.scrollerHeight;
var scrollTop = Math.max(0, Math.min(maxHeight, scrollTop)); var scrollTop = Math.max(0, Math.min(maxHeight, scrollTop));
if (this.scrollTop !== scrollTop) { if (this.scrollTop !== scrollTop) {