From 509d362bdd76b2e50c69c8f0963d61d1f6319e01 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 26 Jan 2022 11:51:12 -0800 Subject: [PATCH] Avoid stripping newlines before opening backticks in error messages --- crates/zed/src/language.rs | 50 +++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/crates/zed/src/language.rs b/crates/zed/src/language.rs index 90ce03f57a..4901f6ceaa 100644 --- a/crates/zed/src/language.rs +++ b/crates/zed/src/language.rs @@ -14,7 +14,7 @@ struct RustDiagnosticProcessor; impl DiagnosticProcessor for RustDiagnosticProcessor { fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) { lazy_static! { - static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`").unwrap(); + static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap(); } for diagnostic in &mut params.diagnostics { @@ -69,3 +69,51 @@ fn load_query(path: &str) -> Cow<'static, str> { Cow::Owned(s) => Cow::Owned(String::from_utf8(s).unwrap()), } } + +#[cfg(test)] +mod tests { + use language::DiagnosticProcessor; + + use super::RustDiagnosticProcessor; + + #[test] + fn test_process_rust_diagnostics() { + let mut params = lsp::PublishDiagnosticsParams { + uri: lsp::Url::from_file_path("/a").unwrap(), + version: None, + diagnostics: vec![ + // no newlines + lsp::Diagnostic { + message: "use of moved value `a`".to_string(), + ..Default::default() + }, + // newline at the end of a code span + lsp::Diagnostic { + message: "consider importing this struct: `use b::c;\n`".to_string(), + ..Default::default() + }, + // code span starting right after a newline + lsp::Diagnostic { + message: "cannot borrow `self.d` as mutable\n`self` is a `&` reference" + .to_string(), + ..Default::default() + }, + ], + }; + RustDiagnosticProcessor.process_diagnostics(&mut params); + + assert_eq!(params.diagnostics[0].message, "use of moved value `a`"); + + // remove trailing newline from code span + assert_eq!( + params.diagnostics[1].message, + "consider importing this struct: `use b::c;`" + ); + + // do not remove newline before the start of code span + assert_eq!( + params.diagnostics[2].message, + "cannot borrow `self.d` as mutable\n`self` is a `&` reference" + ); + } +}