Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
diagnostics: Add back links to nearby related diagnostics (#64134)
Gautam Manchandanibd61c4a2e3e6
diff --git a/crates/diagnostics/src/diagnostic_renderer.rs b/crates/diagnostics/src/diagnostic_renderer.rsindex 682f5b56172..903115f81b0 100644--- a/crates/diagnostics/src/diagnostic_renderer.rs+++ b/crates/diagnostics/src/diagnostic_renderer.rs@@ -64,11 +64,9 @@ impl DiagnosticRenderer {} else {append_source_and_code(&mut markdown, entry.diagnostic);- if entry.range.start.row.abs_diff(primary.range.start.row) >= 5 {- markdown.push_str(&format!(- " ([back](file://#diagnostic-{buffer_id}-{group_id}-{primary_ix}))"- ));- }+ markdown.push_str(&format!(+ " ([back](file://#diagnostic-{buffer_id}-{group_id}-{primary_ix}))"+ ));results.push(DiagnosticBlock {initial_range: entry.range.clone(),severity: entry.diagnostic.severity,diff --git a/crates/diagnostics/src/diagnostics_tests.rs b/crates/diagnostics/src/diagnostics_tests.rsindex 47d96d87917..a18719d55e2 100644--- a/crates/diagnostics/src/diagnostics_tests.rs+++ b/crates/diagnostics/src/diagnostics_tests.rs@@ -11,7 +11,7 @@ use editor::{};use gpui::{TestAppContext, VisualTestContext};use indoc::indoc;-use language::{DiagnosticSourceKind, Rope};+use language::{BufferId, Diagnostic, DiagnosticEntryRef, DiagnosticSourceKind, Rope};use lsp::LanguageServerId;use pretty_assertions::assert_eq;use project::{@@ -142,7 +142,7 @@ async fn test_diagnostics(cx: &mut TestAppContext) {§ move occurs because `y` has type `Vec<char>`, which does not implement§ the `Copy` trait (back)a(x); § value moved here (back)- b(y); § value moved here+ b(y); § value moved here (back)// comment 1// comment 2c(y);@@ -222,7 +222,7 @@ async fn test_diagnostics(cx: &mut TestAppContext) {§ move occurs because `y` has type `Vec<char>`, which does not implement§ the `Copy` trait (back)a(x); § value moved here (back)- b(y); § value moved here+ b(y); § value moved here (back)// comment 1// comment 2c(y);@@ -313,7 +313,7 @@ async fn test_diagnostics(cx: &mut TestAppContext) {§ move occurs because `y` has type `Vec<char>`, which does not implement§ the `Copy` trait (back)a(x); § value moved here (back)- b(y); § value moved here+ b(y); § value moved here (back)// comment 1// comment 2c(y);@@ -433,7 +433,7 @@ async fn test_diagnostics_with_folds(cx: &mut TestAppContext) {indoc::indoc! {"§ main.js§ ------ function test() { § method `test` defined here+ function test() { § method `test` defined here (back)return 1};@@ -1347,6 +1347,62 @@ async fn test_diagnostics_with_links(cx: &mut TestAppContext) {cx.update_editor(|editor, _, _| assert!(editor.hover_state.diagnostic_popover.is_some()))}+#[gpui::test]+fn test_nearby_related_diagnostic_links_back_to_primary(cx: &mut TestAppContext) {+ init_test(cx);++ let buffer_id = BufferId::new(1).expect("test buffer ID should be nonzero");+ let group_id = 1;+ let diagnostic_group = [+ DiagnosticEntryRef {+ range: text::Point::new(10, 4)..text::Point::new(10, 16),+ diagnostic: &Diagnostic {+ message: "the trait bound `(): HasTest` is not satisfied".into(),+ is_primary: true,+ group_id,+ ..Default::default()+ },+ },+ DiagnosticEntryRef {+ range: text::Point::new(13, 0)..text::Point::new(13, 22),+ diagnostic: &Diagnostic {+ message: "required by a bound in `Test::TestAssoc`".into(),+ group_id,+ ..Default::default()+ },+ },+ ];++ let primary_index = diagnostic_group+ .iter()+ .position(|entry| entry.diagnostic.is_primary)+ .expect("test diagnostic group should contain a primary diagnostic");+ let expected_back_link =+ format!("([back](file://#diagnostic-{buffer_id}-{group_id}-{primary_index}))");++ cx.update(|cx| {+ let blocks = diagnostic_renderer::DiagnosticRenderer::diagnostic_blocks_for_group(+ diagnostic_group.to_vec(),+ buffer_id,+ None,+ None,+ cx,+ );++ let [_primary_block, related_block] = blocks.as_slice() else {+ panic!("expected one primary and one related diagnostic block");+ };++ let markdown = related_block.markdown.read(cx).source();++ assert!(+ markdown.contains(&expected_back_link),+ "related diagnostic should link back to the primary diagnostic, got: {}",+ markdown+ );+ });+}+#[gpui::test]async fn test_markup_content_diagnostic_messages_render_as_markdown(cx: &mut TestAppContext) {init_test(cx);@@ -2085,9 +2141,9 @@ async fn test_buffer_diagnostics(cx: &mut TestAppContext) {§ the `Copy` trait (back)let y = vec![];§ move occurs because `y` has type `Vec<char>`, which does not implement- § the `Copy` trait- a(x); § value moved here- b(y); § value moved here+ § the `Copy` trait (back)+ a(x); § value moved here (back)+ b(y); § value moved here (back)c(y);§ use of moved value§ value used here after move@@ -2221,7 +2277,7 @@ async fn test_buffer_diagnostics_without_warnings(cx: &mut TestAppContext) {§ move occurs because `x` has type `Vec<char>`, which does not implement§ the `Copy` trait (back)let y = vec![];- a(x); § value moved here+ a(x); § value moved here (back)b(y);c(y);d(x);