make bg tokenizer API sync again
This commit is contained in:
parent
ed9b0d4f0a
commit
36074ac3a4
3 changed files with 98 additions and 122 deletions
|
|
@ -125,12 +125,12 @@ var BackgroundTokenizer = function(tokenizer, editor) {
|
||||||
this.running = false;
|
this.running = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.getTokens = function(firstRow, lastRow, callback) {
|
this.getTokens = function(firstRow, lastRow) {
|
||||||
callback(this.$tokenizeRows(firstRow, lastRow));
|
return this.$tokenizeRows(firstRow, lastRow);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.getState = function(row, callback) {
|
this.getState = function(row) {
|
||||||
callback(this.$tokenizeRows(row, row)[0].state);
|
return this.$tokenizeRows(row, row)[0].state;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$tokenizeRows = function(firstRow, lastRow) {
|
this.$tokenizeRows = function(firstRow, lastRow) {
|
||||||
|
|
|
||||||
|
|
@ -422,31 +422,30 @@ var Editor =function(renderer, doc) {
|
||||||
|
|
||||||
this.clearSelection();
|
this.clearSelection();
|
||||||
|
|
||||||
var _self = this;
|
var lineState = this.bgTokenizer.getState(cursor.row);
|
||||||
this.bgTokenizer.getState(cursor.row, function (lineState) {
|
var shouldOutdent = this.mode.checkOutdent(lineState, this.doc.getLine(cursor.row), text);
|
||||||
var shouldOutdent = _self.mode.checkOutdent(lineState, _self.doc.getLine(cursor.row), text);
|
var line = this.doc.getLine(cursor.row);
|
||||||
var line = _self.doc.getLine(cursor.row),
|
var lineIndent = this.mode.getNextLineIndent(lineState, line.slice(0, cursor.column), this.doc.getTabString());
|
||||||
lineIndent = _self.mode.getNextLineIndent(lineState, line.slice(0, cursor.column), _self.doc.getTabString());
|
var end = this.doc.insert(cursor, text);
|
||||||
var end = _self.doc.insert(cursor, text);
|
|
||||||
|
|
||||||
/* TODO: This shortcut is somehow broken
|
/* TODO: This shortcut is somehow broken
|
||||||
if (!shouldOutdent && line != _self.doc.getLine(row) && text != "\n") {
|
if (!shouldOutdent && line != this.doc.getLine(row) && text != "\n") {
|
||||||
_self.moveCursorToPosition(end);
|
this.moveCursorToPosition(end);
|
||||||
_self.renderer.scrollCursorIntoView();
|
this.renderer.scrollCursorIntoView();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
_self.bgTokenizer.getState(cursor.row, function(lineState) {
|
var lineState = this.bgTokenizer.getState(cursor.row);
|
||||||
// multi line insert
|
// multi line insert
|
||||||
if (cursor.row !== end.row) {
|
if (cursor.row !== end.row) {
|
||||||
var size = _self.doc.getTabSize(),
|
var size = this.doc.getTabSize(),
|
||||||
minIndent = Number.MAX_VALUE;
|
minIndent = Number.MAX_VALUE;
|
||||||
|
|
||||||
for (var row = cursor.row + 1; row <= end.row; ++row) {
|
for (var row = cursor.row + 1; row <= end.row; ++row) {
|
||||||
var indent = 0;
|
var indent = 0;
|
||||||
|
|
||||||
line = _self.doc.getLine(row);
|
line = this.doc.getLine(row);
|
||||||
for (var i = 0; i < line.length; ++i)
|
for (var i = 0; i < line.length; ++i)
|
||||||
if (line.charAt(i) == '\t')
|
if (line.charAt(i) == '\t')
|
||||||
indent += size;
|
indent += size;
|
||||||
|
|
@ -461,25 +460,23 @@ var Editor =function(renderer, doc) {
|
||||||
for (var row = cursor.row + 1; row <= end.row; ++row) {
|
for (var row = cursor.row + 1; row <= end.row; ++row) {
|
||||||
var outdent = minIndent;
|
var outdent = minIndent;
|
||||||
|
|
||||||
line = _self.doc.getLine(row);
|
line = this.doc.getLine(row);
|
||||||
for (var i = 0; i < line.length && outdent > 0; ++i)
|
for (var i = 0; i < line.length && outdent > 0; ++i)
|
||||||
if (line.charAt(i) == '\t')
|
if (line.charAt(i) == '\t')
|
||||||
outdent -= size;
|
outdent -= size;
|
||||||
else if (line.charAt(i) == ' ')
|
else if (line.charAt(i) == ' ')
|
||||||
outdent -= 1;
|
outdent -= 1;
|
||||||
_self.doc.replace(new Range(row, 0, row, line.length), line.substr(i));
|
this.doc.replace(new Range(row, 0, row, line.length), line.substr(i));
|
||||||
}
|
}
|
||||||
end.column += _self.doc.indentRows(cursor.row + 1, end.row, lineIndent);
|
end.column += this.doc.indentRows(cursor.row + 1, end.row, lineIndent);
|
||||||
} else {
|
} else {
|
||||||
if (shouldOutdent) {
|
if (shouldOutdent) {
|
||||||
end.column += _self.mode.autoOutdent(lineState, _self.doc, cursor.row);
|
end.column += this.mode.autoOutdent(lineState, this.doc, cursor.row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_self.moveCursorToPosition(end);
|
this.moveCursorToPosition(end);
|
||||||
_self.renderer.scrollCursorIntoView();
|
this.renderer.scrollCursorIntoView();
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$overwrite = false;
|
this.$overwrite = false;
|
||||||
|
|
@ -640,12 +637,10 @@ var Editor =function(renderer, doc) {
|
||||||
if (this.$readOnly)
|
if (this.$readOnly)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var _self = this;
|
var state = this.bgTokenizer.getState(this.getCursorPosition().row);
|
||||||
this.bgTokenizer.getState(this.getCursorPosition().row, function(state) {
|
var rows = this.$getSelectedRows()
|
||||||
var rows = _self.$getSelectedRows()
|
var addedColumns = this.mode.toggleCommentLines(state, this.doc, rows.first, rows.last);
|
||||||
var addedColumns = _self.mode.toggleCommentLines(state, _self.doc, rows.first, rows.last);
|
this.selection.shiftSelection(addedColumns);
|
||||||
_self.selection.shiftSelection(addedColumns);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.removeLines = function() {
|
this.removeLines = function() {
|
||||||
|
|
|
||||||
|
|
@ -160,23 +160,19 @@ var Text = function(parentEl) {
|
||||||
var last = Math.min(lastRow, layerConfig.lastRow);
|
var last = Math.min(lastRow, layerConfig.lastRow);
|
||||||
|
|
||||||
var lineElements = this.element.childNodes;
|
var lineElements = this.element.childNodes;
|
||||||
var _self = this;
|
var tokens = this.tokenizer.getTokens(first, last);
|
||||||
this.tokenizer.getTokens(first, last, function(tokens) {
|
for (var i=first; i<=last; i++) {
|
||||||
for ( var i = first; i <= last; i++) {
|
|
||||||
var lineElement = lineElements[i - layerConfig.firstRow];
|
var lineElement = lineElements[i - layerConfig.firstRow];
|
||||||
if (!lineElement)
|
if (!lineElement)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var html = [];
|
var html = [];
|
||||||
_self.$renderLine(html, i, tokens[i-first].tokens);
|
this.$renderLine(html, i, tokens[i-first].tokens);
|
||||||
dom.setInnerHtml(lineElement, html.join(""));
|
dom.setInnerHtml(lineElement, html.join(""));
|
||||||
}
|
}
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.scrollLines = function(config) {
|
this.scrollLines = function(config) {
|
||||||
var _self = this;
|
|
||||||
|
|
||||||
this.$computeTabString();
|
this.$computeTabString();
|
||||||
var oldConfig = this.config;
|
var oldConfig = this.config;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
@ -197,50 +193,37 @@ var Text = function(parentEl) {
|
||||||
for (var row=config.lastRow+1; row<=oldConfig.lastRow; row++)
|
for (var row=config.lastRow+1; row<=oldConfig.lastRow; row++)
|
||||||
el.removeChild(el.lastChild);
|
el.removeChild(el.lastChild);
|
||||||
|
|
||||||
appendTop(appendBottom);
|
|
||||||
|
|
||||||
function appendTop(callback) {
|
|
||||||
if (config.firstRow < oldConfig.firstRow) {
|
if (config.firstRow < oldConfig.firstRow) {
|
||||||
_self.$renderLinesFragment(config, config.firstRow, oldConfig.firstRow - 1, function(fragment) {
|
var fragment = this.$renderLinesFragment(config, config.firstRow, oldConfig.firstRow - 1);
|
||||||
if (el.firstChild)
|
if (el.firstChild)
|
||||||
el.insertBefore(fragment, el.firstChild);
|
el.insertBefore(fragment, el.firstChild);
|
||||||
else
|
else
|
||||||
el.appendChild(fragment);
|
el.appendChild(fragment);
|
||||||
callback();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
callback();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendBottom() {
|
|
||||||
if (config.lastRow > oldConfig.lastRow) {
|
if (config.lastRow > oldConfig.lastRow) {
|
||||||
_self.$renderLinesFragment(config, oldConfig.lastRow + 1, config.lastRow, function(fragment) {
|
var fragment = this.$renderLinesFragment(config, oldConfig.lastRow + 1, config.lastRow);
|
||||||
el.appendChild(fragment);
|
el.appendChild(fragment);
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$renderLinesFragment = function(config, firstRow, lastRow, callback) {
|
this.$renderLinesFragment = function(config, firstRow, lastRow) {
|
||||||
var fragment = document.createDocumentFragment();
|
var fragment = document.createDocumentFragment();
|
||||||
var _self = this;
|
var tokens = this.tokenizer.getTokens(firstRow, lastRow);
|
||||||
this.tokenizer.getTokens(firstRow, lastRow, function(tokens) {
|
|
||||||
for (var row=firstRow; row<=lastRow; row++) {
|
for (var row=firstRow; row<=lastRow; row++) {
|
||||||
var lineEl = document.createElement("div");
|
var lineEl = document.createElement("div");
|
||||||
lineEl.className = "ace_line";
|
lineEl.className = "ace_line";
|
||||||
var style = lineEl.style;
|
var style = lineEl.style;
|
||||||
style.height = _self.$characterSize.height + "px";
|
style.height = this.$characterSize.height + "px";
|
||||||
style.width = config.width + "px";
|
style.width = config.width + "px";
|
||||||
|
|
||||||
var html = [];
|
var html = [];
|
||||||
_self.$renderLine(html, row, tokens[row-firstRow].tokens);
|
this.$renderLine(html, row, tokens[row-firstRow].tokens);
|
||||||
// don't use setInnerHtml since we are working with an empty DIV
|
// don't use setInnerHtml since we are working with an empty DIV
|
||||||
lineEl.innerHTML = html.join("");
|
lineEl.innerHTML = html.join("");
|
||||||
fragment.appendChild(lineEl);
|
fragment.appendChild(lineEl);
|
||||||
}
|
}
|
||||||
callback(fragment);
|
return fragment;
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.update = function(config) {
|
this.update = function(config) {
|
||||||
|
|
@ -248,16 +231,14 @@ var Text = function(parentEl) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
||||||
var html = [];
|
var html = [];
|
||||||
var _self = this;
|
var tokens = this.tokenizer.getTokens(config.firstRow, config.lastRow);
|
||||||
this.tokenizer.getTokens(config.firstRow, config.lastRow, function(tokens) {
|
for (var i=config.firstRow; i<=config.lastRow; i++) {
|
||||||
for ( var i = config.firstRow; i <= config.lastRow; i++) {
|
html.push("<div class='ace_line' style='height:" + this.$characterSize.height + "px;", "width:",
|
||||||
html.push("<div class='ace_line' style='height:" + _self.$characterSize.height + "px;", "width:",
|
|
||||||
config.width, "px'>");
|
config.width, "px'>");
|
||||||
_self.$renderLine(html, i, tokens[i-config.firstRow].tokens), html.push("</div>");
|
this.$renderLine(html, i, tokens[i-config.firstRow].tokens), html.push("</div>");
|
||||||
}
|
}
|
||||||
|
|
||||||
_self.element = dom.setInnerHtml(_self.element, html.join(""));
|
this.element = dom.setInnerHtml(this.element, html.join(""));
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$textToken = {
|
this.$textToken = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue