Skip to content

Commit cfc234f

Browse files
author
Andy
authored
Simplify getBraceMatchingAtPosition (#21979)
1 parent f8f4bb8 commit cfc234f

File tree

1 file changed

+13
-47
lines changed

1 file changed

+13
-47
lines changed

src/services/services.ts

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,55 +1767,21 @@ namespace ts {
17671767
return OutliningElementsCollector.collectElements(sourceFile, cancellationToken);
17681768
}
17691769

1770-
function getBraceMatchingAtPosition(fileName: string, position: number) {
1771-
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
1772-
const result: TextSpan[] = [];
1770+
const braceMatching = createMapFromTemplate({
1771+
[SyntaxKind.OpenBraceToken]: SyntaxKind.CloseBraceToken,
1772+
[SyntaxKind.OpenParenToken]: SyntaxKind.CloseParenToken,
1773+
[SyntaxKind.OpenBracketToken]: SyntaxKind.CloseBracketToken,
1774+
[SyntaxKind.GreaterThanToken]: SyntaxKind.LessThanToken,
1775+
});
1776+
braceMatching.forEach((value, key) => braceMatching.set(value.toString(), Number(key) as SyntaxKind));
17731777

1778+
function getBraceMatchingAtPosition(fileName: string, position: number): TextSpan[] {
1779+
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
17741780
const token = getTouchingToken(sourceFile, position, /*includeJsDocComment*/ false);
1775-
1776-
if (token.getStart(sourceFile) === position) {
1777-
const matchKind = getMatchingTokenKind(token);
1778-
1779-
// Ensure that there is a corresponding token to match ours.
1780-
if (matchKind) {
1781-
const parentElement = token.parent;
1782-
1783-
const childNodes = parentElement.getChildren(sourceFile);
1784-
for (const current of childNodes) {
1785-
if (current.kind === matchKind) {
1786-
const range1 = createTextSpan(token.getStart(sourceFile), token.getWidth(sourceFile));
1787-
const range2 = createTextSpan(current.getStart(sourceFile), current.getWidth(sourceFile));
1788-
1789-
// We want to order the braces when we return the result.
1790-
if (range1.start < range2.start) {
1791-
result.push(range1, range2);
1792-
}
1793-
else {
1794-
result.push(range2, range1);
1795-
}
1796-
1797-
break;
1798-
}
1799-
}
1800-
}
1801-
}
1802-
1803-
return result;
1804-
1805-
function getMatchingTokenKind(token: Node): ts.SyntaxKind {
1806-
switch (token.kind) {
1807-
case ts.SyntaxKind.OpenBraceToken: return ts.SyntaxKind.CloseBraceToken;
1808-
case ts.SyntaxKind.OpenParenToken: return ts.SyntaxKind.CloseParenToken;
1809-
case ts.SyntaxKind.OpenBracketToken: return ts.SyntaxKind.CloseBracketToken;
1810-
case ts.SyntaxKind.LessThanToken: return ts.SyntaxKind.GreaterThanToken;
1811-
case ts.SyntaxKind.CloseBraceToken: return ts.SyntaxKind.OpenBraceToken;
1812-
case ts.SyntaxKind.CloseParenToken: return ts.SyntaxKind.OpenParenToken;
1813-
case ts.SyntaxKind.CloseBracketToken: return ts.SyntaxKind.OpenBracketToken;
1814-
case ts.SyntaxKind.GreaterThanToken: return ts.SyntaxKind.LessThanToken;
1815-
}
1816-
1817-
return undefined;
1818-
}
1781+
const matchKind = token.getStart(sourceFile) === position ? braceMatching.get(token.kind.toString()) : undefined;
1782+
const match = matchKind && findChildOfKind(token.parent, matchKind, sourceFile);
1783+
// We want to order the braces when we return the result.
1784+
return match ? [createTextSpanFromNode(token, sourceFile), createTextSpanFromNode(match, sourceFile)].sort((a, b) => a.start - b.start) : emptyArray;
18191785
}
18201786

18211787
function getIndentationAtPosition(fileName: string, position: number, editorOptions: EditorOptions | EditorSettings) {

0 commit comments

Comments
 (0)