From 3e8639596a8a3c74b9a364878819eb54d3754f41 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 15 Apr 2010 16:42:07 +0200 Subject: [PATCH] move tab support from editor to the document --- src/Editor.js | 47 ++++++++----------------------- src/TextDocument.js | 56 ++++++++++++++++++++----------------- src/TextLayer.js | 12 ++++++-- src/VirtualRenderer.js | 1 + test/TextEditTest.js | 8 +++--- test/mode/JavaScriptTest.js | 4 +-- 6 files changed, 58 insertions(+), 70 deletions(-) diff --git a/src/Editor.js b/src/Editor.js index 901bdf34..cb9deb7e 100644 --- a/src/Editor.js +++ b/src/Editor.js @@ -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); diff --git a/src/TextDocument.js b/src/TextDocument.js index 0f741463..5a5e408b 100644 --- a/src/TextDocument.js +++ b/src/TextDocument.js @@ -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; }; diff --git a/src/TextLayer.js b/src/TextLayer.js index d3cc4ed7..6af0eb58 100644 --- a/src/TextLayer.js +++ b/src/TextLayer.js @@ -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, "&").replace(/", output, diff --git a/src/VirtualRenderer.js b/src/VirtualRenderer.js index 001adbe5..bff44bc9 100644 --- a/src/VirtualRenderer.js +++ b/src/VirtualRenderer.js @@ -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) { diff --git a/test/TextEditTest.js b/test/TextEditTest.js index 32af630a..77f78751 100644 --- a/test/TextEditTest.js +++ b/test/TextEditTest.js @@ -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()); diff --git a/test/mode/JavaScriptTest.js b/test/mode/JavaScriptTest.js index 3e6183d4..3ce04472 100644 --- a/test/mode/JavaScriptTest.js +++ b/test/mode/JavaScriptTest.js @@ -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()); } }); \ No newline at end of file