diff --git a/lib/ace/edit_session/bracket_match.js b/lib/ace/edit_session/bracket_match.js index a7f403e3..878cff13 100644 --- a/lib/ace/edit_session/bracket_match.js +++ b/lib/ace/edit_session/bracket_match.js @@ -20,7 +20,7 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): - * Fabian Jakobs + * Fabian Jakobs * * 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 @@ -42,132 +42,132 @@ var TokenIterator = require("ace/token_iterator").TokenIterator; function BracketMatch() { - this.findMatchingBracket = function(position) { - if (position.column == 0) return null; + 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 charBeforeCursor = this.getLine(position.row).charAt(position.column-1); + if (charBeforeCursor == "") return null; - var match = charBeforeCursor.match(/([\(\[\{])|([\)\]\}])/); - if (!match) { - 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); - } - }; + if (match[1]) { + return this.$findClosingBracket(match[1], position); + } else { + return this.$findOpeningBracket(match[2], position); + } + }; - this.$brackets = { - ")": "(", - "(": ")", - "]": "[", - "[": "]", - "{": "}", - "}": "{" - }; + this.$brackets = { + ")": "(", + "(": ")", + "]": "[", + "[": "]", + "{": "}", + "}": "{" + }; - this.$findOpeningBracket = function(bracket, position) { - var openBracket = this.$brackets[bracket]; - var depth = 1; + 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; - } + 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)) + // 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; - }; + 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; + 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; + 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") + "])+"); + // 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(); + // Start searching in token, after after the character at position.column + var vIndex = position.column - iterator.getCurrentTokenColumn(); - while (true) { + 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; - } + 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)) + // 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; + if (token == null) + break; - vIndex = 0; - } - - return null; - }; + vIndex = 0; + } + + return null; + }; } exports.BracketMatch = BracketMatch; diff --git a/lib/ace/token_iterator.js b/lib/ace/token_iterator.js index 407266b6..2eee9386 100644 --- a/lib/ace/token_iterator.js +++ b/lib/ace/token_iterator.js @@ -20,7 +20,7 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): - * Fabian Jakobs + * Fabian Jakobs * * 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 @@ -39,73 +39,73 @@ define(function(require, exports, module) { var TokenIterator = function(session, initialRow, initialColumn) { - this.$session = session; - this.$row = initialRow; - this.$rowTokens = session.getTokens(initialRow, initialRow)[0].tokens; + this.$session = session; + this.$row = initialRow; + this.$rowTokens = session.getTokens(initialRow, initialRow)[0].tokens; - var token = session.getTokenAt(initialRow, initialColumn); - this.$tokenIndex = token ? token.index : -1; + var token = session.getTokenAt(initialRow, initialColumn); + this.$tokenIndex = token ? token.index : -1; }; (function() { - - this.stepBackward = function() { - this.$tokenIndex -= 1; - - while (this.$tokenIndex < 0) { - this.$row -= 1; - if (this.$row < 0) - return null; - - this.$rowTokens = this.$session.getTokens(this.$row, this.$row)[0].tokens; - this.$tokenIndex = this.$rowTokens.length - 1; - } - - return this.$rowTokens[this.$tokenIndex]; - } - - this.stepForward = function() { - var rowCount = this.$session.getLength(); - this.$tokenIndex += 1; - - while (this.$tokenIndex >= this.$rowTokens.length) { - this.$row += 1; - if (this.$row >= rowCount) - return null; + + this.stepBackward = function() { + this.$tokenIndex -= 1; + + while (this.$tokenIndex < 0) { + this.$row -= 1; + if (this.$row < 0) + return null; + + this.$rowTokens = this.$session.getTokens(this.$row, this.$row)[0].tokens; + this.$tokenIndex = this.$rowTokens.length - 1; + } + + return this.$rowTokens[this.$tokenIndex]; + } + + this.stepForward = function() { + var rowCount = this.$session.getLength(); + this.$tokenIndex += 1; + + while (this.$tokenIndex >= this.$rowTokens.length) { + this.$row += 1; + if (this.$row >= rowCount) + return null; - this.$rowTokens = this.$session.getTokens(this.$row, this.$row)[0].tokens; - this.$tokenIndex = 0; - } - - return this.$rowTokens[this.$tokenIndex]; - } - - this.getCurrentToken = function () { - return this.$rowTokens[this.$tokenIndex]; - } - - this.getCurrentTokenRow = function () { - return this.$row; - } - - this.getCurrentTokenColumn = function() { - var rowTokens = this.$rowTokens; - var tokenIndex = this.$tokenIndex; - - // If a column was cached by EditSession.getTokenAt, then use it - var column = rowTokens[tokenIndex].start; - if (column !== undefined) - return column; - - column = 0; - while (tokenIndex > 0) { - tokenIndex -= 1; - column += rowTokens[tokenIndex].value.length; - } - - return column; - } - + this.$rowTokens = this.$session.getTokens(this.$row, this.$row)[0].tokens; + this.$tokenIndex = 0; + } + + return this.$rowTokens[this.$tokenIndex]; + } + + this.getCurrentToken = function () { + return this.$rowTokens[this.$tokenIndex]; + } + + this.getCurrentTokenRow = function () { + return this.$row; + } + + this.getCurrentTokenColumn = function() { + var rowTokens = this.$rowTokens; + var tokenIndex = this.$tokenIndex; + + // If a column was cached by EditSession.getTokenAt, then use it + var column = rowTokens[tokenIndex].start; + if (column !== undefined) + return column; + + column = 0; + while (tokenIndex > 0) { + tokenIndex -= 1; + column += rowTokens[tokenIndex].value.length; + } + + return column; + } + }).call(TokenIterator.prototype); exports.TokenIterator = TokenIterator;