@@ -22,6 +22,7 @@ use tower_lsp_server::{
22
22
mod capabilities;
23
23
mod code_actions;
24
24
mod commands;
25
+ mod file_system;
25
26
mod formatter;
26
27
mod linter;
27
28
mod options;
@@ -36,6 +37,8 @@ use linter::server_linter::ServerLinterRun;
36
37
use options:: { Options , WorkspaceOption } ;
37
38
use worker:: WorkspaceWorker ;
38
39
40
+ use crate :: file_system:: LSPFileSystem ;
41
+
39
42
type ConcurrentHashMap < K , V > = papaya:: HashMap < K , V , FxBuildHasher > ;
40
43
41
44
const OXC_CONFIG_FILE : & str = ".oxlintrc.json" ;
@@ -51,6 +54,7 @@ struct Backend {
51
54
// 2. `workspace/didChangeWorkspaceFolders` request
52
55
workspace_workers : Arc < RwLock < Vec < WorkspaceWorker > > > ,
53
56
capabilities : OnceCell < Capabilities > ,
57
+ file_system : Arc < RwLock < LSPFileSystem > > ,
54
58
}
55
59
56
60
impl LanguageServer for Backend {
@@ -203,6 +207,9 @@ impl LanguageServer for Backend {
203
207
204
208
async fn shutdown ( & self ) -> Result < ( ) > {
205
209
self . clear_all_diagnostics ( ) . await ;
210
+ if self . capabilities . get ( ) . is_some_and ( |option| option. dynamic_formatting ) {
211
+ self . file_system . write ( ) . await . clear ( ) ;
212
+ }
206
213
Ok ( ( ) )
207
214
}
208
215
@@ -444,6 +451,12 @@ impl LanguageServer for Backend {
444
451
let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
445
452
return ;
446
453
} ;
454
+
455
+ if self . capabilities . get ( ) . is_some_and ( |option| option. dynamic_formatting ) {
456
+ // saving the file means we can read again from the file system
457
+ self . file_system . write ( ) . await . remove ( uri) ;
458
+ }
459
+
447
460
if let Some ( diagnostics) = worker. lint_file ( uri, None , ServerLinterRun :: OnSave ) . await {
448
461
self . client
449
462
. publish_diagnostics (
@@ -464,6 +477,13 @@ impl LanguageServer for Backend {
464
477
return ;
465
478
} ;
466
479
let content = params. content_changes . first ( ) . map ( |c| c. text . clone ( ) ) ;
480
+
481
+ if self . capabilities . get ( ) . is_some_and ( |option| option. dynamic_formatting )
482
+ && let Some ( content) = & content
483
+ {
484
+ self . file_system . write ( ) . await . set ( uri, content. to_string ( ) ) ;
485
+ }
486
+
467
487
if let Some ( diagnostics) = worker. lint_file ( uri, content, ServerLinterRun :: OnType ) . await {
468
488
self . client
469
489
. publish_diagnostics (
@@ -483,6 +503,11 @@ impl LanguageServer for Backend {
483
503
} ;
484
504
485
505
let content = params. text_document . text ;
506
+
507
+ if self . capabilities . get ( ) . is_some_and ( |option| option. dynamic_formatting ) {
508
+ self . file_system . write ( ) . await . set ( uri, content. to_string ( ) ) ;
509
+ }
510
+
486
511
if let Some ( diagnostics) =
487
512
worker. lint_file ( uri, Some ( content) , ServerLinterRun :: Always ) . await
488
513
{
@@ -502,6 +527,9 @@ impl LanguageServer for Backend {
502
527
let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
503
528
return ;
504
529
} ;
530
+ if self . capabilities . get ( ) . is_some_and ( |option| option. dynamic_formatting ) {
531
+ self . file_system . write ( ) . await . remove ( uri) ;
532
+ }
505
533
worker. remove_diagnostics ( & params. text_document . uri ) . await ;
506
534
}
507
535
@@ -626,6 +654,7 @@ async fn main() {
626
654
client,
627
655
workspace_workers : Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ,
628
656
capabilities : OnceCell :: new ( ) ,
657
+ file_system : Arc :: new ( RwLock :: new ( LSPFileSystem :: new ( ) ) ) ,
629
658
} )
630
659
. finish ( ) ;
631
660
0 commit comments