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