@@ -1552,78 +1552,49 @@ module ts {
1552
1552
var file = this . getEntry ( fileName ) ;
1553
1553
return file && file . scriptSnapshot ;
1554
1554
}
1555
-
1556
- public getChangeRange ( fileName : string , lastKnownVersion : string , oldScriptSnapshot : IScriptSnapshot ) : TextChangeRange {
1557
- var currentVersion = this . getVersion ( fileName ) ;
1558
- if ( lastKnownVersion === currentVersion ) {
1559
- return unchangedTextChangeRange ; // "No changes"
1560
- }
1561
-
1562
- var scriptSnapshot = this . getScriptSnapshot ( fileName ) ;
1563
- return scriptSnapshot . getChangeRange ( oldScriptSnapshot ) ;
1564
- }
1565
1555
}
1566
1556
1567
1557
class SyntaxTreeCache {
1568
- private hostCache : HostCache ;
1569
-
1570
1558
// For our syntactic only features, we also keep a cache of the syntax tree for the
1571
1559
// currently edited file.
1572
- private currentFileName : string = "" ;
1573
- private currentFileVersion : string = null ;
1574
- private currentSourceFile : SourceFile = null ;
1560
+ private currentFileName : string ;
1561
+ private currentFileVersion : string ;
1562
+ private currentFileScriptSnapshot : IScriptSnapshot ;
1563
+ private currentSourceFile : SourceFile ;
1575
1564
1576
1565
constructor ( private host : LanguageServiceHost ) {
1577
1566
}
1578
1567
1579
- private log ( message : string ) {
1580
- if ( this . host . log ) {
1581
- this . host . log ( message ) ;
1568
+ public getCurrentSourceFile ( fileName : string ) : SourceFile {
1569
+ var scriptSnapshot = this . host . getScriptSnapshot ( fileName ) ;
1570
+ if ( ! scriptSnapshot ) {
1571
+ // The host does not know about this file.
1572
+ throw new Error ( "Could not find file: '" + fileName + "'." ) ;
1582
1573
}
1583
- }
1584
-
1585
- private initialize ( fileName : string ) {
1586
- // ensure that both source file and syntax tree are either initialized or not initialized
1587
- var start = new Date ( ) . getTime ( ) ;
1588
- this . hostCache = new HostCache ( this . host ) ;
1589
- this . log ( "SyntaxTreeCache.Initialize: new HostCache: " + ( new Date ( ) . getTime ( ) - start ) ) ;
1590
1574
1591
- var version = this . hostCache . getVersion ( fileName ) ;
1575
+ var version = this . host . getScriptVersion ( fileName ) ;
1592
1576
var sourceFile : SourceFile ;
1593
1577
1594
1578
if ( this . currentFileName !== fileName ) {
1595
- var scriptSnapshot = this . hostCache . getScriptSnapshot ( fileName ) ;
1596
-
1597
- var start = new Date ( ) . getTime ( ) ;
1579
+ // This is a new file, just parse it
1598
1580
sourceFile = createLanguageServiceSourceFile ( fileName , scriptSnapshot , ScriptTarget . Latest , version , /*setNodeParents:*/ true ) ;
1599
- this . log ( "SyntaxTreeCache.Initialize: createSourceFile: " + ( new Date ( ) . getTime ( ) - start ) ) ;
1600
1581
}
1601
1582
else if ( this . currentFileVersion !== version ) {
1602
- var scriptSnapshot = this . hostCache . getScriptSnapshot ( fileName ) ;
1603
-
1604
- var editRange = this . hostCache . getChangeRange ( fileName , this . currentFileVersion , this . currentSourceFile . scriptSnapshot ) ;
1605
-
1606
- var start = new Date ( ) . getTime ( ) ;
1583
+ // This is the same file, just a newer version. Incrementally parse the file.
1584
+ var editRange = scriptSnapshot . getChangeRange ( this . currentFileScriptSnapshot ) ;
1607
1585
sourceFile = updateLanguageServiceSourceFile ( this . currentSourceFile , scriptSnapshot , version , editRange ) ;
1608
- this . log ( "SyntaxTreeCache.Initialize: updateSourceFile: " + ( new Date ( ) . getTime ( ) - start ) ) ;
1609
1586
}
1610
1587
1611
1588
if ( sourceFile ) {
1612
1589
// All done, ensure state is up to date
1613
1590
this . currentFileVersion = version ;
1614
1591
this . currentFileName = fileName ;
1592
+ this . currentFileScriptSnapshot = scriptSnapshot ;
1615
1593
this . currentSourceFile = sourceFile ;
1616
1594
}
1617
- }
1618
1595
1619
- public getCurrentSourceFile ( fileName : string ) : SourceFile {
1620
- this . initialize ( fileName ) ;
1621
1596
return this . currentSourceFile ;
1622
1597
}
1623
-
1624
- public getCurrentScriptSnapshot ( fileName : string ) : IScriptSnapshot {
1625
- return this . getCurrentSourceFile ( fileName ) . scriptSnapshot ;
1626
- }
1627
1598
}
1628
1599
1629
1600
function setSourceFileFields ( sourceFile : SourceFile , scriptSnapshot : IScriptSnapshot , version : string ) {
@@ -2069,6 +2040,7 @@ module ts {
2069
2040
}
2070
2041
2071
2042
function getValidSourceFile ( fileName : string ) : SourceFile {
2043
+ fileName = normalizeSlashes ( fileName ) ;
2072
2044
var sourceFile = program . getSourceFile ( getCanonicalFileName ( fileName ) ) ;
2073
2045
if ( ! sourceFile ) {
2074
2046
throw new Error ( "Could not find file: '" + fileName + "'." ) ;
@@ -2157,7 +2129,7 @@ module ts {
2157
2129
}
2158
2130
2159
2131
// We have an older version of the sourceFile, incrementally parse the changes
2160
- var textChangeRange = hostCache . getChangeRange ( fileName , oldSourceFile . version , oldSourceFile . scriptSnapshot ) ;
2132
+ var textChangeRange = hostFileInformation . scriptSnapshot . getChangeRange ( oldSourceFile . scriptSnapshot ) ;
2161
2133
return documentRegistry . updateDocument ( oldSourceFile , fileName , newSettings , hostFileInformation . scriptSnapshot , hostFileInformation . version , textChangeRange ) ;
2162
2134
}
2163
2135
}
@@ -2222,8 +2194,6 @@ module ts {
2222
2194
function getSyntacticDiagnostics ( fileName : string ) {
2223
2195
synchronizeHostData ( ) ;
2224
2196
2225
- fileName = normalizeSlashes ( fileName ) ;
2226
-
2227
2197
return program . getSyntacticDiagnostics ( getValidSourceFile ( fileName ) ) ;
2228
2198
}
2229
2199
@@ -2234,7 +2204,6 @@ module ts {
2234
2204
function getSemanticDiagnostics ( fileName : string ) {
2235
2205
synchronizeHostData ( ) ;
2236
2206
2237
- fileName = normalizeSlashes ( fileName )
2238
2207
var targetSourceFile = getValidSourceFile ( fileName ) ;
2239
2208
2240
2209
// Only perform the action per file regardless of '-out' flag as LanguageServiceHost is expected to call this function per file.
@@ -2311,8 +2280,6 @@ module ts {
2311
2280
function getCompletionsAtPosition ( fileName : string , position : number ) {
2312
2281
synchronizeHostData ( ) ;
2313
2282
2314
- fileName = normalizeSlashes ( fileName ) ;
2315
-
2316
2283
var syntacticStart = new Date ( ) . getTime ( ) ;
2317
2284
var sourceFile = getValidSourceFile ( fileName ) ;
2318
2285
@@ -2731,8 +2698,6 @@ module ts {
2731
2698
function getCompletionEntryDetails ( fileName : string , position : number , entryName : string ) : CompletionEntryDetails {
2732
2699
// Note: No need to call synchronizeHostData, as we have captured all the data we need
2733
2700
// in the getCompletionsAtPosition earlier
2734
- fileName = normalizeSlashes ( fileName ) ;
2735
-
2736
2701
var sourceFile = getValidSourceFile ( fileName ) ;
2737
2702
2738
2703
var session = activeCompletionSession ;
@@ -3195,7 +3160,6 @@ module ts {
3195
3160
function getQuickInfoAtPosition ( fileName : string , position : number ) : QuickInfo {
3196
3161
synchronizeHostData ( ) ;
3197
3162
3198
- fileName = normalizeSlashes ( fileName ) ;
3199
3163
var sourceFile = getValidSourceFile ( fileName ) ;
3200
3164
var node = getTouchingPropertyName ( sourceFile , position ) ;
3201
3165
if ( ! node ) {
@@ -3241,7 +3205,6 @@ module ts {
3241
3205
function getDefinitionAtPosition ( fileName : string , position : number ) : DefinitionInfo [ ] {
3242
3206
synchronizeHostData ( ) ;
3243
3207
3244
- fileName = normalizeSlashes ( fileName ) ;
3245
3208
var sourceFile = getValidSourceFile ( fileName ) ;
3246
3209
3247
3210
var node = getTouchingPropertyName ( sourceFile , position ) ;
@@ -3377,7 +3340,6 @@ module ts {
3377
3340
function getOccurrencesAtPosition ( fileName : string , position : number ) : ReferenceEntry [ ] {
3378
3341
synchronizeHostData ( ) ;
3379
3342
3380
- fileName = normalizeSlashes ( fileName ) ;
3381
3343
var sourceFile = getValidSourceFile ( fileName ) ;
3382
3344
3383
3345
var node = getTouchingWord ( sourceFile , position ) ;
@@ -3926,7 +3888,6 @@ module ts {
3926
3888
function findReferences ( fileName : string , position : number , findInStrings : boolean , findInComments : boolean ) : ReferenceEntry [ ] {
3927
3889
synchronizeHostData ( ) ;
3928
3890
3929
- fileName = normalizeSlashes ( fileName ) ;
3930
3891
var sourceFile = getValidSourceFile ( fileName ) ;
3931
3892
3932
3893
var node = getTouchingPropertyName ( sourceFile , position ) ;
@@ -4670,7 +4631,6 @@ module ts {
4670
4631
function getEmitOutput ( fileName : string ) : EmitOutput {
4671
4632
synchronizeHostData ( ) ;
4672
4633
4673
- fileName = normalizeSlashes ( fileName ) ;
4674
4634
var sourceFile = getValidSourceFile ( fileName ) ;
4675
4635
4676
4636
var outputFiles : OutputFile [ ] = [ ] ;
@@ -4814,23 +4774,21 @@ module ts {
4814
4774
function getSignatureHelpItems ( fileName : string , position : number ) : SignatureHelpItems {
4815
4775
synchronizeHostData ( ) ;
4816
4776
4817
- fileName = normalizeSlashes ( fileName ) ;
4818
4777
var sourceFile = getValidSourceFile ( fileName ) ;
4819
4778
4820
4779
return SignatureHelp . getSignatureHelpItems ( sourceFile , position , typeInfoResolver , cancellationToken ) ;
4821
4780
}
4822
4781
4823
4782
/// Syntactic features
4824
- function getCurrentSourceFile ( fileName : string ) : SourceFile {
4825
- fileName = normalizeSlashes ( fileName ) ;
4826
- var currentSourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
4827
- return currentSourceFile ;
4783
+ function getSourceFile ( fileName : string ) : SourceFile {
4784
+ return syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
4828
4785
}
4829
4786
4830
4787
function getNameOrDottedNameSpan ( fileName : string , startPos : number , endPos : number ) : TextSpan {
4831
- fileName = ts . normalizeSlashes ( fileName ) ;
4788
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
4789
+
4832
4790
// Get node at the location
4833
- var node = getTouchingPropertyName ( getCurrentSourceFile ( fileName ) , startPos ) ;
4791
+ var node = getTouchingPropertyName ( sourceFile , startPos ) ;
4834
4792
4835
4793
if ( ! node ) {
4836
4794
return ;
@@ -4884,19 +4842,19 @@ module ts {
4884
4842
4885
4843
function getBreakpointStatementAtPosition ( fileName : string , position : number ) {
4886
4844
// doesn't use compiler - no need to synchronize with host
4887
- fileName = ts . normalizeSlashes ( fileName ) ;
4888
- return BreakpointResolver . spanInSourceFileAtLocation ( getCurrentSourceFile ( fileName ) , position ) ;
4845
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
4846
+
4847
+ return BreakpointResolver . spanInSourceFileAtLocation ( sourceFile , position ) ;
4889
4848
}
4890
4849
4891
- function getNavigationBarItems ( fileName : string ) : NavigationBarItem [ ] {
4892
- fileName = normalizeSlashes ( fileName ) ;
4850
+ function getNavigationBarItems ( fileName : string ) : NavigationBarItem [ ] {
4851
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
4893
4852
4894
- return NavigationBar . getNavigationBarItems ( getCurrentSourceFile ( fileName ) ) ;
4853
+ return NavigationBar . getNavigationBarItems ( sourceFile ) ;
4895
4854
}
4896
4855
4897
4856
function getSemanticClassifications ( fileName : string , span : TextSpan ) : ClassifiedSpan [ ] {
4898
4857
synchronizeHostData ( ) ;
4899
- fileName = normalizeSlashes ( fileName ) ;
4900
4858
4901
4859
var sourceFile = getValidSourceFile ( fileName ) ;
4902
4860
@@ -4970,8 +4928,7 @@ module ts {
4970
4928
4971
4929
function getSyntacticClassifications ( fileName : string , span : TextSpan ) : ClassifiedSpan [ ] {
4972
4930
// doesn't use compiler - no need to synchronize with host
4973
- fileName = normalizeSlashes ( fileName ) ;
4974
- var sourceFile = getCurrentSourceFile ( fileName ) ;
4931
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
4975
4932
4976
4933
// Make a scanner we can get trivia from.
4977
4934
var triviaScanner = createScanner ( ScriptTarget . Latest , /*skipTrivia:*/ false , sourceFile . text ) ;
@@ -5189,13 +5146,12 @@ module ts {
5189
5146
5190
5147
function getOutliningSpans ( fileName : string ) : OutliningSpan [ ] {
5191
5148
// doesn't use compiler - no need to synchronize with host
5192
- fileName = normalizeSlashes ( fileName ) ;
5193
- var sourceFile = getCurrentSourceFile ( fileName ) ;
5149
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
5194
5150
return OutliningElementsCollector . collectElements ( sourceFile ) ;
5195
5151
}
5196
5152
5197
5153
function getBraceMatchingAtPosition ( fileName : string , position : number ) {
5198
- var sourceFile = getCurrentSourceFile ( fileName ) ;
5154
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
5199
5155
var result : TextSpan [ ] = [ ] ;
5200
5156
5201
5157
var token = getTouchingToken ( sourceFile , position ) ;
@@ -5248,10 +5204,8 @@ module ts {
5248
5204
}
5249
5205
5250
5206
function getIndentationAtPosition ( fileName : string , position : number , editorOptions : EditorOptions ) {
5251
- fileName = normalizeSlashes ( fileName ) ;
5252
-
5253
5207
var start = new Date ( ) . getTime ( ) ;
5254
- var sourceFile = getCurrentSourceFile ( fileName ) ;
5208
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
5255
5209
log ( "getIndentationAtPosition: getCurrentSourceFile: " + ( new Date ( ) . getTime ( ) - start ) ) ;
5256
5210
5257
5211
var start = new Date ( ) . getTime ( ) ;
@@ -5263,22 +5217,17 @@ module ts {
5263
5217
}
5264
5218
5265
5219
function getFormattingEditsForRange ( fileName : string , start : number , end : number , options : FormatCodeOptions ) : TextChange [ ] {
5266
- fileName = normalizeSlashes ( fileName ) ;
5267
- var sourceFile = getCurrentSourceFile ( fileName ) ;
5220
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
5268
5221
return formatting . formatSelection ( start , end , sourceFile , getRuleProvider ( options ) , options ) ;
5269
5222
}
5270
5223
5271
5224
function getFormattingEditsForDocument ( fileName : string , options : FormatCodeOptions ) : TextChange [ ] {
5272
- fileName = normalizeSlashes ( fileName ) ;
5273
-
5274
- var sourceFile = getCurrentSourceFile ( fileName ) ;
5225
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
5275
5226
return formatting . formatDocument ( sourceFile , getRuleProvider ( options ) , options ) ;
5276
5227
}
5277
5228
5278
5229
function getFormattingEditsAfterKeystroke ( fileName : string , position : number , key : string , options : FormatCodeOptions ) : TextChange [ ] {
5279
- fileName = normalizeSlashes ( fileName ) ;
5280
-
5281
- var sourceFile = getCurrentSourceFile ( fileName ) ;
5230
+ var sourceFile = syntaxTreeCache . getCurrentSourceFile ( fileName ) ;
5282
5231
5283
5232
if ( key === "}" ) {
5284
5233
return formatting . formatOnClosingCurly ( position , sourceFile , getRuleProvider ( options ) , options ) ;
@@ -5302,8 +5251,6 @@ module ts {
5302
5251
// anything away.
5303
5252
synchronizeHostData ( ) ;
5304
5253
5305
- fileName = normalizeSlashes ( fileName ) ;
5306
-
5307
5254
var sourceFile = getValidSourceFile ( fileName ) ;
5308
5255
5309
5256
cancellationToken . throwIfCancellationRequested ( ) ;
@@ -5446,7 +5393,6 @@ module ts {
5446
5393
function getRenameInfo ( fileName : string , position : number ) : RenameInfo {
5447
5394
synchronizeHostData ( ) ;
5448
5395
5449
- fileName = normalizeSlashes ( fileName ) ;
5450
5396
var sourceFile = getValidSourceFile ( fileName ) ;
5451
5397
5452
5398
var node = getTouchingWord ( sourceFile , position ) ;
@@ -5530,7 +5476,7 @@ module ts {
5530
5476
getFormattingEditsForDocument,
5531
5477
getFormattingEditsAfterKeystroke,
5532
5478
getEmitOutput,
5533
- getSourceFile : getCurrentSourceFile ,
5479
+ getSourceFile,
5534
5480
getProgram
5535
5481
} ;
5536
5482
}
0 commit comments