@@ -7,18 +7,11 @@ import {
7
7
StatusBarAlignment ,
8
8
StatusBarItem ,
9
9
ThemeColor ,
10
- Uri ,
11
10
window ,
12
11
workspace ,
13
12
} from 'vscode' ;
14
13
15
- import {
16
- DidChangeWatchedFilesNotification ,
17
- ExecuteCommandRequest ,
18
- FileChangeType ,
19
- MessageType ,
20
- ShowMessageNotification ,
21
- } from 'vscode-languageclient' ;
14
+ import { ExecuteCommandRequest , MessageType , ShowMessageNotification } from 'vscode-languageclient' ;
22
15
23
16
import { Executable , LanguageClient , LanguageClientOptions , ServerOptions } from 'vscode-languageclient/node' ;
24
17
@@ -58,14 +51,9 @@ export async function activate(context: ExtensionContext) {
58
51
try {
59
52
if ( client . isRunning ( ) ) {
60
53
await client . restart ( ) ;
61
- // ToDo: refactor it on the server side.
62
- // Do not touch watchers on client side, just simplify the restart of the server.
63
- const configFiles = await findOxlintrcConfigFiles ( ) ;
64
- await sendDidChangeWatchedFilesNotificationWith ( client , configFiles ) ;
65
-
66
54
window . showInformationMessage ( 'oxc server restarted.' ) ;
67
55
} else {
68
- await startClient ( client ) ;
56
+ await client . start ( ) ;
69
57
}
70
58
} catch ( err ) {
71
59
client . error ( 'Restarting client failed' , err , 'force' ) ;
@@ -95,7 +83,7 @@ export async function activate(context: ExtensionContext) {
95
83
}
96
84
} else {
97
85
if ( configService . config . enable ) {
98
- await startClient ( client ) ;
86
+ await client . start ( ) ;
99
87
}
100
88
}
101
89
} ,
@@ -187,6 +175,10 @@ export async function activate(context: ExtensionContext) {
187
175
run,
188
176
debug : run ,
189
177
} ;
178
+
179
+ const fileWatchers = createFileEventWatchers ( configService . config . configPath ) ;
180
+ context . subscriptions . push ( ...fileWatchers ) ;
181
+
190
182
// If the extension is launched in debug mode then the debug server options are used
191
183
// Otherwise the run options are used
192
184
// Options to control the language client
@@ -205,7 +197,7 @@ export async function activate(context: ExtensionContext) {
205
197
} ) ) ,
206
198
synchronize : {
207
199
// Notify the server about file config changes in the workspace
208
- fileEvents : createFileEventWatchers ( configService . config . configPath ) ,
200
+ fileEvents : fileWatchers ,
209
201
} ,
210
202
initializationOptions : {
211
203
settings : configService . config . toLanguageServerConfig ( ) ,
@@ -269,8 +261,6 @@ export async function activate(context: ExtensionContext) {
269
261
270
262
if ( client . isRunning ( ) ) {
271
263
await client . restart ( ) ;
272
- const configFiles = await findOxlintrcConfigFiles ( ) ;
273
- await sendDidChangeWatchedFilesNotificationWith ( client , configFiles ) ;
274
264
}
275
265
}
276
266
} ;
@@ -297,52 +287,28 @@ export async function activate(context: ExtensionContext) {
297
287
updateStatsBar ( configService . config . enable ) ;
298
288
299
289
if ( configService . config . enable ) {
300
- await startClient ( client ) ;
301
- }
302
- }
303
-
304
- // Starts the client, it does not check if it is already started
305
- async function startClient ( client : LanguageClient | undefined ) {
306
- if ( client === undefined ) {
307
- return ;
290
+ await client . start ( ) ;
308
291
}
309
- await client . start ( ) ;
310
- // ToDo: refactor it on the server side.
311
- // Do not touch watchers on client side, just simplify the start of the server.
312
- const configFiles = await findOxlintrcConfigFiles ( ) ;
313
- await sendDidChangeWatchedFilesNotificationWith ( client , configFiles ) ;
314
292
}
315
293
316
- export function deactivate ( ) : Thenable < void > | undefined {
294
+ export async function deactivate ( ) : Promise < void > {
317
295
if ( ! client ) {
318
296
return undefined ;
319
297
}
320
- return client . stop ( ) ;
298
+ await client . stop ( ) ;
299
+ client = undefined ;
321
300
}
322
301
302
+ // FileSystemWatcher are not ready on the start and can take some seconds on bigger repositories
303
+ // ToDo: create test to make sure this will never break
323
304
function createFileEventWatchers ( configRelativePath : string | null ) {
324
- const workspaceConfigPatterns = configRelativePath !== null
325
- ? ( workspace . workspaceFolders || [ ] ) . map ( ( workspaceFolder ) =>
326
- new RelativePattern ( workspaceFolder , configRelativePath )
327
- )
328
- : [ ] ;
305
+ if ( configRelativePath !== null ) {
306
+ return ( workspace . workspaceFolders || [ ] ) . map ( ( workspaceFolder ) =>
307
+ workspace . createFileSystemWatcher ( new RelativePattern ( workspaceFolder , configRelativePath ) )
308
+ ) ;
309
+ }
329
310
330
311
return [
331
312
workspace . createFileSystemWatcher ( `**/${ oxlintConfigFileName } ` ) ,
332
- ...workspaceConfigPatterns . map ( ( pattern ) => {
333
- return workspace . createFileSystemWatcher ( pattern ) ;
334
- } ) ,
335
313
] ;
336
314
}
337
-
338
- async function findOxlintrcConfigFiles ( ) {
339
- return workspace . findFiles ( `**/${ oxlintConfigFileName } ` ) ;
340
- }
341
-
342
- async function sendDidChangeWatchedFilesNotificationWith ( languageClient : LanguageClient , files : Uri [ ] ) {
343
- await languageClient . sendNotification ( DidChangeWatchedFilesNotification . type , {
344
- changes : files . map ( file => {
345
- return { uri : file . toString ( ) , type : FileChangeType . Created } ;
346
- } ) ,
347
- } ) ;
348
- }
0 commit comments