@@ -13,10 +13,12 @@ use tower_lsp_server::{
1313 DidChangeConfigurationParams , DidChangeTextDocumentParams , DidChangeWatchedFilesParams ,
1414 DidChangeWatchedFilesRegistrationOptions , DidChangeWorkspaceFoldersParams ,
1515 DidCloseTextDocumentParams , DidOpenTextDocumentParams , DidSaveTextDocumentParams ,
16+ DocumentDiagnosticParams , DocumentDiagnosticReport , DocumentDiagnosticReportResult ,
1617 ExecuteCommandParams , FullDocumentDiagnosticReport , InitializeParams , InitializeResult ,
17- InitializedParams , Registration , ServerInfo , Unregistration , Uri ,
18- WorkspaceDiagnosticParams , WorkspaceDiagnosticReport , WorkspaceDiagnosticReportResult ,
19- WorkspaceDocumentDiagnosticReport , WorkspaceEdit , WorkspaceFullDocumentDiagnosticReport ,
18+ InitializedParams , Registration , RelatedFullDocumentDiagnosticReport , ServerInfo ,
19+ Unregistration , Uri , WorkspaceDiagnosticParams , WorkspaceDiagnosticReport ,
20+ WorkspaceDiagnosticReportResult , WorkspaceDocumentDiagnosticReport , WorkspaceEdit ,
21+ WorkspaceFullDocumentDiagnosticReport ,
2022 } ,
2123} ;
2224// #
@@ -467,7 +469,11 @@ impl LanguageServer for Backend {
467469 if !worker. should_lint_on_run_type ( Run :: OnSave ) . await {
468470 return ;
469471 }
470- if let Some ( diagnostics) = worker. lint_file ( uri, None ) . await {
472+
473+ // we no longer need the cached document, as it is saved to disk
474+ worker. remove_cached_document ( uri) ;
475+
476+ if let Some ( diagnostics) = worker. lint_file ( uri) . await {
471477 self . client
472478 . publish_diagnostics (
473479 uri. clone ( ) ,
@@ -490,7 +496,13 @@ impl LanguageServer for Backend {
490496 return ;
491497 }
492498 let content = params. content_changes . first ( ) . map ( |c| c. text . clone ( ) ) ;
493- if let Some ( diagnostics) = worker. lint_file ( uri, content) . await {
499+
500+ if let Some ( content) = content {
501+ // cache the document content for later use
502+ worker. cache_document ( uri, content) ;
503+ }
504+
505+ if let Some ( diagnostics) = worker. lint_file ( uri) . await {
494506 self . client
495507 . publish_diagnostics (
496508 uri. clone ( ) ,
@@ -508,8 +520,10 @@ impl LanguageServer for Backend {
508520 return ;
509521 } ;
510522
511- let content = params. text_document . text ;
512- if let Some ( diagnostics) = worker. lint_file ( uri, Some ( content) ) . await {
523+ // cache the document content for later use
524+ worker. cache_document ( uri, params. text_document . text ) ;
525+
526+ if let Some ( diagnostics) = worker. lint_file ( uri) . await {
513527 self . client
514528 . publish_diagnostics (
515529 uri. clone ( ) ,
@@ -526,7 +540,38 @@ impl LanguageServer for Backend {
526540 let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
527541 return ;
528542 } ;
529- worker. remove_diagnostics ( & params. text_document . uri ) ;
543+ worker. remove_cached_document ( uri) ;
544+ worker. remove_diagnostics ( uri) ;
545+ }
546+
547+ async fn diagnostic (
548+ & self ,
549+ params : DocumentDiagnosticParams ,
550+ ) -> Result < DocumentDiagnosticReportResult > {
551+ let uri = & params. text_document . uri ;
552+ let workers = self . workspace_workers . read ( ) . await ;
553+ let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
554+ return Ok ( DocumentDiagnosticReportResult :: Report ( DocumentDiagnosticReport :: Full (
555+ RelatedFullDocumentDiagnosticReport :: default ( ) ,
556+ ) ) ) ;
557+ } ;
558+ let diagnostics = worker. lint_file ( uri) . await ;
559+
560+ if let Some ( diagnostics) = diagnostics {
561+ Ok ( DocumentDiagnosticReportResult :: Report ( DocumentDiagnosticReport :: Full (
562+ RelatedFullDocumentDiagnosticReport {
563+ full_document_diagnostic_report : FullDocumentDiagnosticReport {
564+ items : diagnostics. into_iter ( ) . map ( |d| d. diagnostic ) . collect ( ) ,
565+ ..Default :: default ( )
566+ } ,
567+ ..Default :: default ( )
568+ } ,
569+ ) ) )
570+ } else {
571+ Ok ( DocumentDiagnosticReportResult :: Report ( DocumentDiagnosticReport :: Full (
572+ RelatedFullDocumentDiagnosticReport :: default ( ) ,
573+ ) ) )
574+ }
530575 }
531576
532577 async fn code_action ( & self , params : CodeActionParams ) -> Result < Option < CodeActionResponse > > {
0 commit comments