EditSession.findMatchingBracket now uses tokens instead of a simple text search. Moved the bracket-matching code to a separate file edit_session/bracket_match.js. Added a TokenIterator helper class, which walks forward or backward through all tokens in the document.

This commit is contained in:
dgeorge 2011-10-28 14:23:55 -07:00
commit e0048b4967
5 changed files with 300 additions and 94 deletions

View file

@ -0,0 +1,174 @@
/* vim:ts=4:sts=4:sw=4:
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Ajax.org Code Editor (ACE).
*
* The Initial Developer of the Original Code is
* Ajax.org B.V.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
var TokenIterator = require("ace/token_iterator").TokenIterator;
function BracketMatch() {
this.findMatchingBracket = function(position) {
if (position.column == 0) return null;
var charBeforeCursor = this.getLine(position.row).charAt(position.column-1);
if (charBeforeCursor == "") return null;
var match = charBeforeCursor.match(/([\(\[\{])|([\)\]\}])/);
if (!match) {
return null;
}
if (match[1]) {
return this.$findClosingBracket(match[1], position);
} else {
return this.$findOpeningBracket(match[2], position);
}
};
this.$brackets = {
")": "(",
"(": ")",
"]": "[",
"[": "]",
"{": "}",
"}": "{"
};
this.$findOpeningBracket = function(bracket, position) {
var openBracket = this.$brackets[bracket];
var depth = 1;
var iterator = new TokenIterator(this, position.row, position.column);
var token = iterator.getCurrentToken();
if (!token) return null;
// Create a pattern that matches any token with the same type as token.type.
// Exception: if token.type includes "rparen", then also match "lparen".
var typeRe = new RegExp("(\\.?[" +
token.type.replace(".", "|").replace("rparen", "lparen|rparen") + "])+");
// Start searching in token, just before the character at position.column
var vIndex = position.column - iterator.getCurrentTokenColumn() - 2;
var value = token.value;
while (true) {
while (vIndex >= 0) {
var char = value.charAt(vIndex);
if (char == openBracket) {
depth -= 1;
if (depth == 0) {
return {row: iterator.getCurrentTokenRow(),
column: vIndex + iterator.getCurrentTokenColumn()};
}
}
else if (char == bracket) {
depth += 1;
}
vIndex -= 1;
}
// Scan backward through the document, looking for the next token
// whose type matches typeRe
do {
token = iterator.stepBackward();
} while (token && !typeRe.test(token.type))
if (token == null)
break;
value = token.value;
vIndex = token.value.length - 1;
}
return null;
};
this.$findClosingBracket = function(bracket, position) {
var closingBracket = this.$brackets[bracket];
var depth = 1;
var iterator = new TokenIterator(this, position.row, position.column);
var token = iterator.getCurrentToken();
if (!token) return null;
// Create a pattern that matches any token with the same type as token.type.
// Exception: if token.type includes "lparen", then also match "rparen".
var typeRe = new RegExp("(\\.?[" +
token.type.replace(".", "|").replace("lparen", "lparen|rparen") + "])+");
// Start searching in token, after after the character at position.column
var vIndex = position.column - iterator.getCurrentTokenColumn();
while (true) {
var value = token.value;
var valueLength = value.length;
while (vIndex < valueLength) {
var char = value.charAt(vIndex);
if (char == closingBracket) {
depth -= 1;
if (depth == 0) {
return {row: iterator.getCurrentTokenRow(),
column: vIndex + iterator.getCurrentTokenColumn()};
}
}
else if (char == bracket) {
depth += 1;
}
vIndex += 1;
}
// Scan forward through the document, looking for the next token
// whose type matches typeRe
do {
token = iterator.stepForward();
} while (token && !typeRe.test(token.type))
if (token == null)
break;
vIndex = 0;
}
return null;
};
}
exports.BracketMatch = BracketMatch;
});