@@ -23,17 +23,12 @@ function relativeNodePath(fileName: string): string {
23
23
return parts [ parts . length - 1 ] ;
24
24
}
25
25
26
- type EncounteredPaths = {
27
- getScriptSnapshot : string [ ] ;
28
- fileExists : string [ ] ;
29
- readFile : string [ ] ;
30
- } ;
31
-
32
- function getVirtualLanguageService ( ) : {
26
+ function getVirtualLanguageService (
27
+ fallbackServiceHost ?: ts . LanguageServiceHost ,
28
+ ) : {
33
29
languageService : ts . LanguageService ;
34
30
updateCode : UpdateDefinitionFunction ;
35
31
listFiles : ( ) => string [ ] ;
36
- listEncounteredPaths : ( ) => EncounteredPaths ;
37
32
} {
38
33
// as an optimization, the contents of a file can be string or true. This is
39
34
// because some files are only checked for existence during module resolution,
@@ -61,19 +56,7 @@ function getVirtualLanguageService(): {
61
56
return Object . keys ( codeHolder ) ;
62
57
} ;
63
58
64
- const encounteredPaths : EncounteredPaths = {
65
- getScriptSnapshot : [ ] ,
66
- fileExists : [ ] ,
67
- readFile : [ ] ,
68
- } ;
69
- const listEncounteredPaths = ( ) : EncounteredPaths => {
70
- encounteredPaths . getScriptSnapshot . sort ( ) ;
71
- encounteredPaths . fileExists . sort ( ) ;
72
- encounteredPaths . readFile . sort ( ) ;
73
- return encounteredPaths ;
74
- } ;
75
-
76
- const servicesHost : ts . LanguageServiceHost = {
59
+ const serviceHost : ts . LanguageServiceHost = {
77
60
getScriptFileNames : ( ) => {
78
61
return Object . keys ( codeHolder ) ;
79
62
} ,
@@ -87,14 +70,8 @@ function getVirtualLanguageService(): {
87
70
return ts . ScriptSnapshot . fromString ( codeHolder [ fileName ] . toString ( ) ) ;
88
71
}
89
72
90
- if ( process . env . CI ) {
91
- const result = ts . ScriptSnapshot . fromString (
92
- // NOTE: some files do not exist. A good example is "typescript/lib/es2023.ts"
93
- ts . sys . readFile ( fileName ) || '' ,
94
- ) ;
95
-
96
- encounteredPaths . getScriptSnapshot . push ( relativeNodePath ( fileName ) ) ;
97
- return result ;
73
+ if ( fallbackServiceHost ) {
74
+ return fallbackServiceHost . getScriptSnapshot ( fileName ) ;
98
75
}
99
76
} ,
100
77
getCurrentDirectory : ( ) => process . cwd ( ) ,
@@ -108,12 +85,8 @@ function getVirtualLanguageService(): {
108
85
return true ;
109
86
}
110
87
111
- if ( process . env . CI ) {
112
- const result = ts . sys . fileExists ( fileName ) ;
113
- if ( result ) {
114
- encounteredPaths . fileExists . push ( relativeNodePath ( fileName ) ) ;
115
- }
116
- return result ;
88
+ if ( fallbackServiceHost ) {
89
+ return fallbackServiceHost . fileExists ( fileName ) ;
117
90
}
118
91
119
92
return false ;
@@ -124,27 +97,25 @@ function getVirtualLanguageService(): {
124
97
return codeHolder [ fileName ] . toString ( ) ;
125
98
}
126
99
127
- if ( process . env . CI ) {
128
- const result = ts . sys . readFile ( fileName ) ;
129
- encounteredPaths . readFile . push ( relativeNodePath ( fileName ) ) ;
130
- return result ;
100
+ if ( fallbackServiceHost ) {
101
+ return fallbackServiceHost . readFile ( fileName ) ;
131
102
}
132
103
} ,
133
104
readDirectory : ( ...args ) => {
134
- if ( process . env . CI ) {
135
- return ts . sys . readDirectory ( ...args ) ;
105
+ if ( fallbackServiceHost && fallbackServiceHost . readDirectory ) {
106
+ return fallbackServiceHost . readDirectory ( ...args ) ;
136
107
}
137
108
return [ ] ;
138
109
} ,
139
110
directoryExists : ( ...args ) => {
140
- if ( process . env . CI ) {
141
- return ts . sys . directoryExists ( ...args ) ;
111
+ if ( fallbackServiceHost && fallbackServiceHost . directoryExists ) {
112
+ return fallbackServiceHost . directoryExists ( ...args ) ;
142
113
}
143
114
return false ;
144
115
} ,
145
116
getDirectories : ( ...args ) => {
146
- if ( process . env . CI ) {
147
- return ts . sys . getDirectories ( ...args ) ;
117
+ if ( fallbackServiceHost && fallbackServiceHost . getDirectories ) {
118
+ return fallbackServiceHost . getDirectories ( ...args ) ;
148
119
}
149
120
return [ ] ;
150
121
} ,
@@ -155,12 +126,11 @@ function getVirtualLanguageService(): {
155
126
156
127
return {
157
128
languageService : ts . createLanguageService (
158
- servicesHost ,
129
+ serviceHost ,
159
130
ts . createDocumentRegistry ( ) ,
160
131
) ,
161
132
updateCode,
162
133
listFiles,
163
- listEncounteredPaths,
164
134
} ;
165
135
}
166
136
@@ -233,6 +203,7 @@ type AutocompleteFilterFunction = (
233
203
234
204
export type AutocompleterOptions = {
235
205
filter ?: AutocompleteFilterFunction ;
206
+ fallbackServiceHost ?: ts . LanguageServiceHost ;
236
207
} ;
237
208
238
209
function filterDiagnostics ( diagnostics : ts . Diagnostic [ ] ) : {
@@ -259,16 +230,14 @@ export default class Autocompleter {
259
230
private readonly languageService : ts . LanguageService ;
260
231
readonly updateCode : UpdateDefinitionFunction ;
261
232
readonly listFiles : ( ) => string [ ] ;
262
- readonly listEncounteredPaths : ( ) => EncounteredPaths ;
263
233
264
- constructor ( { filter } : AutocompleterOptions = { } ) {
234
+ constructor ( { filter, fallbackServiceHost } : AutocompleterOptions = { } ) {
265
235
this . filter = filter ?? ( ( ) => true ) ;
266
236
( {
267
237
languageService : this . languageService ,
268
238
updateCode : this . updateCode ,
269
239
listFiles : this . listFiles ,
270
- listEncounteredPaths : this . listEncounteredPaths ,
271
- } = getVirtualLanguageService ( ) ) ;
240
+ } = getVirtualLanguageService ( fallbackServiceHost ) ) ;
272
241
}
273
242
274
243
autocomplete ( code : string ) : AutoCompletion [ ] {
0 commit comments