Use events for the backgroundtokenizer

This commit is contained in:
Fabian Jakobs 2010-04-16 15:36:21 +02:00
commit 9fc3f77b4c
2 changed files with 23 additions and 15 deletions

View file

@ -1,17 +1,12 @@
ace.provide("ace.BackgroundTokenizer"); ace.provide("ace.BackgroundTokenizer");
ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) { ace.BackgroundTokenizer = function(tokenizer) {
this.running = false; this.running = false;
this.textLines = []; this.textLines = [];
this.lines = []; this.lines = [];
this.currentLine = 0; this.currentLine = 0;
this.tokenizer = tokenizer; this.tokenizer = tokenizer;
this.onUpdate = onUpdate || function(firstLine, lastLine) {
};
this.onComplete = onComplete || function() {
};
var self = this; var self = this;
this._worker = function() { this._worker = function() {
if (!self.running) { return; } if (!self.running) { return; }
@ -33,7 +28,7 @@ ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) {
// only check every 30 lines // only check every 30 lines
processedLines += 1; processedLines += 1;
if ((processedLines % 30 == 0) && (new Date() - workerStart) > 20) { if ((processedLines % 30 == 0) && (new Date() - workerStart) > 20) {
self.onUpdate(startLine, self.currentLine); self.fireUpdateEvent(startLine, self.currentLine);
return setTimeout(self._worker, 10); return setTimeout(self._worker, 10);
} }
@ -42,10 +37,12 @@ ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) {
self.running = false; self.running = false;
self.onUpdate(startLine, textLines.length - 1); self.fireUpdateEvent(startLine, textLines.length - 1);
self.onComplete();
}; };
this.$initEvents();
}; };
ace.mixin(ace.BackgroundTokenizer.prototype, ace.MEventEmitter);
ace.BackgroundTokenizer.prototype.setTokenizer = function(tokenizer) { ace.BackgroundTokenizer.prototype.setTokenizer = function(tokenizer) {
this.tokenizer = tokenizer; this.tokenizer = tokenizer;
@ -61,6 +58,14 @@ ace.BackgroundTokenizer.prototype.setLines = function(textLines) {
this.stop(); this.stop();
}; };
ace.BackgroundTokenizer.prototype.fireUpdateEvent = function(firstRow, lastRow) {
var data = {
first: firstRow,
last: lastRow
};
this.$dispatchEvent("update", {data: data});
};
ace.BackgroundTokenizer.prototype.start = function(startRow) { ace.BackgroundTokenizer.prototype.start = function(startRow) {
this.currentLine = Math.min(startRow || 0, this.currentLine, this.currentLine = Math.min(startRow || 0, this.currentLine,
this.textLines.length); this.textLines.length);

View file

@ -66,7 +66,8 @@ ace.Editor.prototype.setMode = function(mode) {
if (!this.bgTokenizer) { if (!this.bgTokenizer) {
var onUpdate = ace.bind(this.onTokenizerUpdate, this); var onUpdate = ace.bind(this.onTokenizerUpdate, this);
this.bgTokenizer = new ace.BackgroundTokenizer(tokenizer, onUpdate); this.bgTokenizer = new ace.BackgroundTokenizer(tokenizer);
this.bgTokenizer.addEventListener("update", onUpdate);
} else { } else {
this.bgTokenizer.setTokenizer(tokenizer); this.bgTokenizer.setTokenizer(tokenizer);
} }
@ -126,13 +127,15 @@ ace.Editor.prototype.onBlur = function() {
this.renderer.visualizeBlur(); this.renderer.visualizeBlur();
}; };
ace.Editor.prototype.onDocumentChange = function(startRow, endRow) { ace.Editor.prototype.onDocumentChange = function(e) {
this.bgTokenizer.start(startRow); var data = e.data;
this.renderer.updateLines(startRow, endRow); this.bgTokenizer.start(data.startRow);
this.renderer.updateLines(data.startRow, data.endRow);
}; };
ace.Editor.prototype.onTokenizerUpdate = function(startRow, endRow) { ace.Editor.prototype.onTokenizerUpdate = function(e) {
this.renderer.updateLines(startRow, endRow); var rows = e.data;
this.renderer.updateLines(rows.first, rows.last);
}; };
ace.Editor.prototype.onCursorChange = function() { ace.Editor.prototype.onCursorChange = function() {