@@ -2210,7 +2210,15 @@ module ts {
2210
2210
2211
2211
// Now traverse back down through the else branches, aggregating if/else keywords of if-statements.
2212
2212
while ( ifStatement ) {
2213
- pushIfAndElseKeywords ( ) ;
2213
+ var children = ifStatement . getChildren ( ) ;
2214
+ pushKeywordIf ( keywords , children [ 0 ] , SyntaxKind . IfKeyword ) ;
2215
+
2216
+ // Generally the 'else' keyword is second-to-last, so we traverse backwards.
2217
+ for ( var i = children . length - 1 ; i >= 0 ; i -- ) {
2218
+ if ( pushKeywordIf ( keywords , children [ i ] , SyntaxKind . ElseKeyword ) ) {
2219
+ break ;
2220
+ }
2221
+ }
2214
2222
2215
2223
if ( ! hasKind ( ifStatement . elseStatement , SyntaxKind . IfStatement ) ) {
2216
2224
break
@@ -2221,42 +2229,36 @@ module ts {
2221
2229
2222
2230
var result : ReferenceEntry [ ] = [ ] ;
2223
2231
2224
- // Here we do a little extra.
2225
- // We'd like to make else/ifs on the same line to be highlighted together
2232
+ // We'd like to highlight else/ifs together if they are only separated by spaces/tabs
2233
+ // (i.e. the keywords are separated by no comments, no newlines).
2226
2234
for ( var i = 0 ; i < keywords . length ; i ++ ) {
2227
2235
if ( keywords [ i ] . kind === SyntaxKind . ElseKeyword && i < keywords . length - 1 ) {
2228
2236
var elseKeyword = keywords [ i ] ;
2229
2237
var ifKeyword = keywords [ i + 1 ] ; // this *should* always be an 'if' keyword.
2230
2238
2231
- // Ensure that the keywords are only separated by trivia.
2232
- if ( elseKeyword . end === ifKeyword . getFullStart ( ) ) {
2233
- var elseLine = sourceFile . getLineAndCharacterFromPosition ( elseKeyword . end ) ;
2234
- var ifLine = sourceFile . getLineAndCharacterFromPosition ( ifKeyword . getStart ( ) ) ;
2239
+ var shouldHighlightNextKeyword = true ;
2235
2240
2236
- if ( elseLine . line === ifLine . line ) {
2237
- result . push ( new ReferenceEntry ( filename , TypeScript . TextSpan . fromBounds ( elseKeyword . getStart ( ) , ifKeyword . end ) , /* isWriteAccess */ false ) ) ;
2238
- i ++ ; // skip the next keyword
2239
- continue ;
2241
+ // Avoid recalculating getStart() by iterating backwards.
2242
+ for ( var j = ifKeyword . getStart ( ) - 1 ; j >= elseKeyword . end ; j -- ) {
2243
+ var c = sourceFile . text . charCodeAt ( j ) ;
2244
+ if ( c !== CharacterCodes . space && c !== CharacterCodes . tab ) {
2245
+ shouldHighlightNextKeyword = false ;
2246
+ break ;
2240
2247
}
2241
2248
}
2249
+
2250
+ if ( shouldHighlightNextKeyword ) {
2251
+ result . push ( new ReferenceEntry ( filename , TypeScript . TextSpan . fromBounds ( elseKeyword . getStart ( ) , ifKeyword . end ) , /* isWriteAccess */ false ) ) ;
2252
+ i ++ ; // skip the next keyword
2253
+ continue ;
2254
+ }
2242
2255
}
2243
2256
2257
+ // Ordinary case: just highlight the keyword.
2244
2258
result . push ( keywordToReferenceEntry ( keywords [ i ] ) ) ;
2245
2259
}
2246
2260
2247
2261
return result ;
2248
-
2249
- function pushIfAndElseKeywords ( ) {
2250
- var children = ifStatement . getChildren ( ) ;
2251
- pushKeywordIf ( keywords , children [ 0 ] , SyntaxKind . IfKeyword ) ;
2252
-
2253
- // Generally the 'else' keyword is second-to-last, so we traverse backwards.
2254
- for ( var i = children . length - 1 ; i >= 0 ; i -- ) {
2255
- if ( pushKeywordIf ( keywords , children [ i ] , SyntaxKind . ElseKeyword ) ) {
2256
- break ;
2257
- }
2258
- }
2259
- }
2260
2262
}
2261
2263
2262
2264
function getTryCatchFinallyOccurrences ( tryStatement : TryStatement ) : ReferenceEntry [ ] {
@@ -2350,7 +2352,7 @@ module ts {
2350
2352
}
2351
2353
2352
2354
function pushKeywordIf ( keywordList : Node [ ] , token : Node , ...expected : SyntaxKind [ ] ) : boolean {
2353
- if ( token && contains ( < SyntaxKind [ ] > expected , token . kind ) ) {
2355
+ if ( token && contains ( expected , token . kind ) ) {
2354
2356
keywordList . push ( token ) ;
2355
2357
return true ;
2356
2358
}
0 commit comments