@@ -24,93 +24,50 @@ module TypeScript.Services {
24
24
public static getMatchSpans ( syntaxTree : TypeScript . SyntaxTree , position : number ) : TypeScript . TextSpan [ ] {
25
25
var result : TypeScript . TextSpan [ ] = [ ] ;
26
26
27
- var currentToken = findToken ( syntaxTree . sourceUnit ( ) , position ) ;
27
+ var token = findToken ( syntaxTree . sourceUnit ( ) , position ) ;
28
28
29
- BraceMatcher . getMatchingCloseBrace ( currentToken , position , result ) ;
30
- BraceMatcher . getMatchingOpenBrace ( currentToken , position , result ) ;
29
+ if ( start ( token ) === position ) {
30
+ var matchKind = BraceMatcher . getMatchingTokenKind ( token ) ;
31
31
32
- return result ;
33
- }
32
+ if ( matchKind !== null ) {
33
+ var parentElement = token . parent ;
34
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
35
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
- }
36
+ var current = childAt ( parentElement , i ) ;
63
37
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 ) ;
38
+ if ( current !== null && fullWidth ( current ) > 0 ) {
39
+ if ( current . kind ( ) === matchKind ) {
40
+ var range1 = new TypeScript . TextSpan ( start ( token ) , width ( token ) ) ;
41
+ var range2 = new TypeScript . TextSpan ( start ( current ) , width ( current ) ) ;
42
+ if ( range1 . start ( ) < range2 . start ( ) ) {
43
+ result . push ( range1 , range2 ) ;
44
+ }
45
+ else {
46
+ result . push ( range2 , range1 ) ;
47
+ }
76
48
break ;
77
49
}
78
-
79
- currentPosition -= fullWidth ( element ) ;
80
50
}
81
51
}
82
52
}
83
53
}
84
- }
85
54
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 ;
55
+ return result ;
99
56
}
100
57
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 ;
58
+ private static getMatchingTokenKind ( token : TypeScript . ISyntaxToken ) : TypeScript . SyntaxKind {
59
+ switch ( token . kind ( ) ) {
60
+ case TypeScript . SyntaxKind . OpenBraceToken : return TypeScript . SyntaxKind . CloseBraceToken
61
+ case TypeScript . SyntaxKind . OpenParenToken : return TypeScript . SyntaxKind . CloseParenToken ;
62
+ case TypeScript . SyntaxKind . OpenBracketToken : return TypeScript . SyntaxKind . CloseBracketToken ;
63
+ case TypeScript . SyntaxKind . LessThanToken : return TypeScript . SyntaxKind . GreaterThanToken ;
64
+ case TypeScript . SyntaxKind . CloseBraceToken : return TypeScript . SyntaxKind . OpenBraceToken
65
+ case TypeScript . SyntaxKind . CloseParenToken : return TypeScript . SyntaxKind . OpenParenToken ;
66
+ case TypeScript . SyntaxKind . CloseBracketToken : return TypeScript . SyntaxKind . OpenBracketToken ;
67
+ case TypeScript . SyntaxKind . GreaterThanToken : return TypeScript . SyntaxKind . LessThanToken ;
112
68
}
69
+
113
70
return null ;
114
71
}
115
72
}
116
- }
73
+ }
0 commit comments