Skip to content

Commit 2532738

Browse files
committed
initial change
1 parent fce3bdd commit 2532738

156 files changed

Lines changed: 50722 additions & 54 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/services/braceMatcher.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
///<reference path='references.ts' />
17+
18+
module TypeScript.Services {
19+
export class BraceMatcher {
20+
21+
// Given a script name and position in the script, return a pair of text range if the
22+
// position corresponds to a "brace matchin" characters (e.g. "{" or "(", etc.)
23+
// If the position is not on any range, return an empty set.
24+
public static getMatchSpans(syntaxTree: TypeScript.SyntaxTree, position: number): TypeScript.TextSpan[] {
25+
var result: TypeScript.TextSpan[] = [];
26+
27+
var currentToken = findToken(syntaxTree.sourceUnit(), position);
28+
29+
BraceMatcher.getMatchingCloseBrace(currentToken, position, result);
30+
BraceMatcher.getMatchingOpenBrace(currentToken, position, result);
31+
32+
return result;
33+
}
34+
35+
private static getMatchingCloseBrace(currentToken: TypeScript.ISyntaxToken, position: number, result: TypeScript.TextSpan[]) {
36+
if (start(currentToken) === position) {
37+
var closingBraceKind = BraceMatcher.getMatchingCloseBraceTokenKind(currentToken);
38+
if (closingBraceKind !== null) {
39+
var parentElement = currentToken.parent
40+
var currentPosition = fullStart(currentToken.parent);
41+
for (var i = 0, n = childCount(parentElement); i < n; i++) {
42+
var element = childAt(parentElement, i);
43+
if (element !== null && fullWidth(element) > 0) {
44+
if (element.kind() === closingBraceKind) {
45+
var range1 = new TypeScript.TextSpan(position, width(currentToken));
46+
var range2 = new TypeScript.TextSpan(currentPosition + leadingTriviaWidth(element), width(element));
47+
result.push(range1, range2);
48+
break;
49+
}
50+
51+
currentPosition += fullWidth(element);
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
private static getMatchingOpenBrace(currentToken: TypeScript.ISyntaxToken, position: number, result: TypeScript.TextSpan[]) {
59+
// Check if the current token to the left is a close brace
60+
if (currentToken.fullStart() === position) {
61+
currentToken = previousToken(currentToken);
62+
}
63+
64+
if (currentToken !== null && start(currentToken) === (position - 1)) {
65+
var openBraceKind = BraceMatcher.getMatchingOpenBraceTokenKind(currentToken);
66+
if (openBraceKind !== null) {
67+
var parentElement = currentToken.parent;
68+
var currentPosition = fullStart(currentToken.parent) + fullWidth(parentElement);
69+
for (var i = childCount(parentElement) - 1 ; i >= 0; i--) {
70+
var element = childAt(parentElement, i);
71+
if (element !== null && fullWidth(element) > 0) {
72+
if (element.kind() === openBraceKind) {
73+
var range1 = new TypeScript.TextSpan(position - 1, width(currentToken));
74+
var range2 = new TypeScript.TextSpan(currentPosition - lastToken(element).trailingTriviaWidth() - width(element), width(element));
75+
result.push(range1, range2);
76+
break;
77+
}
78+
79+
currentPosition -= fullWidth(element);
80+
}
81+
}
82+
}
83+
}
84+
}
85+
86+
private static getMatchingCloseBraceTokenKind(positionedElement: TypeScript.ISyntaxElement): TypeScript.SyntaxKind {
87+
var element = positionedElement !== null && positionedElement;
88+
switch (element.kind()) {
89+
case TypeScript.SyntaxKind.OpenBraceToken:
90+
return TypeScript.SyntaxKind.CloseBraceToken
91+
case TypeScript.SyntaxKind.OpenParenToken:
92+
return TypeScript.SyntaxKind.CloseParenToken;
93+
case TypeScript.SyntaxKind.OpenBracketToken:
94+
return TypeScript.SyntaxKind.CloseBracketToken;
95+
case TypeScript.SyntaxKind.LessThanToken:
96+
return TypeScript.SyntaxUtilities.isAngleBracket(positionedElement) ? TypeScript.SyntaxKind.GreaterThanToken : null;
97+
}
98+
return null;
99+
}
100+
101+
private static getMatchingOpenBraceTokenKind(positionedElement: TypeScript.ISyntaxElement): TypeScript.SyntaxKind {
102+
var element = positionedElement !== null && positionedElement;
103+
switch (element.kind()) {
104+
case TypeScript.SyntaxKind.CloseBraceToken:
105+
return TypeScript.SyntaxKind.OpenBraceToken
106+
case TypeScript.SyntaxKind.CloseParenToken:
107+
return TypeScript.SyntaxKind.OpenParenToken;
108+
case TypeScript.SyntaxKind.CloseBracketToken:
109+
return TypeScript.SyntaxKind.OpenBracketToken;
110+
case TypeScript.SyntaxKind.GreaterThanToken:
111+
return TypeScript.SyntaxUtilities.isAngleBracket(positionedElement) ? TypeScript.SyntaxKind.LessThanToken : null;
112+
}
113+
return null;
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)