|
7 | 7 | /// <reference path='syntax\incrementalParser.ts' /> |
8 | 8 | /// <reference path='outliningElementsCollector.ts' /> |
9 | 9 | /// <reference path='getScriptLexicalStructureWalker.ts' /> |
10 | | -/// <reference path='braceMatcher.ts' /> |
11 | 10 | /// <reference path='breakpoints.ts' /> |
12 | 11 | /// <reference path='indentation.ts' /> |
13 | 12 | /// <reference path='formatting\formatting.ts' /> |
@@ -2083,6 +2082,40 @@ module ts { |
2083 | 2082 | } |
2084 | 2083 | } |
2085 | 2084 |
|
| 2085 | + /** Get the token whose text contains the position, or the containing node. */ |
| 2086 | + function getNodeAtPosition(sourceFile: SourceFile, position: number) { |
| 2087 | + var current: Node = sourceFile; |
| 2088 | + outer: while (true) { |
| 2089 | + // find the child that has this |
| 2090 | + for (var i = 0, n = current.getChildCount(); i < n; i++) { |
| 2091 | + var child = current.getChildAt(i); |
| 2092 | + if (child.getStart() <= position && position < child.getEnd()) { |
| 2093 | + current = child; |
| 2094 | + continue outer; |
| 2095 | + } |
| 2096 | + } |
| 2097 | + return current; |
| 2098 | + } |
| 2099 | + } |
| 2100 | + |
| 2101 | + /** Get a token that contains the position. This is guaranteed to return a token, the position can be in the |
| 2102 | + * leading trivia or within the token text. |
| 2103 | + */ |
| 2104 | + function getTokenAtPosition(sourceFile: SourceFile, position: number) { |
| 2105 | + var current: Node = sourceFile; |
| 2106 | + outer: while (true) { |
| 2107 | + // find the child that has this |
| 2108 | + for (var i = 0, n = current.getChildCount(); i < n; i++) { |
| 2109 | + var child = current.getChildAt(i); |
| 2110 | + if (child.getFullStart() <= position && position < child.getEnd()) { |
| 2111 | + current = child; |
| 2112 | + continue outer; |
| 2113 | + } |
| 2114 | + } |
| 2115 | + return current; |
| 2116 | + } |
| 2117 | + } |
| 2118 | + |
2086 | 2119 | function getContainerNode(node: Node): Node { |
2087 | 2120 | while (true) { |
2088 | 2121 | node = node.parent; |
@@ -3726,7 +3759,59 @@ module ts { |
3726 | 3759 |
|
3727 | 3760 | function getBraceMatchingAtPosition(filename: string, position: number) { |
3728 | 3761 | var sourceFile = getCurrentSourceFile(filename); |
3729 | | - return BraceMatcher.getMatchSpans(sourceFile, position); |
| 3762 | + return getMatchSpans(sourceFile, position); |
| 3763 | + |
| 3764 | + function getMatchSpans(sourceFile: SourceFile, position: number): TypeScript.TextSpan[] { |
| 3765 | + var result: TypeScript.TextSpan[] = []; |
| 3766 | + |
| 3767 | + var token = getTokenAtPosition(sourceFile, position); |
| 3768 | + |
| 3769 | + if (token.getStart(sourceFile) === position) { |
| 3770 | + var matchKind = getMatchingTokenKind(token); |
| 3771 | + |
| 3772 | + // Ensure that there is a corresponding token to match ours. |
| 3773 | + if (matchKind) { |
| 3774 | + var parentElement = token.parent; |
| 3775 | + |
| 3776 | + var childNodes = parentElement.getChildren(sourceFile); |
| 3777 | + for (var i = 0, n = childNodes.length; i < n; i++) { |
| 3778 | + var current = childNodes[i]; |
| 3779 | + |
| 3780 | + if (current.kind === matchKind) { |
| 3781 | + var range1 = new TypeScript.TextSpan(token.getStart(sourceFile), token.getWidth(sourceFile)); |
| 3782 | + var range2 = new TypeScript.TextSpan(current.getStart(sourceFile), current.getWidth(sourceFile)); |
| 3783 | + |
| 3784 | + // We want to order the braces when we return the result. |
| 3785 | + if (range1.start() < range2.start()) { |
| 3786 | + result.push(range1, range2); |
| 3787 | + } |
| 3788 | + else { |
| 3789 | + result.push(range2, range1); |
| 3790 | + } |
| 3791 | + |
| 3792 | + break; |
| 3793 | + } |
| 3794 | + } |
| 3795 | + } |
| 3796 | + } |
| 3797 | + |
| 3798 | + return result; |
| 3799 | + } |
| 3800 | + |
| 3801 | + function getMatchingTokenKind(token: Node): ts.SyntaxKind { |
| 3802 | + switch (token.kind) { |
| 3803 | + case ts.SyntaxKind.OpenBraceToken: return ts.SyntaxKind.CloseBraceToken |
| 3804 | + case ts.SyntaxKind.OpenParenToken: return ts.SyntaxKind.CloseParenToken; |
| 3805 | + case ts.SyntaxKind.OpenBracketToken: return ts.SyntaxKind.CloseBracketToken; |
| 3806 | + case ts.SyntaxKind.LessThanToken: return ts.SyntaxKind.GreaterThanToken; |
| 3807 | + case ts.SyntaxKind.CloseBraceToken: return ts.SyntaxKind.OpenBraceToken |
| 3808 | + case ts.SyntaxKind.CloseParenToken: return ts.SyntaxKind.OpenParenToken; |
| 3809 | + case ts.SyntaxKind.CloseBracketToken: return ts.SyntaxKind.OpenBracketToken; |
| 3810 | + case ts.SyntaxKind.GreaterThanToken: return ts.SyntaxKind.LessThanToken; |
| 3811 | + } |
| 3812 | + |
| 3813 | + return undefined; |
| 3814 | + } |
3730 | 3815 | } |
3731 | 3816 |
|
3732 | 3817 | function getIndentationAtPosition(filename: string, position: number, editorOptions: EditorOptions) { |
@@ -4244,40 +4329,6 @@ module ts { |
4244 | 4329 | }; |
4245 | 4330 | } |
4246 | 4331 |
|
4247 | | - /** Get the token whose text contains the position, or the containing node. */ |
4248 | | - export function getNodeAtPosition(sourceFile: SourceFile, position: number) { |
4249 | | - var current: Node = sourceFile; |
4250 | | - outer: while (true) { |
4251 | | - // find the child that has this |
4252 | | - for (var i = 0, n = current.getChildCount(); i < n; i++) { |
4253 | | - var child = current.getChildAt(i); |
4254 | | - if (child.getStart() <= position && position < child.getEnd()) { |
4255 | | - current = child; |
4256 | | - continue outer; |
4257 | | - } |
4258 | | - } |
4259 | | - return current; |
4260 | | - } |
4261 | | - } |
4262 | | - |
4263 | | - /** Get a token that contains the position. This is guaranteed to return a token, the position can be in the |
4264 | | - * leading trivia or within the token text. |
4265 | | - */ |
4266 | | - export function getTokenAtPosition(sourceFile: SourceFile, position: number) { |
4267 | | - var current: Node = sourceFile; |
4268 | | - outer: while (true) { |
4269 | | - // find the child that has this |
4270 | | - for (var i = 0, n = current.getChildCount(); i < n; i++) { |
4271 | | - var child = current.getChildAt(i); |
4272 | | - if (child.getFullStart() <= position && position < child.getEnd()) { |
4273 | | - current = child; |
4274 | | - continue outer; |
4275 | | - } |
4276 | | - } |
4277 | | - return current; |
4278 | | - } |
4279 | | - } |
4280 | | - |
4281 | 4332 | function initializeServices() { |
4282 | 4333 | objectAllocator = { |
4283 | 4334 | getNodeConstructor: kind => { |
|
0 commit comments