Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

project: Send diagnostic related information in code action requests (#62110)

Buyun Xu6dee3fc7559e
diff --git a/crates/language/src/diagnostic.rs b/crates/language/src/diagnostic.rs
index 9a468a14b86..010039760aa 100644
--- a/crates/language/src/diagnostic.rs
+++ b/crates/language/src/diagnostic.rs
@@ -1,7 +1,8 @@
use gpui::SharedString;
-use lsp::{DiagnosticSeverity, NumberOrString};
+use lsp::{DiagnosticRelatedInformation, DiagnosticSeverity, NumberOrString};
use serde::{Deserialize, Serialize};
use serde_json::Value;
+use std::sync::Arc;
/// A diagnostic associated with a certain range of a buffer.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -45,6 +46,10 @@ pub struct Diagnostic {
pub data: Option<Value>,
/// Whether to underline the corresponding text range in the editor.
pub underline: bool,
+ /// Related information from the language server that produced this diagnostic. Passed back to the LS when we request code actions for this diagnostic.
+ ///
+ /// Kept as sent, since flattening it into non-primary entries is lossy.
+ pub related_information: Option<Arc<[DiagnosticRelatedInformation]>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -71,6 +76,7 @@ impl Default for Diagnostic {
underline: true,
data: None,
registration_id: None,
+ related_information: None,
}
}
}
diff --git a/crates/language/src/diagnostic_set.rs b/crates/language/src/diagnostic_set.rs
index 04564ecd657..42e764f07ab 100644
--- a/crates/language/src/diagnostic_set.rs
+++ b/crates/language/src/diagnostic_set.rs
@@ -118,17 +118,11 @@ impl DiagnosticEntry<PointUtf16> {
/// Returns a raw LSP diagnostic used to provide diagnostic context to LSP
/// codeAction request
pub fn to_lsp_diagnostic_stub(&self) -> Result<lsp::Diagnostic> {
- let range = range_to_lsp(self.range.clone())?;
-
- Ok(lsp::Diagnostic {
- range,
- code: self.diagnostic.code.clone(),
- severity: Some(self.diagnostic.severity),
- source: self.diagnostic.source.clone(),
- message: self.diagnostic.message.clone(),
- data: self.diagnostic.data.clone(),
- ..Default::default()
- })
+ DiagnosticEntryRef {
+ range: self.range.clone(),
+ diagnostic: &self.diagnostic,
+ }
+ .to_lsp_diagnostic_stub()
}
}
impl DiagnosticEntryRef<'_, PointUtf16> {
@@ -144,6 +138,11 @@ impl DiagnosticEntryRef<'_, PointUtf16> {
source: self.diagnostic.source.clone(),
message: self.diagnostic.message.clone(),
data: self.diagnostic.data.clone(),
+ related_information: self
+ .diagnostic
+ .related_information
+ .as_ref()
+ .map(|related_information| related_information.to_vec()),
..Default::default()
})
}
diff --git a/crates/language/src/proto.rs b/crates/language/src/proto.rs
index 5e3179e929d..4e5731d23e4 100644
--- a/crates/language/src/proto.rs
+++ b/crates/language/src/proto.rs
@@ -473,6 +473,8 @@ pub fn deserialize_diagnostics(
proto::diagnostic::SourceKind::Other => DiagnosticSourceKind::Other,
},
data,
+ // Not serialized: only the peer talking to the language server sends these back.
+ related_information: None,
},
})
})
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs
index df3fa475986..85e044056b1 100644
--- a/crates/project/src/lsp_store.rs
+++ b/crates/project/src/lsp_store.rs
@@ -12188,7 +12188,14 @@ impl LspStore {
if is_supporting {
supporting_diagnostics.insert(
(source, diagnostic.code.clone(), range),
- (diagnostic.severity, is_unnecessary),
+ (
+ diagnostic.severity,
+ is_unnecessary,
+ diagnostic
+ .related_information
+ .as_ref()
+ .map(|infos| Arc::from(infos.as_slice())),
+ ),
);
} else {
let group_id = post_inc(&mut self.as_local_mut().unwrap().next_diagnostic_group_id);
@@ -12221,6 +12228,10 @@ impl LspStore {
underline,
data: diagnostic.data.clone(),
registration_id: registration_id.clone(),
+ related_information: diagnostic
+ .related_information
+ .as_ref()
+ .map(|infos| Arc::from(infos.as_slice())),
},
});
if let Some(infos) = &diagnostic.related_information {
@@ -12249,6 +12260,7 @@ impl LspStore {
underline,
data: diagnostic.data.clone(),
registration_id: registration_id.clone(),
+ related_information: None,
},
});
}
@@ -12261,15 +12273,18 @@ impl LspStore {
let diagnostic = &mut entry.diagnostic;
if !diagnostic.is_primary {
let source = *sources_by_group_id.get(&diagnostic.group_id).unwrap();
- if let Some(&(severity, is_unnecessary)) = supporting_diagnostics.get(&(
- source,
- diagnostic.code.clone(),
- entry.range.clone(),
- )) {
+ if let Some((severity, is_unnecessary, related_information)) =
+ supporting_diagnostics.get(&(
+ source,
+ diagnostic.code.clone(),
+ entry.range.clone(),
+ ))
+ {
if let Some(severity) = severity {
- diagnostic.severity = severity;
+ diagnostic.severity = *severity;
}
- diagnostic.is_unnecessary = is_unnecessary;
+ diagnostic.is_unnecessary = *is_unnecessary;
+ diagnostic.related_information = related_information.clone();
}
}
}
diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rs
index cba0f6cb8e4..e926216141c 100644
--- a/crates/project/tests/integration/project_tests.rs
+++ b/crates/project/tests/integration/project_tests.rs
@@ -7609,87 +7609,82 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
.unwrap();
let buffer_uri = Uri::from_file_path(path!("/dir/a.rs")).unwrap();
+ let error_1_related_information: Arc<[lsp::DiagnosticRelatedInformation]> =
+ Arc::from([lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: buffer_uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
+ },
+ message: "error 1 hint 1".to_string(),
+ }]);
+ let error_2_related_information: Arc<[lsp::DiagnosticRelatedInformation]> = Arc::from([
+ lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: buffer_uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
+ },
+ message: "error 2 hint 1".to_string(),
+ },
+ lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: buffer_uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
+ },
+ message: "error 2 hint 2".to_string(),
+ },
+ ]);
+ let error_1_hint_related_information: Arc<[lsp::DiagnosticRelatedInformation]> =
+ Arc::from([lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: buffer_uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
+ },
+ message: "original diagnostic".to_string(),
+ }]);
+ let error_2_hint_related_information: Arc<[lsp::DiagnosticRelatedInformation]> =
+ Arc::from([lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: buffer_uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(2, 8), lsp::Position::new(2, 17)),
+ },
+ message: "original diagnostic".to_string(),
+ }]);
let message = lsp::PublishDiagnosticsParams {
- uri: buffer_uri.clone(),
+ uri: buffer_uri,
diagnostics: vec![
lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
severity: Some(DiagnosticSeverity::WARNING),
message: "error 1".to_string(),
- related_information: Some(vec![lsp::DiagnosticRelatedInformation {
- location: lsp::Location {
- uri: buffer_uri.clone(),
- range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
- },
- message: "error 1 hint 1".to_string(),
- }]),
+ related_information: Some(error_1_related_information.to_vec()),
..Default::default()
},
lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
severity: Some(DiagnosticSeverity::HINT),
message: "error 1 hint 1".to_string(),
- related_information: Some(vec![lsp::DiagnosticRelatedInformation {
- location: lsp::Location {
- uri: buffer_uri.clone(),
- range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
- },
- message: "original diagnostic".to_string(),
- }]),
+ related_information: Some(error_1_hint_related_information.to_vec()),
..Default::default()
},
lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(2, 8), lsp::Position::new(2, 17)),
severity: Some(DiagnosticSeverity::ERROR),
message: "error 2".to_string(),
- related_information: Some(vec![
- lsp::DiagnosticRelatedInformation {
- location: lsp::Location {
- uri: buffer_uri.clone(),
- range: lsp::Range::new(
- lsp::Position::new(1, 13),
- lsp::Position::new(1, 15),
- ),
- },
- message: "error 2 hint 1".to_string(),
- },
- lsp::DiagnosticRelatedInformation {
- location: lsp::Location {
- uri: buffer_uri.clone(),
- range: lsp::Range::new(
- lsp::Position::new(1, 13),
- lsp::Position::new(1, 15),
- ),
- },
- message: "error 2 hint 2".to_string(),
- },
- ]),
+ related_information: Some(error_2_related_information.to_vec()),
..Default::default()
},
lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
severity: Some(DiagnosticSeverity::HINT),
message: "error 2 hint 1".to_string(),
- related_information: Some(vec![lsp::DiagnosticRelatedInformation {
- location: lsp::Location {
- uri: buffer_uri.clone(),
- range: lsp::Range::new(lsp::Position::new(2, 8), lsp::Position::new(2, 17)),
- },
- message: "original diagnostic".to_string(),
- }]),
+ related_information: Some(error_2_hint_related_information.to_vec()),
..Default::default()
},
lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
severity: Some(DiagnosticSeverity::HINT),
message: "error 2 hint 2".to_string(),
- related_information: Some(vec![lsp::DiagnosticRelatedInformation {
- location: lsp::Location {
- uri: buffer_uri,
- range: lsp::Range::new(lsp::Position::new(2, 8), lsp::Position::new(2, 17)),
- },
- message: "original diagnostic".to_string(),
- }]),
+ related_information: Some(error_2_hint_related_information.to_vec()),
..Default::default()
},
],
@@ -7723,6 +7718,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 1,
is_primary: true,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_1_related_information.clone()),
..Diagnostic::default()
}
},
@@ -7734,6 +7730,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 1,
is_primary: false,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_1_hint_related_information.clone()),
..Diagnostic::default()
}
},
@@ -7745,6 +7742,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 0,
is_primary: false,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_2_hint_related_information.clone()),
..Diagnostic::default()
}
},
@@ -7756,6 +7754,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 0,
is_primary: false,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_2_hint_related_information.clone()),
..Diagnostic::default()
}
},
@@ -7767,6 +7766,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 0,
is_primary: true,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_2_related_information.clone()),
..Diagnostic::default()
}
}
@@ -7784,6 +7784,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 0,
is_primary: false,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_2_hint_related_information.clone()),
..Diagnostic::default()
}
},
@@ -7795,6 +7796,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 0,
is_primary: false,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_2_hint_related_information),
..Diagnostic::default()
}
},
@@ -7806,6 +7808,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 0,
is_primary: true,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_2_related_information),
..Diagnostic::default()
}
}
@@ -7823,6 +7826,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 1,
is_primary: true,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_1_related_information),
..Diagnostic::default()
}
},
@@ -7834,6 +7838,7 @@ async fn test_grouped_diagnostics(cx: &mut gpui::TestAppContext) {
group_id: 1,
is_primary: false,
source_kind: DiagnosticSourceKind::Pushed,
+ related_information: Some(error_1_hint_related_information),
..Diagnostic::default()
}
},
@@ -9697,6 +9702,430 @@ async fn test_code_actions_only_kinds(cx: &mut gpui::TestAppContext) {
);
}
+#[gpui::test]
+async fn test_code_actions_include_related_information(cx: &mut gpui::TestAppContext) {
+ let (project, buffer, _handle, fake_servers) =
+ code_action_project(&["test-language-server"], cx).await;
+ let fake_server = &fake_servers[0];
+
+ let uri = Uri::from_file_path(path!("/dir/a.ts")).unwrap();
+ let other_uri = Uri::from_file_path(path!("/dir/b.ts")).unwrap();
+ let related_information = vec![
+ lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1)),
+ },
+ message: "overlapping related diagnostic".to_string(),
+ },
+ lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(0, 2), lsp::Position::new(0, 3)),
+ },
+ message: "non-overlapping related diagnostic".to_string(),
+ },
+ // Flattening drops this one, as it points at another file.
+ lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: other_uri,
+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1)),
+ },
+ message: "related diagnostic in another file".to_string(),
+ },
+ // Flattening drops this one, as its message is empty.
+ lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(0, 3), lsp::Position::new(0, 4)),
+ },
+ message: String::new(),
+ },
+ ];
+ fake_server.notify::<lsp::notification::PublishDiagnostics>(lsp::PublishDiagnosticsParams {
+ uri,
+ version: None,
+ diagnostics: vec![lsp::Diagnostic {
+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1)),
+ severity: Some(DiagnosticSeverity::ERROR),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "primary diagnostic".to_string(),
+ related_information: Some(related_information.clone()),
+ data: Some(json!({ "fix_id": "test-fix" })),
+ ..Default::default()
+ }],
+ });
+ cx.executor().run_until_parked();
+
+ let mut request_handled = fake_server
+ .set_request_handler::<lsp::request::CodeActionRequest, _, _>(move |params, _| {
+ let related_information = related_information.clone();
+ async move {
+ assert_eq!(
+ params.context.diagnostics,
+ vec![
+ lsp::Diagnostic {
+ range: lsp::Range::new(
+ lsp::Position::new(0, 0),
+ lsp::Position::new(0, 1)
+ ),
+ severity: Some(DiagnosticSeverity::ERROR),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "primary diagnostic".to_string(),
+ related_information: Some(related_information),
+ data: Some(json!({ "fix_id": "test-fix" })),
+ ..Default::default()
+ },
+ // The flattened entry is still sent as its own diagnostic.
+ lsp::Diagnostic {
+ range: lsp::Range::new(
+ lsp::Position::new(0, 0),
+ lsp::Position::new(0, 1)
+ ),
+ severity: Some(DiagnosticSeverity::INFORMATION),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "overlapping related diagnostic".to_string(),
+ data: Some(json!({ "fix_id": "test-fix" })),
+ ..Default::default()
+ },
+ ]
+ );
+ Ok(Some(Vec::new()))
+ }
+ });
+
+ let code_actions_task = project.update(cx, |project, cx| {
+ project.code_actions(&buffer, 0..1, None, cx)
+ });
+
+ request_handled
+ .next()
+ .await
+ .expect("The code action request should have been triggered");
+ assert!(code_actions_task.await.unwrap().unwrap().is_empty());
+}
+
+#[gpui::test]
+async fn test_code_actions_without_related_information(cx: &mut gpui::TestAppContext) {
+ let (project, buffer, _handle, fake_servers) =
+ code_action_project(&["test-language-server"], cx).await;
+ let fake_server = &fake_servers[0];
+
+ fake_server.notify::<lsp::notification::PublishDiagnostics>(lsp::PublishDiagnosticsParams {
+ uri: Uri::from_file_path(path!("/dir/a.ts")).unwrap(),
+ version: None,
+ diagnostics: vec![lsp::Diagnostic {
+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1)),
+ severity: Some(DiagnosticSeverity::ERROR),
+ source: Some("test-language-server".to_string()),
+ message: "primary diagnostic".to_string(),
+ ..Default::default()
+ }],
+ });
+ cx.executor().run_until_parked();
+
+ let mut request_handled = fake_server
+ .set_request_handler::<lsp::request::CodeActionRequest, _, _>(|params, _| async move {
+ assert_eq!(
+ params.context.diagnostics,
+ vec![lsp::Diagnostic {
+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1)),
+ severity: Some(DiagnosticSeverity::ERROR),
+ source: Some("test-language-server".to_string()),
+ message: "primary diagnostic".to_string(),
+ related_information: None,
+ ..Default::default()
+ }]
+ );
+ Ok(Some(Vec::new()))
+ });
+
+ let code_actions_task = project.update(cx, |project, cx| {
+ project.code_actions(&buffer, 0..1, None, cx)
+ });
+
+ request_handled
+ .next()
+ .await
+ .expect("The code action request should have been triggered");
+ assert!(code_actions_task.await.unwrap().unwrap().is_empty());
+}
+
+#[gpui::test]
+async fn test_code_actions_with_primary_diagnostic_outside_of_the_range(
+ cx: &mut gpui::TestAppContext,
+) {
+ let (project, buffer, _handle, fake_servers) =
+ code_action_project(&["test-language-server"], cx).await;
+ let fake_server = &fake_servers[0];
+
+ let uri = Uri::from_file_path(path!("/dir/a.ts")).unwrap();
+ fake_server.notify::<lsp::notification::PublishDiagnostics>(lsp::PublishDiagnosticsParams {
+ uri: uri.clone(),
+ version: None,
+ diagnostics: vec![lsp::Diagnostic {
+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1)),
+ severity: Some(DiagnosticSeverity::ERROR),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "primary diagnostic".to_string(),
+ related_information: Some(vec![lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri,
+ range: lsp::Range::new(lsp::Position::new(0, 2), lsp::Position::new(0, 3)),
+ },
+ message: "related diagnostic".to_string(),
+ }]),
+ ..Default::default()
+ }],
+ });
+ cx.executor().run_until_parked();
+
+ let mut request_handled = fake_server
+ .set_request_handler::<lsp::request::CodeActionRequest, _, _>(|params, _| async move {
+ // The primary is out of range, so only the flattened entry is sent.
+ assert_eq!(
+ params.context.diagnostics,
+ vec![lsp::Diagnostic {
+ range: lsp::Range::new(lsp::Position::new(0, 2), lsp::Position::new(0, 3)),
+ severity: Some(DiagnosticSeverity::INFORMATION),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "related diagnostic".to_string(),
+ related_information: None,
+ ..Default::default()
+ }]
+ );
+ Ok(Some(Vec::new()))
+ });
+
+ let code_actions_task = project.update(cx, |project, cx| {
+ project.code_actions(&buffer, 2..3, None, cx)
+ });
+
+ request_handled
+ .next()
+ .await
+ .expect("The code action request should have been triggered");
+ assert!(code_actions_task.await.unwrap().unwrap().is_empty());
+}
+
+#[gpui::test]
+async fn test_code_actions_preserve_supporting_diagnostic_entry(cx: &mut gpui::TestAppContext) {
+ let (project, buffer, _handle, fake_servers) =
+ code_action_project(&["test-language-server"], cx).await;
+ let fake_server = &fake_servers[0];
+
+ let uri = Uri::from_file_path(path!("/dir/a.ts")).unwrap();
+ let primary_range = lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1));
+ let supporting_range = lsp::Range::new(lsp::Position::new(0, 2), lsp::Position::new(0, 3));
+ fake_server.notify::<lsp::notification::PublishDiagnostics>(lsp::PublishDiagnosticsParams {
+ uri: uri.clone(),
+ version: None,
+ diagnostics: vec![
+ lsp::Diagnostic {
+ range: primary_range,
+ severity: Some(DiagnosticSeverity::ERROR),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "primary diagnostic".to_string(),
+ related_information: Some(vec![lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: uri.clone(),
+ range: supporting_range,
+ },
+ message: "supporting diagnostic".to_string(),
+ }]),
+ ..Default::default()
+ },
+ // Sorted by severity first, so this is merged into the error's flattened entry.
+ lsp::Diagnostic {
+ range: supporting_range,
+ severity: Some(DiagnosticSeverity::WARNING),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "supporting diagnostic".to_string(),
+ related_information: Some(vec![lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: uri.clone(),
+ range: primary_range,
+ },
+ message: "primary diagnostic".to_string(),
+ }]),
+ ..Default::default()
+ },
+ ],
+ });
+ cx.executor().run_until_parked();
+
+ let mut request_handled = fake_server
+ .set_request_handler::<lsp::request::CodeActionRequest, _, _>(
+ move |params, _| async move {
+ assert_eq!(
+ params.context.diagnostics,
+ vec![
+ lsp::Diagnostic {
+ range: primary_range,
+ severity: Some(DiagnosticSeverity::ERROR),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "primary diagnostic".to_string(),
+ related_information: Some(vec![lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: Uri::from_file_path(path!("/dir/a.ts")).unwrap(),
+ range: supporting_range,
+ },
+ message: "supporting diagnostic".to_string(),
+ }]),
+ ..Default::default()
+ },
+ // With the severity and related information of the supporting diagnostic.
+ lsp::Diagnostic {
+ range: supporting_range,
+ severity: Some(DiagnosticSeverity::WARNING),
+ code: Some(lsp::NumberOrString::String("test-code".to_string())),
+ source: Some("test-language-server".to_string()),
+ message: "supporting diagnostic".to_string(),
+ related_information: Some(vec![lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: Uri::from_file_path(path!("/dir/a.ts")).unwrap(),
+ range: primary_range,
+ },
+ message: "primary diagnostic".to_string(),
+ }]),
+ ..Default::default()
+ },
+ ]
+ );
+ Ok(Some(Vec::new()))
+ },
+ );
+
+ let code_actions_task = project.update(cx, |project, cx| {
+ project.code_actions(&buffer, 0..4, None, cx)
+ });
+
+ request_handled
+ .next()
+ .await
+ .expect("The code action request should have been triggered");
+ assert!(code_actions_task.await.unwrap().unwrap().is_empty());
+}
+
+#[gpui::test]
+async fn test_code_actions_with_related_information_from_multiple_servers(
+ cx: &mut gpui::TestAppContext,
+) {
+ let (project, buffer, _handle, fake_servers) =
+ code_action_project(&["first-language-server", "second-language-server"], cx).await;
+
+ let uri = Uri::from_file_path(path!("/dir/a.ts")).unwrap();
+ for (index, fake_server) in fake_servers.iter().enumerate() {
+ let name = fake_server.server.name();
+ let column = index as u32 * 2;
+ fake_server.notify::<lsp::notification::PublishDiagnostics>(
+ lsp::PublishDiagnosticsParams {
+ uri: uri.clone(),
+ version: None,
+ diagnostics: vec![lsp::Diagnostic {
+ range: lsp::Range::new(
+ lsp::Position::new(0, column),
+ lsp::Position::new(0, column + 1),
+ ),
+ severity: Some(DiagnosticSeverity::ERROR),
+ source: Some(name.to_string()),
+ message: format!("{name} primary diagnostic"),
+ related_information: Some(vec![lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: uri.clone(),
+ range: lsp::Range::new(
+ lsp::Position::new(0, column + 1),
+ lsp::Position::new(0, column + 2),
+ ),
+ },
+ message: format!("{name} related diagnostic"),
+ }]),
+ ..Default::default()
+ }],
+ },
+ );
+ }
+ cx.executor().run_until_parked();
+
+ // Both servers receive the same list, each primary with its own related information.
+ let expected_diagnostics = fake_servers
+ .iter()
+ .enumerate()
+ .flat_map(|(index, fake_server)| {
+ let name = fake_server.server.name();
+ let column = index as u32 * 2;
+ [
+ lsp::Diagnostic {
+ range: lsp::Range::new(
+ lsp::Position::new(0, column),
+ lsp::Position::new(0, column + 1),
+ ),
+ severity: Some(DiagnosticSeverity::ERROR),
+ source: Some(name.to_string()),
+ message: format!("{name} primary diagnostic"),
+ related_information: Some(vec![lsp::DiagnosticRelatedInformation {
+ location: lsp::Location {
+ uri: uri.clone(),
+ range: lsp::Range::new(
+ lsp::Position::new(0, column + 1),
+ lsp::Position::new(0, column + 2),
+ ),
+ },
+ message: format!("{name} related diagnostic"),
+ }]),
+ ..Default::default()
+ },
+ lsp::Diagnostic {
+ range: lsp::Range::new(
+ lsp::Position::new(0, column + 1),
+ lsp::Position::new(0, column + 2),
+ ),
+ severity: Some(DiagnosticSeverity::INFORMATION),
+ source: Some(name.to_string()),
+ message: format!("{name} related diagnostic"),
+ ..Default::default()
+ },
+ ]
+ })
+ .collect::<Vec<_>>();
+
+ let mut requests_handled = fake_servers
+ .iter()
+ .map(|fake_server| {
+ let expected_diagnostics = expected_diagnostics.clone();
+ fake_server.set_request_handler::<lsp::request::CodeActionRequest, _, _>(
+ move |params, _| {
+ let expected_diagnostics = expected_diagnostics.clone();
+ async move {
+ assert_eq!(params.context.diagnostics, expected_diagnostics);
+ Ok(Some(Vec::new()))
+ }
+ },
+ )
+ })
+ .collect::<Vec<_>>();
+
+ let code_actions_task = project.update(cx, |project, cx| {
+ project.code_actions(&buffer, 0..4, None, cx)
+ });
+
+ for request_handled in &mut requests_handled {
+ request_handled
+ .next()
+ .await
+ .expect("The code action request should have been triggered");
+ }
+ assert!(code_actions_task.await.unwrap().unwrap().is_empty());
+}
+
#[gpui::test]
async fn test_code_actions_without_requested_kinds_do_not_send_only_filter(
cx: &mut gpui::TestAppContext,
@@ -16856,3 +17285,61 @@ async fn test_staging_hunks_with_ambiguous_placement(cx: &mut gpui::TestAppConte
]
);
}
+
+async fn code_action_project(
+ server_names: &[&'static str],
+ cx: &mut gpui::TestAppContext,
+) -> (
+ Entity<Project>,
+ Entity<Buffer>,
+ project::lsp_store::OpenLspBufferHandle,
+ Vec<lsp::FakeLanguageServer>,
+) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/dir"),
+ json!({
+ "a.ts": "abcd",
+ "b.ts": "efgh",
+ }),
+ )
+ .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(typescript_lang());
+ let mut fake_language_servers = server_names
+ .iter()
+ .map(|name| {
+ language_registry.register_fake_lsp(
+ "TypeScript",
+ FakeLspAdapter {
+ name,
+ capabilities: lsp::ServerCapabilities {
+ code_action_provider: Some(lsp::CodeActionProviderCapability::Simple(true)),
+ ..lsp::ServerCapabilities::default()
+ },
+ ..FakeLspAdapter::default()
+ },
+ )
+ })
+ .collect::<Vec<_>>();
+
+ let (buffer, handle) = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer_with_lsp(path!("/dir/a.ts"), cx)
+ })
+ .await
+ .unwrap();
+
+ let mut fake_servers = Vec::with_capacity(server_names.len());
+ for fake_language_servers in &mut fake_language_servers {
+ fake_servers.push(fake_language_servers.next().await.unwrap());
+ }
+ cx.executor().run_until_parked();
+
+ (project, buffer, handle, fake_servers)
+}