Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
project: Underline diagnostics reported past the end of a line (#63125)
Дмитрий Егоров9a76a39d7df6
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rsindex 96b8474bc4e..9f88a70f29d 100644--- a/crates/project/src/lsp_store.rs+++ b/crates/project/src/lsp_store.rs@@ -2882,6 +2882,27 @@ impl LocalLspStore {entry.range.start =snapshot.clip_point_utf16(Unclipped(entry.range.start), Bias::Left);}+ } else if entry.range.end == PointUtf16::new(entry.range.start.row + 1, 0) {+ // Some language servers (e.g. Python parsers reporting a missing `:`)+ // report a diagnostic whose range starts at the end of a line and ends+ // at the start of the next one. That range contains nothing but the+ // line terminator itself, so there is no glyph left to underline and+ // the diagnostic renders invisibly even though it's real. Only collapse+ // ranges that are exactly the terminator (checked via the precise end+ // position above) so genuinely multi-line diagnostics that merely start+ // at an end-of-line are left untouched.+ let line_end = snapshot.clip_point_utf16(+ Unclipped(PointUtf16::new(entry.range.start.row, u32::MAX)),+ Bias::Left,+ );+ if entry.range.start == line_end {+ entry.range.end = entry.range.start;+ if entry.range.start.column > 0 {+ entry.range.start.column -= 1;+ entry.range.start =+ snapshot.clip_point_utf16(Unclipped(entry.range.start), Bias::Left);+ }+ }}sanitized_diagnostics.push(entry);diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rsindex 04c7e3fc0d7..0818800e00b 100644--- a/crates/project/tests/integration/project_tests.rs+++ b/crates/project/tests/integration/project_tests.rs@@ -4180,6 +4180,71 @@ async fn test_empty_diagnostic_ranges(cx: &mut gpui::TestAppContext) {});}+#[gpui::test]+async fn test_diagnostic_range_spanning_line_terminator(cx: &mut gpui::TestAppContext) {+ init_test(cx);++ // Some language servers (e.g. Pyright, ruff, pyrefly, ty on a missing `:`)+ // report a syntax error whose range starts at the end of one line and ends+ // at the start of the next, i.e. it spans only the line terminator itself.+ let text = concat!(+ "def main()\n", //+ " pass\n",+ );++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(path!("/dir"), json!({ "a.py": text })).await;++ let project = Project::test(fs, [Path::new(path!("/dir"))], cx).await;+ let buffer = project+ .update(cx, |project, cx| {+ project.open_local_buffer(path!("/dir/a.py"), cx)+ })+ .await+ .unwrap();++ project.update(cx, |project, cx| {+ project.lsp_store().update(cx, |lsp_store, cx| {+ lsp_store+ .update_diagnostic_entries(+ LanguageServerId(0),+ PathBuf::from(path!("/dir/a.py")),+ None,+ None,+ vec![DiagnosticEntry::new(+ Unclipped(PointUtf16::new(0, 11))..Unclipped(PointUtf16::new(1, 0)),+ Diagnostic {+ severity: DiagnosticSeverity::ERROR,+ message: "Expected `:`".into(),+ source_kind: DiagnosticSourceKind::Pushed,+ ..Diagnostic::default()+ },+ )],+ cx,+ )+ .unwrap();+ })+ });++ // The range is pulled back onto the last character of the line it actually+ // concerns, instead of covering nothing but the newline, so the diagnostic+ // has a visible glyph to underline.+ buffer.update(cx, |buffer, _| {+ let chunks = chunks_with_diagnostics(buffer, 0..buffer.len());+ assert_eq!(+ chunks+ .iter()+ .map(|(s, d)| (s.as_str(), *d))+ .collect::<Vec<_>>(),+ &[+ ("def main(", None),+ (")", Some(DiagnosticSeverity::ERROR)),+ ("\n pass\n", None),+ ]+ );+ });+}+#[gpui::test]async fn test_diagnostics_from_multiple_language_servers(cx: &mut gpui::TestAppContext) {init_test(cx);