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

View file

@ -355,10 +355,10 @@ module.exports = {
case "{":
case "[":
var cursor = editor.getCursorPosition();
var end = editor.session.$findClosingBracket(param, cursor, /paren/);
var end = editor.session.$findClosingBracket(param, cursor, "paren");
if (!end)
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)
return;
end.column ++;