@@ -18,14 +18,16 @@ use tower_lsp::lsp_types::notification::PublishDiagnostics;
18
18
use tower_lsp:: lsp_types:: {
19
19
CodeActionOrCommand , CodeActionParams , CodeActionProviderCapability , CodeActionResponse ,
20
20
Command , ConfigurationItem , Diagnostic , DidChangeConfigurationParams ,
21
- DidChangeTextDocumentParams , DidCloseTextDocumentParams , DidOpenTextDocumentParams ,
22
- DidSaveTextDocumentParams , ExecuteCommandOptions , ExecuteCommandParams , InitializeParams ,
21
+ DidChangeTextDocumentParams , DidChangeWatchedFilesParams ,
22
+ DidChangeWatchedFilesRegistrationOptions , DidCloseTextDocumentParams ,
23
+ DidOpenTextDocumentParams , DidSaveTextDocumentParams , ExecuteCommandOptions ,
24
+ ExecuteCommandParams , FileChangeType , FileSystemWatcher , GlobPattern , InitializeParams ,
23
25
InitializeResult , InitializedParams , MessageType , PublishDiagnosticsParams , Range ,
24
- ServerCapabilities , TextDocumentSyncCapability , TextDocumentSyncKind , TextDocumentSyncOptions ,
25
- TextDocumentSyncSaveOptions , Url ,
26
+ Registration , ServerCapabilities , TextDocumentSyncCapability , TextDocumentSyncKind ,
27
+ TextDocumentSyncOptions , TextDocumentSyncSaveOptions , Url , WatchKind ,
26
28
} ;
27
29
use tower_lsp:: { Client , LanguageServer } ;
28
- use tracing:: { error, info} ;
30
+ use tracing:: { error, info, warn } ;
29
31
30
32
use crate :: config:: Config ;
31
33
use crate :: diagnostics:: { lint_to_code_actions, lints_to_diagnostics} ;
@@ -372,6 +374,27 @@ impl LanguageServer for Backend {
372
374
. await ;
373
375
374
376
self . pull_config ( ) . await ;
377
+
378
+ let did_change_watched_files = Registration {
379
+ id : "workspace/didChangeWatchedFiles" . to_owned ( ) ,
380
+ method : "workspace/didChangeWatchedFiles" . to_owned ( ) ,
381
+ register_options : Some (
382
+ serde_json:: to_value ( DidChangeWatchedFilesRegistrationOptions {
383
+ watchers : vec ! [ FileSystemWatcher {
384
+ glob_pattern: GlobPattern :: String ( "**/*" . to_owned( ) ) ,
385
+ kind: Some ( WatchKind :: Delete ) ,
386
+ } ] ,
387
+ } )
388
+ . unwrap ( ) ,
389
+ ) ,
390
+ } ;
391
+ if let Err ( err) = self
392
+ . client
393
+ . register_capability ( vec ! [ did_change_watched_files] )
394
+ . await
395
+ {
396
+ warn ! ( "Unable to register watch file capability: {}" , err) ;
397
+ }
375
398
}
376
399
377
400
async fn did_save ( & self , params : DidSaveTextDocumentParams ) {
@@ -411,6 +434,38 @@ impl LanguageServer for Backend {
411
434
412
435
async fn did_close ( & self , _params : DidCloseTextDocumentParams ) { }
413
436
437
+ async fn did_change_watched_files ( & self , params : DidChangeWatchedFilesParams ) {
438
+ let mut doc_lock = self . doc_state . lock ( ) . await ;
439
+ let mut urls_to_clear = Vec :: new ( ) ;
440
+
441
+ for change in & params. changes {
442
+ if change. typ != FileChangeType :: DELETED {
443
+ continue ;
444
+ }
445
+
446
+ doc_lock. retain ( |url, _| {
447
+ // `change.uri` could be a directory so use `starts_with` instead of `==`.
448
+ let to_remove = url. as_str ( ) . starts_with ( change. uri . as_str ( ) ) ;
449
+
450
+ if to_remove {
451
+ urls_to_clear. push ( url. clone ( ) ) ;
452
+ }
453
+
454
+ !to_remove
455
+ } ) ;
456
+ }
457
+
458
+ for url in & urls_to_clear {
459
+ self . client
460
+ . send_notification :: < PublishDiagnostics > ( PublishDiagnosticsParams {
461
+ uri : url. clone ( ) ,
462
+ diagnostics : vec ! [ ] ,
463
+ version : None ,
464
+ } )
465
+ . await ;
466
+ }
467
+ }
468
+
414
469
async fn execute_command ( & self , params : ExecuteCommandParams ) -> Result < Option < Value > > {
415
470
let mut string_args = params
416
471
. arguments
0 commit comments