move tab support from editor to the document

This commit is contained in:
Fabian Jakobs 2010-04-15 16:42:07 +02:00
commit 3e8639596a
6 changed files with 58 additions and 70 deletions

View file

@ -34,6 +34,11 @@ ace.Editor.prototype.setDocument = function(doc) {
doc.addEventListener("change", ace.bind(this.onDocumentChange, this));
this.renderer.setDocument(doc);
var self = this;
doc.addEventListener("changeTabSize", function() {
self.renderer.draw();
});
this.selection = doc.getSelection();
var onCursorChange = ace.bind(this.onCursorChange, this);
@ -42,6 +47,7 @@ ace.Editor.prototype.setDocument = function(doc) {
var onSelectionChange = ace.bind(this.onSelectionChange, this);
this.selection.addEventListener("changeSelection", onSelectionChange);
this.bgTokenizer.setLines(this.doc.lines);
};
@ -216,8 +222,8 @@ ace.Editor.prototype.onCut = function() {
ace.Editor.prototype.onTextInput = function(text) {
var cursor = this.getCursorPosition();
if (this.getUseSoftTabs()) {
text = text.replace(/\t/g, this.getTabString());
if (this.doc.getUseSoftTabs()) {
text = text.replace(/\t/g, this.doc.getTabString());
}
if (!this.selection.isEmpty()) {
@ -233,7 +239,7 @@ ace.Editor.prototype.onTextInput = function(text) {
if (row !== end.row) {
var line = this.doc.getLine(row);
var lineState = this.bgTokenizer.getState(row);
var indent = this.mode.getNextLineIndent(line, lineState, this.getTabString());
var indent = this.mode.getNextLineIndent(line, lineState, this.doc.getTabString());
if (indent) {
var indentRange = {
start: {
@ -250,37 +256,6 @@ ace.Editor.prototype.onTextInput = function(text) {
this.renderer.scrollCursorIntoView();
};
ace.Editor.prototype.getTabString = function() {
if (this.getUseSoftTabs()) {
return new Array(this.getTabSize()+1).join(" ");
}
return "\t";
};
ace.Editor.prototype._useSoftTabs = true;
ace.Editor.prototype.setUseSoftTabs = function(useSoftTabs) {
if (this._useSoftTabs === useSoftTabs) return;
this._useSoftTabs = useSoftTabs;
};
ace.Editor.prototype.getUseSoftTabs = function() {
return this._useSoftTabs;
};
ace.Editor.prototype._tabSize = 4;
ace.Editor.prototype.setTabSize = function(tabSize) {
if (this._tabSize === tabSize) return;
this._tabSize = tabSize;
this.renderer.draw();
};
ace.Editor.prototype.getTabSize = function() {
return this._tabSize;
};
ace.Editor.prototype.removeRight = function() {
if (this.selection.isEmpty()) {
this.selection.selectRight();
@ -309,14 +284,14 @@ ace.Editor.prototype.removeLine = function() {
};
ace.Editor.prototype.blockIndent = function(indentString) {
var indentString = indentString || this.getTabString();
var indentString = indentString || this.doc.getTabString();
var addedColumns = this.doc.indentRows(this.getSelectionRange(), indentString);
this.selection.shiftSelection(addedColumns);
};
ace.Editor.prototype.blockOutdent = function(indentString) {
var indentString = indentString || this.getTabString();
var indentString = indentString || this.doc.getTabString();
var addedColumns = this.doc.outdentRows(this.getSelectionRange(), indentString);
this.selection.shiftSelection(addedColumns);

View file

@ -32,6 +32,36 @@ ace.TextDocument.prototype.fireChangeEvent = function(firstRow, lastRow) {
this.$dispatchEvent("change", { data: data});
};
ace.TextDocument.prototype.getTabString = function() {
if (this.getUseSoftTabs()) {
return new Array(this.getTabSize()+1).join(" ");
}
return "\t";
};
ace.TextDocument.prototype._useSoftTabs = true;
ace.TextDocument.prototype.setUseSoftTabs = function(useSoftTabs) {
if (this._useSoftTabs === useSoftTabs) return;
this._useSoftTabs = useSoftTabs;
};
ace.TextDocument.prototype.getUseSoftTabs = function() {
return this._useSoftTabs;
};
ace.TextDocument.prototype._tabSize = 4;
ace.TextDocument.prototype.setTabSize = function(tabSize) {
if (this._tabSize === tabSize) return;
this._tabSize = tabSize;
this.$dispatchEvent("changeTabSize");
};
ace.TextDocument.prototype.getTabSize = function() {
return this._tabSize;
};
ace.TextDocument.prototype.getWidth = function() {
if (this.modified) {
this.modified = false;
@ -50,32 +80,6 @@ ace.TextDocument.prototype.getLine = function(row) {
return this.lines[row] || "";
};
ace.TextDocument.prototype.keywords = {
"break" : 1,
"case" : 1,
"catch" : 1,
"continue" : 1,
"default" : 1,
"delete" : 1,
"do" : 1,
"else" : 1,
"finally" : 1,
"for" : 1,
"function" : 1,
"if" : 1,
"in" : 1,
"instanceof" : 1,
"new" : 1,
"return" : 1,
"switch" : 1,
"throw" : 1,
"try" : 1,
"typeof" : 1,
"var" : 1,
"while" : 1,
"with" : 1
};
ace.TextDocument.prototype.getLength = function() {
return this.lines.length;
};

View file

@ -6,6 +6,7 @@ ace.TextLayer = function(parentEl) {
parentEl.appendChild(this.element);
this._measureSizes();
this._tabString = " ";
};
ace.TextLayer.prototype.setTokenizer = function(tokenizer) {
@ -41,6 +42,10 @@ ace.TextLayer.prototype._measureSizes = function() {
this.element.removeChild(measureNode);
};
ace.TextLayer.prototype.setTabSize = function(tabSize) {
this._tabString = new Array(tabSize+1).join(" ");
};
ace.TextLayer.prototype.updateLines = function(layerConfig, firstRow, lastRow) {
var first = Math.max(firstRow, layerConfig.firstRow);
var last = Math.min(lastRow, layerConfig.lastRow);
@ -73,8 +78,11 @@ ace.TextLayer.prototype.renderLine = function(stringBuilder, row) {
for ( var i = 0; i < tokens.length; i++) {
var token = tokens[i];
var output = token.value.replace(/&/g, "&amp;").replace(/</g, "&lt;")
.replace(/\s/g, "&nbsp;");
var output = token.value
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/\t/g, this._tabString)
.replace(/\s/g, "&nbsp;");
if (token.type !== "text") {
stringBuilder.push("<span class='", token.type, "'>", output,

View file

@ -37,6 +37,7 @@ ace.VirtualRenderer.prototype.setDocument = function(doc) {
this.lines = doc.lines;
this.doc = doc;
this.markerLayer.setDocument(doc);
this.textLayer.setTabSize(doc.getTabSize());
};
ace.VirtualRenderer.prototype.setTokenizer = function(tokenizer) {

View file

@ -194,13 +194,13 @@ var TextEditTest = TestCase("TextEditTest",
var doc = new ace.TextDocument("");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.setTabSize(2);
editor.setUseSoftTabs(true);
doc.setTabSize(2);
doc.setUseSoftTabs(true);
editor.onTextInput("\t");
assertEquals(" ", doc.toString());
editor.setTabSize(5);
doc.setTabSize(5);
editor.onTextInput("\t");
assertEquals(" ", doc.toString());
},
@ -209,7 +209,7 @@ var TextEditTest = TestCase("TextEditTest",
var doc = new ace.TextDocument("");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.setUseSoftTabs(false);
doc.setUseSoftTabs(false);
editor.onTextInput("\t");
assertEquals("\t", doc.toString());

View file

@ -56,7 +56,7 @@ var JavaScriptTest = new TestCase("mode.JavaScriptTest", {
editor.navigateLineEnd();
editor.onTextInput("\n");
assertEquals(["if () {", editor.getTabString()].join("\n"), doc.toString());
assertEquals(["if () {", doc.getTabString()].join("\n"), doc.toString());
},
"test: no auto indent after opening brace in multi line comment" : function() {
@ -76,6 +76,6 @@ var JavaScriptTest = new TestCase("mode.JavaScriptTest", {
editor.navigateLineEnd();
editor.onTextInput("\n");
assertEquals([" if () {", " " + editor.getTabString()].join("\n"), doc.toString());
assertEquals([" if () {", " " + doc.getTabString()].join("\n"), doc.toString());
}
});