@@ -143,49 +143,81 @@ async function getDiagnostics(
143
143
}
144
144
}
145
145
146
+ const FILE_ENDING_REGEX = / \. ( s v e l t e | d \. t s | t s | j s | j s x | t s x | m j s | c j s | m t s | c t s ) $ / ;
147
+ const VITE_CONFIG_REGEX = / v i t e \. c o n f i g \. ( j s | t s ) \. t i m e s t a m p - / ;
148
+
146
149
class DiagnosticsWatcher {
147
150
private updateDiagnostics : any ;
151
+ private watcher : any ;
152
+ private currentWatchedDirs = new Set < string > ( ) ;
153
+ private userIgnored : Array < ( path : string ) => boolean > ;
148
154
149
155
constructor (
150
156
private workspaceUri : URI ,
151
157
private svelteCheck : SvelteCheck ,
152
158
private writer : Writer ,
153
159
filePathsToIgnore : string [ ] ,
154
- ignoreInitialAdd : boolean
160
+ private ignoreInitialAdd : boolean
155
161
) {
156
- const fileEnding = / \. ( s v e l t e | d \. t s | t s | j s | j s x | t s x | m j s | c j s | m t s | c t s ) $ / ;
157
- const viteConfigRegex = / v i t e \. c o n f i g \. ( j s | t s ) \. t i m e s t a m p - / ;
158
- const userIgnored = createIgnored ( filePathsToIgnore ) ;
159
- const offset = workspaceUri . fsPath . length + 1 ;
162
+ this . userIgnored = createIgnored ( filePathsToIgnore ) ;
160
163
161
- watch ( workspaceUri . fsPath , {
164
+ // Create watcher with initial paths
165
+ this . watcher = watch ( [ ] , {
162
166
ignored : ( path , stats ) => {
163
167
if (
164
168
path . includes ( 'node_modules' ) ||
165
169
path . includes ( '.git' ) ||
166
- ( stats ?. isFile ( ) && ( ! fileEnding . test ( path ) || viteConfigRegex . test ( path ) ) )
170
+ ( stats ?. isFile ( ) && ( ! FILE_ENDING_REGEX . test ( path ) || VITE_CONFIG_REGEX . test ( path ) ) )
167
171
) {
168
172
return true ;
169
173
}
170
174
171
- if ( userIgnored . length !== 0 ) {
172
- path = path . slice ( offset ) ;
173
- for ( const i of userIgnored ) {
174
- if ( i ( path ) ) {
175
+ if ( this . userIgnored . length !== 0 ) {
176
+ // Make path relative to workspace for user ignores
177
+ const workspaceRelative = path . startsWith ( this . workspaceUri . fsPath )
178
+ ? path . slice ( this . workspaceUri . fsPath . length + 1 )
179
+ : path ;
180
+ for ( const i of this . userIgnored ) {
181
+ if ( i ( workspaceRelative ) ) {
175
182
return true ;
176
183
}
177
184
}
178
185
}
179
186
180
187
return false ;
181
188
} ,
182
- ignoreInitial : ignoreInitialAdd
189
+ ignoreInitial : this . ignoreInitialAdd
183
190
} )
184
191
. on ( 'add' , ( path ) => this . updateDocument ( path , true ) )
185
192
. on ( 'unlink' , ( path ) => this . removeDocument ( path ) )
186
193
. on ( 'change' , ( path ) => this . updateDocument ( path , false ) ) ;
187
194
188
- if ( ignoreInitialAdd ) {
195
+ this . updateWatchedDirectories ( ) ;
196
+ }
197
+
198
+ private async updateWatchedDirectories ( ) {
199
+ const watchDirs = await this . svelteCheck . getWatchDirectories ( ) ;
200
+ const dirsToWatch = watchDirs || [ { path : this . workspaceUri . fsPath , recursive : true } ] ;
201
+ const newDirs = new Set ( dirsToWatch . map ( d => d . path ) ) ;
202
+
203
+ // Fast diff: find directories to add and remove
204
+ const toAdd = [ ...newDirs ] . filter ( dir => ! this . currentWatchedDirs . has ( dir ) ) ;
205
+ const toRemove = [ ...this . currentWatchedDirs ] . filter ( dir => ! newDirs . has ( dir ) ) ;
206
+
207
+ // Add new directories
208
+ if ( toAdd . length > 0 ) {
209
+ this . watcher . add ( toAdd ) ;
210
+ }
211
+
212
+ // Remove old directories
213
+ if ( toRemove . length > 0 ) {
214
+ this . watcher . unwatch ( toRemove ) ;
215
+ }
216
+
217
+ // Update current set
218
+ this . currentWatchedDirs = newDirs ;
219
+
220
+ if ( this . ignoreInitialAdd ) {
189
221
this . scheduleDiagnostics ( ) ;
190
222
}
191
223
}
@@ -210,10 +242,15 @@ class DiagnosticsWatcher {
210
242
this . scheduleDiagnostics ( ) ;
211
243
}
212
244
213
- scheduleDiagnostics ( ) {
245
+ scheduleDiagnostics ( updateWatchers = false ) {
214
246
clearTimeout ( this . updateDiagnostics ) ;
215
247
this . updateDiagnostics = setTimeout (
216
- ( ) => getDiagnostics ( this . workspaceUri , this . writer , this . svelteCheck ) ,
248
+ async ( ) => {
249
+ if ( updateWatchers ) {
250
+ await this . updateWatchedDirectories ( ) ;
251
+ }
252
+ getDiagnostics ( this . workspaceUri , this . writer , this . svelteCheck ) ;
253
+ } ,
217
254
1000
218
255
) ;
219
256
}
@@ -264,7 +301,7 @@ parseOptions(async (opts) => {
264
301
} ;
265
302
266
303
if ( opts . watch ) {
267
- svelteCheckOptions . onProjectReload = ( ) => watcher . scheduleDiagnostics ( ) ;
304
+ svelteCheckOptions . onProjectReload = ( ) => watcher . scheduleDiagnostics ( true ) ;
268
305
const watcher = new DiagnosticsWatcher (
269
306
opts . workspaceUri ,
270
307
new SvelteCheck ( opts . workspaceUri . fsPath , svelteCheckOptions ) ,
0 commit comments