Skip to content

Commit 3ffed28

Browse files
Moved brace matching code into services.ts
1 parent 1be4fe1 commit 3ffed28

2 files changed

Lines changed: 87 additions & 110 deletions

File tree

src/services/braceMatcher.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/services/services.ts

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
/// <reference path='syntax\incrementalParser.ts' />
88
/// <reference path='outliningElementsCollector.ts' />
99
/// <reference path='getScriptLexicalStructureWalker.ts' />
10-
/// <reference path='braceMatcher.ts' />
1110
/// <reference path='breakpoints.ts' />
1211
/// <reference path='indentation.ts' />
1312
/// <reference path='formatting\formatting.ts' />
@@ -2083,6 +2082,40 @@ module ts {
20832082
}
20842083
}
20852084

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+
20862119
function getContainerNode(node: Node): Node {
20872120
while (true) {
20882121
node = node.parent;
@@ -3726,7 +3759,59 @@ module ts {
37263759

37273760
function getBraceMatchingAtPosition(filename: string, position: number) {
37283761
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+
}
37303815
}
37313816

37323817
function getIndentationAtPosition(filename: string, position: number, editorOptions: EditorOptions) {
@@ -4244,40 +4329,6 @@ module ts {
42444329
};
42454330
}
42464331

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-
42814332
function initializeServices() {
42824333
objectAllocator = {
42834334
getNodeConstructor: kind => {

0 commit comments

Comments
 (0)