Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Improve HTMLUtils.findBlocks performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Gerber committed Nov 18, 2014
1 parent e273f83 commit 3a1e205
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/language/HTMLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,19 @@ define(function (require, exports, module) {
*/
function findBlocks(editor, modeName) {
// Start scanning from beginning of file
var ctx = TokenUtils.getInitialContext(editor._codeMirror, {line: 0, ch: 0}),
blocks = [],
var blocks = [],
cm = editor._codeMirror,
currentBlock = null,
inBlock = false,
outerMode = editor._codeMirror.getMode(),
line,
lineCount = editor.lineCount(),
outerMode = cm.getMode(),
previousMode,
tokenModeName,
previousMode;
tokens;

while (TokenUtils.moveNextToken(ctx, false)) {
tokenModeName = CodeMirror.innerMode(outerMode, ctx.token.state).mode.name;
function checkForBlock(ctx) {
tokenModeName = CodeMirror.innerMode(outerMode, ctx.state).mode.name;
if (inBlock) {
if (!currentBlock.end) {
// Handle empty blocks
Expand All @@ -529,13 +532,13 @@ define(function (require, exports, module) {
currentBlock.text = editor.document.getRange(currentBlock.start, currentBlock.end);
inBlock = false;
} else {
currentBlock.end = { line: ctx.pos.line, ch: ctx.pos.ch };
currentBlock.end = { line: line, ch: ctx.end };
}
} else {
// Check for start of a block
if (tokenModeName === modeName) {
currentBlock = {
start: { line: ctx.pos.line, ch: ctx.pos.ch }
start: { line: line, ch: ctx.end }
};
blocks.push(currentBlock);
inBlock = true;
Expand All @@ -546,6 +549,12 @@ define(function (require, exports, module) {
}
}

for (line = 0; line < lineCount; line++) {
tokens = cm.getLineTokens(line);
tokens.forEach(checkForBlock);
tokens = null; // Garbage collection - tokens can be pretty big
}

return blocks;
}

Expand Down

0 comments on commit 3a1e205

Please sign in to comment.