Sanitize language server diagnostics coming from Rust

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra
2022-01-25 17:49:50 +01:00
co-authored by Nathan Sobo
parent 7250974aa6
commit 121b45e249
5 changed files with 59 additions and 13 deletions
+17
View File
@@ -39,6 +39,10 @@ pub trait ToPointUtf16 {
fn to_point_utf16(self) -> PointUtf16;
}
pub trait DiagnosticProcessor: 'static + Send + Sync {
fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
}
#[derive(Default, Deserialize)]
pub struct LanguageConfig {
pub name: String,
@@ -69,6 +73,7 @@ pub struct BracketPair {
pub struct Language {
pub(crate) config: LanguageConfig,
pub(crate) grammar: Option<Arc<Grammar>>,
pub(crate) diagnostic_processor: Option<Box<dyn DiagnosticProcessor>>,
}
pub struct Grammar {
@@ -135,6 +140,7 @@ impl Language {
highlight_map: Default::default(),
})
}),
diagnostic_processor: None,
}
}
@@ -178,6 +184,11 @@ impl Language {
Ok(self)
}
pub fn with_diagnostics_processor(mut self, processor: impl DiagnosticProcessor) -> Self {
self.diagnostic_processor = Some(Box::new(processor));
self
}
pub fn name(&self) -> &str {
self.config.name.as_str()
}
@@ -225,6 +236,12 @@ impl Language {
.and_then(|config| config.disk_based_diagnostics_progress_token.as_ref())
}
pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
if let Some(processor) = self.diagnostic_processor.as_ref() {
processor.process_diagnostics(diagnostics);
}
}
pub fn brackets(&self) -> &[BracketPair] {
&self.config.brackets
}
+2 -1
View File
@@ -822,7 +822,8 @@ impl Project {
send.await.log_err();
}
}
LspEvent::DiagnosticsUpdate(params) => {
LspEvent::DiagnosticsUpdate(mut params) => {
language.process_diagnostics(&mut params);
this.update(&mut cx, |this, cx| {
this.update_diagnostics(params, &disk_based_sources, cx)
.log_err();
+1
View File
@@ -73,6 +73,7 @@ num_cpus = "1.13.0"
parking_lot = "0.11.1"
postage = { version = "0.4.1", features = ["futures-traits"] }
rand = "0.8.3"
regex = "1.5"
rsa = "0.4"
rust-embed = { version = "6.2", features = ["include-exclude"] }
serde = { version = "1", features = ["derive"] }
+27
View File
@@ -1,4 +1,6 @@
pub use language::*;
use lazy_static::lazy_static;
use regex::Regex;
use rust_embed::RustEmbed;
use std::borrow::Cow;
use std::{str, sync::Arc};
@@ -7,6 +9,30 @@ use std::{str, sync::Arc};
#[folder = "languages"]
struct LanguageDir;
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();
}
for diagnostic in &mut params.diagnostics {
for message in diagnostic
.related_information
.iter_mut()
.flatten()
.map(|info| &mut info.message)
.chain([&mut diagnostic.message])
{
if let Cow::Owned(sanitized) = REGEX.replace_all(message, "`$1`") {
*message = sanitized;
}
}
}
}
}
pub fn build_language_registry() -> LanguageRegistry {
let mut languages = LanguageRegistry::default();
languages.add(Arc::new(rust()));
@@ -26,6 +52,7 @@ fn rust() -> Language {
.unwrap()
.with_outline_query(load_query("rust/outline.scm").as_ref())
.unwrap()
.with_diagnostics_processor(RustDiagnosticProcessor)
}
fn markdown() -> Language {