@@ -22,6 +22,7 @@ use tower_lsp_server::{
2222mod capabilities;
2323mod code_actions;
2424mod commands;
25+ mod file_system;
2526mod formatter;
2627mod linter;
2728mod options;
@@ -36,6 +37,8 @@ use linter::server_linter::ServerLinterRun;
3637use options:: { Options , WorkspaceOption } ;
3738use worker:: WorkspaceWorker ;
3839
40+ use crate :: file_system:: LSPFileSystem ;
41+
3942type ConcurrentHashMap < K , V > = papaya:: HashMap < K , V , FxBuildHasher > ;
4043
4144const OXC_CONFIG_FILE : & str = ".oxlintrc.json" ;
@@ -51,6 +54,7 @@ struct Backend {
5154 // 2. `workspace/didChangeWorkspaceFolders` request
5255 workspace_workers : Arc < RwLock < Vec < WorkspaceWorker > > > ,
5356 capabilities : OnceCell < Capabilities > ,
57+ file_system : Arc < RwLock < LSPFileSystem > > ,
5458}
5559
5660impl LanguageServer for Backend {
@@ -203,6 +207,9 @@ impl LanguageServer for Backend {
203207
204208 async fn shutdown ( & self ) -> Result < ( ) > {
205209 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+ }
206213 Ok ( ( ) )
207214 }
208215
@@ -444,6 +451,12 @@ impl LanguageServer for Backend {
444451 let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
445452 return ;
446453 } ;
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+
447460 if let Some ( diagnostics) = worker. lint_file ( uri, None , ServerLinterRun :: OnSave ) . await {
448461 self . client
449462 . publish_diagnostics (
@@ -464,6 +477,13 @@ impl LanguageServer for Backend {
464477 return ;
465478 } ;
466479 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+
467487 if let Some ( diagnostics) = worker. lint_file ( uri, content, ServerLinterRun :: OnType ) . await {
468488 self . client
469489 . publish_diagnostics (
@@ -483,6 +503,11 @@ impl LanguageServer for Backend {
483503 } ;
484504
485505 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+
486511 if let Some ( diagnostics) =
487512 worker. lint_file ( uri, Some ( content) , ServerLinterRun :: Always ) . await
488513 {
@@ -502,6 +527,9 @@ impl LanguageServer for Backend {
502527 let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
503528 return ;
504529 } ;
530+ if self . capabilities . get ( ) . is_some_and ( |option| option. dynamic_formatting ) {
531+ self . file_system . write ( ) . await . remove ( uri) ;
532+ }
505533 worker. remove_diagnostics ( & params. text_document . uri ) . await ;
506534 }
507535
@@ -626,6 +654,7 @@ async fn main() {
626654 client,
627655 workspace_workers : Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ,
628656 capabilities : OnceCell :: new ( ) ,
657+ file_system : Arc :: new ( RwLock :: new ( LSPFileSystem :: new ( ) ) ) ,
629658 } )
630659 . finish ( ) ;
631660
0 commit comments