@@ -59,7 +59,7 @@ module ts {
59
59
isOpen : boolean ;
60
60
version : string ;
61
61
scriptSnapshot : IScriptSnapshot ;
62
-
62
+ nameTable : Map < string > ;
63
63
getNamedDeclarations ( ) : Declaration [ ] ;
64
64
}
65
65
@@ -750,6 +750,7 @@ module ts {
750
750
public isOpen : boolean ;
751
751
public languageVersion : ScriptTarget ;
752
752
public identifiers : Map < string > ;
753
+ public nameTable : Map < string > ;
753
754
754
755
private namedDeclarations : Declaration [ ] ;
755
756
@@ -1546,6 +1547,8 @@ module ts {
1546
1547
export function createLanguageServiceSourceFile ( filename : string , scriptSnapshot : IScriptSnapshot , scriptTarget : ScriptTarget , version : string , isOpen : boolean , setNodeParents : boolean ) : SourceFile {
1547
1548
var sourceFile = createSourceFile ( filename , scriptSnapshot . getText ( 0 , scriptSnapshot . getLength ( ) ) , scriptTarget , setNodeParents ) ;
1548
1549
setSourceFileFields ( sourceFile , scriptSnapshot , version , isOpen ) ;
1550
+ // after full parsing we can use table with interned strings as name table
1551
+ sourceFile . nameTable = sourceFile . identifiers ;
1549
1552
return sourceFile ;
1550
1553
}
1551
1554
@@ -1577,6 +1580,9 @@ module ts {
1577
1580
if ( ! disableIncrementalParsing ) {
1578
1581
var newSourceFile = sourceFile . update ( scriptSnapshot . getText ( 0 , scriptSnapshot . getLength ( ) ) , textChangeRange ) ;
1579
1582
setSourceFileFields ( newSourceFile , scriptSnapshot , version , isOpen ) ;
1583
+ // after incremental parsing nameTable might not be up-to-date
1584
+ // drop it so it can be lazily recreated later
1585
+ newSourceFile . nameTable = undefined ;
1580
1586
return newSourceFile ;
1581
1587
}
1582
1588
}
@@ -3235,7 +3241,7 @@ module ts {
3235
3241
3236
3242
if ( node . kind === SyntaxKind . Identifier || node . kind === SyntaxKind . ThisKeyword || node . kind === SyntaxKind . SuperKeyword ||
3237
3243
isLiteralNameOfPropertyDeclarationOrIndexAccess ( node ) || isNameOfExternalModuleImportOrDeclaration ( node ) ) {
3238
- return getReferencesForNode ( node , [ sourceFile ] , /*findInStrings:*/ false , /*findInComments:*/ false ) ;
3244
+ return getReferencesForNode ( node , [ sourceFile ] , /*searchOnlyInCurrentFile*/ true , /* findInStrings:*/ false , /*findInComments:*/ false ) ;
3239
3245
}
3240
3246
3241
3247
switch ( node . kind ) {
@@ -3788,10 +3794,31 @@ module ts {
3788
3794
}
3789
3795
3790
3796
Debug . assert ( node . kind === SyntaxKind . Identifier || node . kind === SyntaxKind . NumericLiteral || node . kind === SyntaxKind . StringLiteral ) ;
3791
- return getReferencesForNode ( node , program . getSourceFiles ( ) , findInStrings , findInComments ) ;
3797
+ return getReferencesForNode ( node , program . getSourceFiles ( ) , /*searchOnlyInCurrentFile*/ false , findInStrings , findInComments ) ;
3798
+ }
3799
+
3800
+ function initializeNameTable ( sourceFile : SourceFile ) : void {
3801
+ var nameTable : Map < string > = { } ;
3802
+
3803
+ walk ( sourceFile ) ;
3804
+ sourceFile . nameTable = nameTable ;
3805
+
3806
+ function walk ( node : Node ) {
3807
+ switch ( node . kind ) {
3808
+ case SyntaxKind . Identifier :
3809
+ nameTable [ ( < Identifier > node ) . text ] = ( < Identifier > node ) . text ;
3810
+ break ;
3811
+ case SyntaxKind . StringLiteral :
3812
+ case SyntaxKind . NumericLiteral :
3813
+ nameTable [ ( < LiteralExpression > node ) . text ] = ( < LiteralExpression > node ) . text ;
3814
+ break ;
3815
+ default :
3816
+ forEachChild ( node , walk ) ;
3817
+ }
3818
+ }
3792
3819
}
3793
3820
3794
- function getReferencesForNode ( node : Node , sourceFiles : SourceFile [ ] , findInStrings : boolean , findInComments : boolean ) : ReferenceEntry [ ] {
3821
+ function getReferencesForNode ( node : Node , sourceFiles : SourceFile [ ] , searchOnlyInCurrentFile : boolean , findInStrings : boolean , findInComments : boolean ) : ReferenceEntry [ ] {
3795
3822
// Labels
3796
3823
if ( isLabelName ( node ) ) {
3797
3824
if ( isJumpStatementTarget ( node ) ) {
@@ -3847,15 +3874,28 @@ module ts {
3847
3874
getReferencesInNode ( scope , symbol , declaredName , node , searchMeaning , findInStrings , findInComments , result ) ;
3848
3875
}
3849
3876
else {
3850
- var internedName = getInternedName ( symbol , declarations )
3851
- forEach ( sourceFiles , sourceFile => {
3852
- cancellationToken . throwIfCancellationRequested ( ) ;
3877
+ if ( searchOnlyInCurrentFile ) {
3878
+ Debug . assert ( sourceFiles . length === 1 ) ;
3879
+ result = [ ] ;
3880
+ getReferencesInNode ( sourceFiles [ 0 ] , symbol , declaredName , node , searchMeaning , findInStrings , findInComments , result ) ;
3881
+ }
3882
+ else {
3883
+ var internedName = getInternedName ( symbol , declarations )
3884
+ forEach ( sourceFiles , sourceFile => {
3885
+ cancellationToken . throwIfCancellationRequested ( ) ;
3853
3886
3854
- if ( lookUp ( sourceFile . identifiers , internedName ) ) {
3855
- result = result || [ ] ;
3856
- getReferencesInNode ( sourceFile , symbol , declaredName , node , searchMeaning , findInStrings , findInComments , result ) ;
3857
- }
3858
- } ) ;
3887
+ if ( ! sourceFile . nameTable ) {
3888
+ initializeNameTable ( sourceFile )
3889
+ }
3890
+
3891
+ Debug . assert ( sourceFile . nameTable !== undefined ) ;
3892
+
3893
+ if ( lookUp ( sourceFile . nameTable , internedName ) ) {
3894
+ result = result || [ ] ;
3895
+ getReferencesInNode ( sourceFile , symbol , declaredName , node , searchMeaning , findInStrings , findInComments , result ) ;
3896
+ }
3897
+ } ) ;
3898
+ }
3859
3899
}
3860
3900
3861
3901
return result ;
0 commit comments