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

View file

@ -66,7 +66,8 @@ ace.Editor.prototype.setMode = function(mode) {
if (!this.bgTokenizer) {
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 {
this.bgTokenizer.setTokenizer(tokenizer);
}
@ -126,13 +127,15 @@ ace.Editor.prototype.onBlur = function() {
this.renderer.visualizeBlur();
};
ace.Editor.prototype.onDocumentChange = function(startRow, endRow) {
this.bgTokenizer.start(startRow);
this.renderer.updateLines(startRow, endRow);
ace.Editor.prototype.onDocumentChange = function(e) {
var data = e.data;
this.bgTokenizer.start(data.startRow);
this.renderer.updateLines(data.startRow, data.endRow);
};
ace.Editor.prototype.onTokenizerUpdate = function(startRow, endRow) {
this.renderer.updateLines(startRow, endRow);
ace.Editor.prototype.onTokenizerUpdate = function(e) {
var rows = e.data;
this.renderer.updateLines(rows.first, rows.last);
};
ace.Editor.prototype.onCursorChange = function() {