@@ -14,8 +14,8 @@ export interface LanguageServiceContainer {
14
14
readonly compilerOptions : ts . CompilerOptions ;
15
15
readonly snapshotManager : SnapshotManager ;
16
16
getService ( ) : ts . LanguageService ;
17
- updateDocument ( documentOrFilePath : Document | string ) : DocumentSnapshot ;
18
- deleteDocument ( filePath : string ) : void ;
17
+ updateSnapshot ( documentOrFilePath : Document | string ) : DocumentSnapshot ;
18
+ deleteSnapshot ( filePath : string ) : void ;
19
19
}
20
20
21
21
const services = new Map < string , Promise < LanguageServiceContainer > > ( ) ;
@@ -125,36 +125,31 @@ async function createLanguageService(
125
125
tsconfigPath,
126
126
compilerOptions,
127
127
getService : ( ) => languageService ,
128
- updateDocument ,
129
- deleteDocument ,
128
+ updateSnapshot ,
129
+ deleteSnapshot ,
130
130
snapshotManager
131
131
} ;
132
132
133
- function deleteDocument ( filePath : string ) : void {
133
+ function deleteSnapshot ( filePath : string ) : void {
134
134
svelteModuleLoader . deleteFromModuleCache ( filePath ) ;
135
135
snapshotManager . delete ( filePath ) ;
136
136
}
137
137
138
- function updateDocument ( documentOrFilePath : Document | string ) : DocumentSnapshot {
139
- const filePath =
140
- typeof documentOrFilePath === 'string'
141
- ? documentOrFilePath
142
- : documentOrFilePath . getFilePath ( ) || '' ;
143
- const document = typeof documentOrFilePath === 'string' ? undefined : documentOrFilePath ;
144
- const prevSnapshot = snapshotManager . get ( filePath ) ;
138
+ function updateSnapshot ( documentOrFilePath : Document | string ) : DocumentSnapshot {
139
+ return typeof documentOrFilePath === 'string'
140
+ ? updateSnapshotFromFilePath ( documentOrFilePath )
141
+ : updateSnapshotFromDocument ( documentOrFilePath ) ;
142
+ }
145
143
146
- // Don't reinitialize document if no update needed.
147
- if ( document && prevSnapshot ?. version === document . version ) {
144
+ function updateSnapshotFromDocument ( document : Document ) : DocumentSnapshot {
145
+ const filePath = document . getFilePath ( ) || '' ;
146
+ const prevSnapshot = snapshotManager . get ( filePath ) ;
147
+ if ( prevSnapshot ?. version === document . version ) {
148
148
return prevSnapshot ;
149
149
}
150
150
151
- const newSnapshot = document
152
- ? DocumentSnapshot . fromDocument ( document , transformationConfig )
153
- : DocumentSnapshot . fromFilePath (
154
- filePath ,
155
- docContext . createDocument ,
156
- transformationConfig
157
- ) ;
151
+ const newSnapshot = DocumentSnapshot . fromDocument ( document , transformationConfig ) ;
152
+
158
153
snapshotManager . set ( filePath , newSnapshot ) ;
159
154
if ( prevSnapshot && prevSnapshot . scriptKind !== newSnapshot . scriptKind ) {
160
155
// Restart language service as it doesn't handle script kind changes.
@@ -165,6 +160,21 @@ async function createLanguageService(
165
160
return newSnapshot ;
166
161
}
167
162
163
+ function updateSnapshotFromFilePath ( filePath : string ) : DocumentSnapshot {
164
+ const prevSnapshot = snapshotManager . get ( filePath ) ;
165
+ if ( prevSnapshot ) {
166
+ return prevSnapshot ;
167
+ }
168
+
169
+ const newSnapshot = DocumentSnapshot . fromFilePath (
170
+ filePath ,
171
+ docContext . createDocument ,
172
+ transformationConfig
173
+ ) ;
174
+ snapshotManager . set ( filePath , newSnapshot ) ;
175
+ return newSnapshot ;
176
+ }
177
+
168
178
function getSnapshot ( fileName : string ) : DocumentSnapshot {
169
179
fileName = ensureRealSvelteFilePath ( fileName ) ;
170
180
0 commit comments