Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
project: Persist workspace diagnostics when closing buffer (#59875)
Xin Zhaoc7b43b20b409
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rsindex e98b70c3d14..e943edd62eb 100644--- a/crates/project/src/lsp_store.rs+++ b/crates/project/src/lsp_store.rs@@ -4823,8 +4823,21 @@ impl LspStore {let diagnostic_updates = local.language_servers- .keys()- .cloned()+ .iter()+ .filter_map(|(server_id, state)| {+ let supports_workspace_diagnostics = match state {+ LanguageServerState::Running {+ workspace_diagnostics_refresh_tasks,+ ..+ } => !workspace_diagnostics_refresh_tasks.is_empty(),+ _ => false,+ };+ if supports_workspace_diagnostics {+ None+ } else {+ Some(*server_id)+ }+ }).map(|server_id| DocumentDiagnosticsUpdate {diagnostics: DocumentDiagnostics {document_abs_path: buffer_abs_path.clone(),diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rsindex dc9057409bc..6ec9f345021 100644--- a/crates/project/tests/integration/project_tests.rs+++ b/crates/project/tests/integration/project_tests.rs@@ -4431,6 +4431,219 @@ async fn test_diagnostic_summaries_cleared_on_buffer_reload(cx: &mut gpui::TestA);}+#[gpui::test]+async fn test_diagnostic_summaries_cleared_on_buffer_close_without_workspace_diagnostics(+ cx: &mut gpui::TestAppContext,+) {+ init_test(cx);++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(path!("/dir"), json!({ "a.rs": "one two three" }))+ .await;++ let project = Project::test(fs, [path!("/dir").as_ref()], cx).await;+ let language_registry = project.read_with(cx, |project, _| project.languages().clone());+ language_registry.add(rust_lang());++ let mut fake_servers = language_registry.register_fake_lsp(+ "Rust",+ FakeLspAdapter {+ capabilities: lsp::ServerCapabilities {+ diagnostic_provider: Some(lsp::DiagnosticServerCapabilities::Options(+ lsp::DiagnosticOptions {+ identifier: Some("test-close-no-ws".to_string()),+ inter_file_dependencies: true,+ workspace_diagnostics: false,+ work_done_progress_options: Default::default(),+ },+ )),+ ..lsp::ServerCapabilities::default()+ },+ initializer: Some(Box::new(move |fake_server| {+ fake_server.set_request_handler::<lsp::request::DocumentDiagnosticRequest, _, _>(+ move |_, _| async move {+ Ok(lsp::DocumentDiagnosticReportResult::Report(+ lsp::DocumentDiagnosticReport::Full(+ lsp::RelatedFullDocumentDiagnosticReport {+ related_documents: None,+ full_document_diagnostic_report:+ lsp::FullDocumentDiagnosticReport {+ result_id: None,+ items: vec![lsp::Diagnostic {+ range: lsp::Range::new(+ lsp::Position::new(0, 0),+ lsp::Position::new(0, 3),+ ),+ severity: Some(lsp::DiagnosticSeverity::ERROR),+ message: "pulled error no-ws".to_string(),+ ..Default::default()+ }],+ },+ },+ ),+ ))+ },+ );+ })),+ ..FakeLspAdapter::default()+ },+ );++ let (buffer, handle) = project+ .update(cx, |project, cx| {+ project.open_local_buffer_with_lsp(path!("/dir/a.rs"), cx)+ })+ .await+ .unwrap();++ let _fake_server = fake_servers.next().await.unwrap();+ cx.executor().run_until_parked();++ let lsp_store = project.read_with(cx, |project, _| project.lsp_store());+ lsp_store.update(cx, |lsp_store, cx| {+ lsp_store+ .pull_diagnostics_for_buffer(buffer.clone(), cx)+ .detach();+ });+ cx.executor().run_until_parked();++ project.update(cx, |project, cx| {+ assert_eq!(+ project.diagnostic_summary(false, cx),+ DiagnosticSummary {+ error_count: 1,+ warning_count: 0,+ }+ );+ });++ cx.update(|_| {+ drop(buffer);+ drop(handle);+ });+ cx.executor().run_until_parked();++ project.update(cx, |project, cx| {+ assert_eq!(+ project.diagnostic_summary(false, cx),+ DiagnosticSummary {+ error_count: 0,+ warning_count: 0,+ }+ );+ });+}++#[gpui::test]+async fn test_diagnostic_summaries_retained_on_buffer_close_with_workspace_diagnostics(+ cx: &mut gpui::TestAppContext,+) {+ init_test(cx);++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(path!("/dir"), json!({ "a.rs": "one two three" }))+ .await;++ let project = Project::test(fs, [path!("/dir").as_ref()], cx).await;+ let language_registry = project.read_with(cx, |project, _| project.languages().clone());+ language_registry.add(rust_lang());++ let mut fake_servers = language_registry.register_fake_lsp(+ "Rust",+ FakeLspAdapter {+ capabilities: lsp::ServerCapabilities {+ diagnostic_provider: Some(lsp::DiagnosticServerCapabilities::Options(+ lsp::DiagnosticOptions {+ identifier: Some("test-close-ws".to_string()),+ inter_file_dependencies: true,+ workspace_diagnostics: true,+ work_done_progress_options: Default::default(),+ },+ )),+ ..lsp::ServerCapabilities::default()+ },+ initializer: Some(Box::new(move |fake_server| {+ fake_server.set_request_handler::<lsp::request::DocumentDiagnosticRequest, _, _>(+ move |_, _| async move {+ Ok(lsp::DocumentDiagnosticReportResult::Report(+ lsp::DocumentDiagnosticReport::Full(+ lsp::RelatedFullDocumentDiagnosticReport {+ related_documents: None,+ full_document_diagnostic_report:+ lsp::FullDocumentDiagnosticReport {+ result_id: None,+ items: vec![lsp::Diagnostic {+ range: lsp::Range::new(+ lsp::Position::new(0, 0),+ lsp::Position::new(0, 3),+ ),+ severity: Some(lsp::DiagnosticSeverity::ERROR),+ message: "pulled error ws".to_string(),+ ..Default::default()+ }],+ },+ },+ ),+ ))+ },+ );+ fake_server.set_request_handler::<lsp::request::WorkspaceDiagnosticRequest, _, _>(+ move |_, _| async move {+ Ok(lsp::WorkspaceDiagnosticReportResult::Report(+ lsp::WorkspaceDiagnosticReport { items: Vec::new() },+ ))+ },+ );+ })),+ ..FakeLspAdapter::default()+ },+ );++ let (buffer, handle) = project+ .update(cx, |project, cx| {+ project.open_local_buffer_with_lsp(path!("/dir/a.rs"), cx)+ })+ .await+ .unwrap();++ let _fake_server = fake_servers.next().await.unwrap();+ cx.executor().run_until_parked();++ let lsp_store = project.read_with(cx, |project, _| project.lsp_store());+ lsp_store.update(cx, |lsp_store, cx| {+ lsp_store+ .pull_diagnostics_for_buffer(buffer.clone(), cx)+ .detach();+ });+ cx.executor().run_until_parked();++ project.update(cx, |project, cx| {+ assert_eq!(+ project.diagnostic_summary(false, cx),+ DiagnosticSummary {+ error_count: 1,+ warning_count: 0,+ }+ );+ });++ cx.update(|_| {+ drop(buffer);+ drop(handle);+ });+ cx.executor().run_until_parked();++ project.update(cx, |project, cx| {+ assert_eq!(+ project.diagnostic_summary(false, cx),+ DiagnosticSummary {+ error_count: 1,+ warning_count: 0,+ }+ );+ });+}+#[gpui::test]async fn test_edits_from_lsp2_with_past_version(cx: &mut gpui::TestAppContext) {init_test(cx);