@@ -315,46 +315,56 @@ module ts.formatting {
315
315
}
316
316
317
317
function findPrecedingToken ( position : number , sourceFile : SourceFile ) : Node {
318
- return find ( sourceFile , /*diveIntoLastChild*/ false ) ;
318
+ return find ( sourceFile ) ;
319
319
320
- function find ( n : Node , diveIntoLastChild : boolean ) : Node {
320
+ function findRightmostToken ( n : Node ) : Node {
321
321
if ( isToken ( n ) ) {
322
322
return n ;
323
323
}
324
324
325
325
var children = n . getChildren ( ) ;
326
- if ( diveIntoLastChild ) {
327
- var candidate = findLastChildNodeCandidate ( children , /*exclusiveStartPosition*/ children . length ) ;
328
- return candidate && find ( candidate , /*diveIntoLastChild*/ true ) ;
326
+ var candidate = findRightmostChildNodeWithTokens ( children , /*exclusiveStartPosition*/ children . length ) ;
327
+ return candidate && findRightmostToken ( candidate ) ;
328
+
329
+ }
330
+
331
+ function find ( n : Node ) : Node {
332
+ if ( isToken ( n ) ) {
333
+ return n ;
329
334
}
330
335
336
+ var children = n . getChildren ( ) ;
331
337
for ( var i = 0 , len = children . length ; i < len ; ++ i ) {
332
338
var child = children [ i ] ;
333
339
if ( nodeHasTokens ( child ) ) {
334
340
if ( position < child . end ) {
335
341
if ( child . getStart ( sourceFile ) >= position ) {
336
342
// actual start of the node is past the position - previous token should be at the end of previous child
337
- var candidate = findLastChildNodeCandidate ( children , /*exclusiveStartPosition*/ i ) ;
338
- return candidate && find ( candidate , /*diveIntoLastChild*/ true )
343
+ var candidate = findRightmostChildNodeWithTokens ( children , /*exclusiveStartPosition*/ i ) ;
344
+ return candidate && findRightmostToken ( candidate )
339
345
}
340
346
else {
341
347
// candidate should be in this node
342
- return find ( child , diveIntoLastChild ) ;
348
+ return find ( child ) ;
343
349
}
344
350
}
345
351
}
346
352
}
347
353
348
- // here we know that none of child token nodes embrace the position
349
- // try to find the closest token on the left
354
+ Debug . assert ( n . kind === SyntaxKind . SourceFile ) ;
355
+
356
+ // Here we know that none of child token nodes embrace the position,
357
+ // the only known case is when position is at the end of the file.
358
+ // Try to find the rightmost token in the file without filtering.
359
+ // Namely we are skipping the check: 'position < node.end'
350
360
if ( children . length ) {
351
- var candidate = findLastChildNodeCandidate ( children , /*exclusiveStartPosition*/ children . length ) ;
352
- return candidate && find ( candidate , /*diveIntoLastChild*/ true ) ;
361
+ var candidate = findRightmostChildNodeWithTokens ( children , /*exclusiveStartPosition*/ children . length ) ;
362
+ return candidate && findRightmostToken ( candidate ) ;
353
363
}
354
364
}
355
365
356
366
/// finds last node that is considered as candidate for search (isCandidate(node) === true) starting from 'exclusiveStartPosition'
357
- function findLastChildNodeCandidate ( children : Node [ ] , exclusiveStartPosition : number ) : Node {
367
+ function findRightmostChildNodeWithTokens ( children : Node [ ] , exclusiveStartPosition : number ) : Node {
358
368
for ( var i = exclusiveStartPosition - 1 ; i >= 0 ; -- i ) {
359
369
if ( nodeHasTokens ( children [ i ] ) ) {
360
370
return children [ i ] ;
0 commit comments