@@ -13,8 +13,10 @@ use tower_lsp_server::{
1313 DidChangeConfigurationParams , DidChangeTextDocumentParams , DidChangeWatchedFilesParams ,
1414 DidChangeWatchedFilesRegistrationOptions , DidChangeWorkspaceFoldersParams ,
1515 DidCloseTextDocumentParams , DidOpenTextDocumentParams , DidSaveTextDocumentParams ,
16- ExecuteCommandParams , InitializeParams , InitializeResult , InitializedParams , Registration ,
17- ServerInfo , Unregistration , Uri , WorkspaceEdit ,
16+ DocumentDiagnosticParams , DocumentDiagnosticReport , DocumentDiagnosticReportResult ,
17+ ExecuteCommandParams , FullDocumentDiagnosticReport , InitializeParams , InitializeResult ,
18+ InitializedParams , Registration , RelatedFullDocumentDiagnosticReport , ServerInfo ,
19+ Unregistration , Uri , WorkspaceEdit ,
1820 } ,
1921} ;
2022// #
@@ -466,7 +468,11 @@ impl LanguageServer for Backend {
466468 if !worker. should_lint_on_run_type ( Run :: OnSave ) . await {
467469 return ;
468470 }
469- if let Some ( diagnostics) = worker. lint_file ( uri, None ) . await {
471+
472+ // we no longer need the cached document, as it is saved to disk
473+ worker. remove_cached_document ( uri) ;
474+
475+ if let Some ( diagnostics) = worker. lint_file ( uri) . await {
470476 self . client
471477 . publish_diagnostics (
472478 uri. clone ( ) ,
@@ -489,7 +495,13 @@ impl LanguageServer for Backend {
489495 return ;
490496 }
491497 let content = params. content_changes . first ( ) . map ( |c| c. text . clone ( ) ) ;
492- if let Some ( diagnostics) = worker. lint_file ( uri, content) . await {
498+
499+ if let Some ( content) = content {
500+ // cache the document content for later use
501+ worker. cache_document ( uri, content) ;
502+ }
503+
504+ if let Some ( diagnostics) = worker. lint_file ( uri) . await {
493505 self . client
494506 . publish_diagnostics (
495507 uri. clone ( ) ,
@@ -507,8 +519,10 @@ impl LanguageServer for Backend {
507519 return ;
508520 } ;
509521
510- let content = params. text_document . text ;
511- if let Some ( diagnostics) = worker. lint_file ( uri, Some ( content) ) . await {
522+ // cache the document content for later use
523+ worker. cache_document ( uri, params. text_document . text ) ;
524+
525+ if let Some ( diagnostics) = worker. lint_file ( uri) . await {
512526 self . client
513527 . publish_diagnostics (
514528 uri. clone ( ) ,
@@ -525,7 +539,38 @@ impl LanguageServer for Backend {
525539 let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
526540 return ;
527541 } ;
528- worker. remove_diagnostics ( & params. text_document . uri ) ;
542+ worker. remove_cached_document ( uri) ;
543+ worker. remove_diagnostics ( uri) ;
544+ }
545+
546+ async fn diagnostic (
547+ & self ,
548+ params : DocumentDiagnosticParams ,
549+ ) -> Result < DocumentDiagnosticReportResult > {
550+ let uri = & params. text_document . uri ;
551+ let workers = self . workspace_workers . read ( ) . await ;
552+ let Some ( worker) = workers. iter ( ) . find ( |worker| worker. is_responsible_for_uri ( uri) ) else {
553+ return Ok ( DocumentDiagnosticReportResult :: Report ( DocumentDiagnosticReport :: Full (
554+ RelatedFullDocumentDiagnosticReport :: default ( ) ,
555+ ) ) ) ;
556+ } ;
557+ let diagnostics = worker. lint_file ( uri) . await ;
558+
559+ if let Some ( diagnostics) = diagnostics {
560+ Ok ( DocumentDiagnosticReportResult :: Report ( DocumentDiagnosticReport :: Full (
561+ RelatedFullDocumentDiagnosticReport {
562+ full_document_diagnostic_report : FullDocumentDiagnosticReport {
563+ items : diagnostics. into_iter ( ) . map ( |d| d. diagnostic ) . collect ( ) ,
564+ ..Default :: default ( )
565+ } ,
566+ ..Default :: default ( )
567+ } ,
568+ ) ) )
569+ } else {
570+ Ok ( DocumentDiagnosticReportResult :: Report ( DocumentDiagnosticReport :: Full (
571+ RelatedFullDocumentDiagnosticReport :: default ( ) ,
572+ ) ) )
573+ }
529574 }
530575
531576 async fn code_action ( & self , params : CodeActionParams ) -> Result < Option < CodeActionResponse > > {
0 commit comments