@@ -1977,6 +1977,24 @@ module ts {
1977
1977
}
1978
1978
}
1979
1979
1980
+ function getTokenAtPosition ( sourceFile : SourceFile , position : number ) {
1981
+ var current : Node = sourceFile ;
1982
+ outer: while ( true ) {
1983
+ // find the child that has this
1984
+ for ( var i = 0 , n = current . getChildCount ( ) ; i < n ; i ++ ) {
1985
+ var child = current . getChildAt ( i ) ;
1986
+ if ( child . getFullStart ( ) <= position && position < child . getEnd ( ) ) {
1987
+ current = child ;
1988
+ continue outer;
1989
+ }
1990
+ if ( child . end > position ) {
1991
+ break ;
1992
+ }
1993
+ }
1994
+ return current ;
1995
+ }
1996
+ }
1997
+
1980
1998
function getContainerNode ( node : Node ) : Node {
1981
1999
while ( true ) {
1982
2000
node = node . parent ;
@@ -3569,19 +3587,19 @@ module ts {
3569
3587
//
3570
3588
// 'i' is for case insensitivity (We do this to match C# TODO comment code).
3571
3589
//
3572
- // 'm' is so we can find matches in a multiline input.
3590
+ // 'm' is so we can find matches in a multi-line input.
3573
3591
return new RegExp ( regExpString , "gim" ) ;
3574
3592
}
3575
3593
3576
- function getTodoComments ( fileName : string , descriptors : TodoCommentDescriptor [ ] ) : TodoComment [ ] {
3577
- fileName = TypeScript . switchToForwardSlashes ( fileName ) ;
3594
+ function getTodoComments ( filename : string , descriptors : TodoCommentDescriptor [ ] ) : TodoComment [ ] {
3595
+ filename = TypeScript . switchToForwardSlashes ( filename ) ;
3596
+
3597
+ var sourceFile = getCurrentSourceFile ( filename ) ;
3578
3598
3579
- var sourceFile = getCurrentSourceFile ( fileName ) ;
3580
- var syntaxTree = sourceFile . getSyntaxTree ( ) ;
3581
3599
cancellationToken . throwIfCancellationRequested ( ) ;
3582
3600
3583
- var text = syntaxTree . text ;
3584
- var fileContents = text . substr ( 0 , text . length ( ) ) ;
3601
+ var fileContents = sourceFile . text ;
3602
+
3585
3603
cancellationToken . throwIfCancellationRequested ( ) ;
3586
3604
3587
3605
var result : TodoComment [ ] = [ ] ;
@@ -3602,7 +3620,7 @@ module ts {
3602
3620
// ["// hack 1", "// ", "hack 1", undefined, "hack"]
3603
3621
//
3604
3622
// Here are the relevant capture groups:
3605
- // 0) The full match for the entire regex .
3623
+ // 0) The full match for the entire regexp .
3606
3624
// 1) The preamble to the message portion.
3607
3625
// 2) The message portion.
3608
3626
// 3...N) The descriptor that was matched - by index. 'undefined' for each
@@ -3616,20 +3634,18 @@ module ts {
3616
3634
var preamble = matchArray [ 1 ] ;
3617
3635
var matchPosition = matchArray . index + preamble . length ;
3618
3636
3619
- // Ok , we have found a match in the file. This is only an acceptable match if
3637
+ // OK , we have found a match in the file. This is only an acceptable match if
3620
3638
// it is contained within a comment.
3621
- var token = TypeScript . findToken ( syntaxTree . sourceUnit ( ) , matchPosition ) ;
3639
+ var token = getTokenAtPosition ( sourceFile , matchPosition ) ;
3622
3640
3623
- if ( matchPosition >= TypeScript . start ( token ) && matchPosition < TypeScript . end ( token ) ) {
3624
- // match was within the token itself. Not in the comment. Keep searching
3625
- // descriptor.
3641
+ if ( token . getStart ( ) <= matchPosition && matchPosition < token . getEnd ( ) ) {
3642
+ // match was within the token itself. Not in the comment. Keep searching // descriptor.
3626
3643
continue ;
3627
3644
}
3628
3645
3629
- // Looks to be within the trivia. See if we can find the comment containing it.
3630
- var triviaList = matchPosition < TypeScript . start ( token ) ? token . leadingTrivia ( syntaxTree . text ) : token . trailingTrivia ( syntaxTree . text ) ;
3631
- var trivia = findContainingComment ( triviaList , matchPosition ) ;
3632
- if ( trivia === null ) {
3646
+ // Looks to be within the trivia. See if we can find the comment containing it.
3647
+ if ( ! getContainingComment ( getTrailingComments ( fileContents , token . getFullStart ( ) ) , matchPosition ) &&
3648
+ ! getContainingComment ( getLeadingComments ( fileContents , token . getFullStart ( ) ) , matchPosition ) ) {
3633
3649
continue ;
3634
3650
}
3635
3651
@@ -3653,25 +3669,27 @@ module ts {
3653
3669
}
3654
3670
3655
3671
return result ;
3656
- }
3657
3672
3658
- function isLetterOrDigit ( char : number ) : boolean {
3659
- return ( char >= TypeScript . CharacterCodes . a && char <= TypeScript . CharacterCodes . z ) ||
3660
- ( char >= TypeScript . CharacterCodes . A && char <= TypeScript . CharacterCodes . Z ) ||
3661
- ( char >= TypeScript . CharacterCodes . _0 && char <= TypeScript . CharacterCodes . _9 ) ;
3662
- }
3663
-
3664
- function findContainingComment ( triviaList : TypeScript . ISyntaxTriviaList , position : number ) : TypeScript . ISyntaxTrivia {
3665
- for ( var i = 0 , n = triviaList . count ( ) ; i < n ; i ++ ) {
3666
- var trivia = triviaList . syntaxTriviaAt ( i ) ;
3667
- var fullEnd = trivia . fullStart ( ) + trivia . fullWidth ( ) ;
3668
- if ( trivia . isComment ( ) && trivia . fullStart ( ) <= position && position < fullEnd ) {
3669
- return trivia ;
3673
+ function getContainingComment ( comments : Comment [ ] , position : number ) : Comment {
3674
+ if ( comments ) {
3675
+ for ( var i = 0 , n = comments . length ; i < n ; i ++ ) {
3676
+ var comment = comments [ i ] ;
3677
+ if ( comment . pos <= position && position < comment . end ) {
3678
+ return comment ;
3679
+ }
3680
+ }
3670
3681
}
3682
+
3683
+ return undefined ;
3671
3684
}
3672
3685
3673
- return null ;
3686
+ function isLetterOrDigit ( char : number ) : boolean {
3687
+ return ( char >= TypeScript . CharacterCodes . a && char <= TypeScript . CharacterCodes . z ) ||
3688
+ ( char >= TypeScript . CharacterCodes . A && char <= TypeScript . CharacterCodes . Z ) ||
3689
+ ( char >= TypeScript . CharacterCodes . _0 && char <= TypeScript . CharacterCodes . _9 ) ;
3690
+ }
3674
3691
}
3692
+
3675
3693
3676
3694
return {
3677
3695
dispose : dispose ,
0 commit comments