make findMatchingBracket faster

This commit is contained in:
nightwing 2013-03-14 16:16:45 +04:00
commit 04a308e00d
2 changed files with 11 additions and 23 deletions

View file

@ -102,7 +102,7 @@ function BracketMatch() {
"}": "{" "}": "{"
}; };
this.$findOpeningBracket = function(bracket, position, typeRe) { this.$findOpeningBracket = function(bracket, position, type) {
var openBracket = this.$brackets[bracket]; var openBracket = this.$brackets[bracket];
var depth = 1; var depth = 1;
@ -113,20 +113,14 @@ function BracketMatch() {
if (!token) if (!token)
return; return;
if (!typeRe){ if (!type)
typeRe = new RegExp( type = token.type.replace(/\.(?:rparen|lparen|start|end)$/, "");
"(\\.?" +
token.type.replace(".", "\\.").replace("rparen", ".paren")
+ ")+"
);
}
// Start searching in token, just before the character at position.column // Start searching in token, just before the character at position.column
var valueIndex = position.column - iterator.getCurrentTokenColumn() - 2; var valueIndex = position.column - iterator.getCurrentTokenColumn() - 2;
var value = token.value; var value = token.value;
while (true) { while (true) {
while (valueIndex >= 0) { while (valueIndex >= 0) {
var chr = value.charAt(valueIndex); var chr = value.charAt(valueIndex);
if (chr == openBracket) { if (chr == openBracket) {
@ -146,7 +140,7 @@ function BracketMatch() {
// whose type matches typeRe // whose type matches typeRe
do { do {
token = iterator.stepBackward(); token = iterator.stepBackward();
} while (token && !typeRe.test(token.type)); } while (token && token.type.lastIndexOf(type, 0));
if (token == null) if (token == null)
break; break;
@ -158,7 +152,7 @@ function BracketMatch() {
return null; return null;
}; };
this.$findClosingBracket = function(bracket, position, typeRe) { this.$findClosingBracket = function(bracket, position, type) {
var closingBracket = this.$brackets[bracket]; var closingBracket = this.$brackets[bracket];
var depth = 1; var depth = 1;
@ -169,19 +163,13 @@ function BracketMatch() {
if (!token) if (!token)
return; return;
if (!typeRe){ if (!type)
typeRe = new RegExp( type = token.type.replace(/\.(?:rparen|lparen|start|end)$/, "");
"(\\.?" +
token.type.replace(".", "\\.").replace("lparen", ".paren")
+ ")+"
);
}
// Start searching in token, after the character at position.column // Start searching in token, after the character at position.column
var valueIndex = position.column - iterator.getCurrentTokenColumn(); var valueIndex = position.column - iterator.getCurrentTokenColumn();
while (true) { while (true) {
var value = token.value; var value = token.value;
var valueLength = value.length; var valueLength = value.length;
while (valueIndex < valueLength) { while (valueIndex < valueLength) {
@ -203,7 +191,7 @@ function BracketMatch() {
// whose type matches typeRe // whose type matches typeRe
do { do {
token = iterator.stepForward(); token = iterator.stepForward();
} while (token && !typeRe.test(token.type)); } while (token && token.type.lastIndexOf(type, 0));
if (token == null) if (token == null)
break; break;

View file

@ -355,10 +355,10 @@ module.exports = {
case "{": case "{":
case "[": case "[":
var cursor = editor.getCursorPosition(); var cursor = editor.getCursorPosition();
var end = editor.session.$findClosingBracket(param, cursor, /paren/); var end = editor.session.$findClosingBracket(param, cursor, "paren");
if (!end) if (!end)
return; return;
var start = editor.session.$findOpeningBracket(editor.session.$brackets[param], cursor, /paren/); var start = editor.session.$findOpeningBracket(editor.session.$brackets[param], cursor, "paren");
if (!start) if (!start)
return; return;
end.column ++; end.column ++;