@@ -6,14 +6,14 @@ import { getPackageInfo } from '../../importPackage';
6
6
import { DocumentSnapshot } from './DocumentSnapshot' ;
7
7
import { createSvelteModuleLoader } from './module-loader' ;
8
8
import { SnapshotManager } from './SnapshotManager' ;
9
- import { ensureRealSvelteFilePath , findTsConfigPath , isSvelteFilePath } from './utils' ;
9
+ import { ensureRealSvelteFilePath , findTsConfigPath } from './utils' ;
10
10
11
11
export interface LanguageServiceContainer {
12
12
readonly tsconfigPath : string ;
13
13
readonly compilerOptions : ts . CompilerOptions ;
14
14
readonly snapshotManager : SnapshotManager ;
15
15
getService ( ) : ts . LanguageService ;
16
- updateDocument ( document : Document ) : ts . LanguageService ;
16
+ updateDocument ( documentOrFilePath : Document | string ) : DocumentSnapshot ;
17
17
deleteDocument ( filePath : string ) : void ;
18
18
}
19
19
@@ -37,9 +37,7 @@ export function getLanguageServiceForDocument(
37
37
workspaceUris : string [ ] ,
38
38
docContext : LanguageServiceDocumentContext
39
39
) : ts . LanguageService {
40
- return getService ( document . getFilePath ( ) || '' , workspaceUris , docContext ) . updateDocument (
41
- document
42
- ) ;
40
+ return getLanguageServiceForPath ( document . getFilePath ( ) || '' , workspaceUris , docContext ) ;
43
41
}
44
42
45
43
export function getService (
@@ -130,23 +128,34 @@ export function createLanguageService(
130
128
snapshotManager . delete ( filePath ) ;
131
129
}
132
130
133
- function updateDocument ( document : Document ) : ts . LanguageService {
134
- const preSnapshot = snapshotManager . get ( document . getFilePath ( ) ! ) ;
131
+ function updateDocument ( documentOrFilePath : Document | string ) : DocumentSnapshot {
132
+ const filePath =
133
+ typeof documentOrFilePath === 'string'
134
+ ? documentOrFilePath
135
+ : documentOrFilePath . getFilePath ( ) || '' ;
136
+ const document = typeof documentOrFilePath === 'string' ? undefined : documentOrFilePath ;
137
+ const prevSnapshot = snapshotManager . get ( filePath ) ;
135
138
136
139
// Don't reinitialize document if no update needed.
137
- if ( preSnapshot ?. version === document . version ) {
138
- return languageService ;
140
+ if ( document && prevSnapshot ?. version === document . version ) {
141
+ return prevSnapshot ;
139
142
}
140
143
141
- const newSnapshot = DocumentSnapshot . fromDocument ( document , transformationConfig ) ;
142
- if ( preSnapshot && preSnapshot . scriptKind !== newSnapshot . scriptKind ) {
144
+ const newSnapshot = document
145
+ ? DocumentSnapshot . fromDocument ( document , transformationConfig )
146
+ : DocumentSnapshot . fromFilePath (
147
+ filePath ,
148
+ docContext . createDocument ,
149
+ transformationConfig
150
+ ) ;
151
+ if ( prevSnapshot && prevSnapshot . scriptKind !== newSnapshot . scriptKind ) {
143
152
// Restart language service as it doesn't handle script kind changes.
144
153
languageService . dispose ( ) ;
145
154
languageService = ts . createLanguageService ( host ) ;
146
155
}
147
156
148
- snapshotManager . set ( document . getFilePath ( ) ! , newSnapshot ) ;
149
- return languageService ;
157
+ snapshotManager . set ( filePath , newSnapshot ) ;
158
+ return newSnapshot ;
150
159
}
151
160
152
161
function getSnapshot ( fileName : string ) : DocumentSnapshot {
@@ -157,16 +166,11 @@ export function createLanguageService(
157
166
return doc ;
158
167
}
159
168
160
- if ( isSvelteFilePath ( fileName ) ) {
161
- const file = ts . sys . readFile ( fileName ) || '' ;
162
- doc = DocumentSnapshot . fromDocument (
163
- docContext . createDocument ( fileName , file ) ,
164
- transformationConfig
165
- ) ;
166
- } else {
167
- doc = DocumentSnapshot . fromFilePath ( fileName , transformationConfig ) ;
168
- }
169
-
169
+ doc = DocumentSnapshot . fromFilePath (
170
+ fileName ,
171
+ docContext . createDocument ,
172
+ transformationConfig
173
+ ) ;
170
174
snapshotManager . set ( fileName , doc ) ;
171
175
return doc ;
172
176
}
0 commit comments