Merge clangd's inactiveRegions with existing diagnostics (#26737)

Closes: https://github.com/zed-industries/zed/issues/13089

This PR attempts to resolve the issues discussed in my previous PR
#26146.

Release Notes:

- Fixed: `inactiveRegions` doesn't replace existing diagnostics anymore
This commit is contained in:
Naim A. 2025-03-25 15:13:53 +02:00 committed by GitHub
parent 8f1023360d
commit d9dcc59334
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 164 additions and 23 deletions

View file

@ -4,7 +4,8 @@ use futures::StreamExt;
use gpui::AsyncApp;
use http_client::github::{latest_github_release, GitHubLspBinaryVersion};
pub use language::*;
use lsp::{InitializeParams, LanguageServerBinary, LanguageServerName};
use lsp::{DiagnosticTag, InitializeParams, LanguageServerBinary, LanguageServerName};
use project::lsp_store::clangd_ext;
use serde_json::json;
use smol::fs::{self, File};
use std::{any::Any, env::consts, path::PathBuf, sync::Arc};
@ -279,10 +280,9 @@ impl super::LspAdapter for CLspAdapter {
// enable clangd's dot-to-arrow feature.
"editsNearCursor": true
},
// TODO: inactiveRegions support needs an implementation in clangd_ext.rs
// "inactiveRegionsCapabilities": {
// "inactiveRegions": true,
// }
"inactiveRegionsCapabilities": {
"inactiveRegions": true,
}
}
});
if let Some(ref mut original_experimental) = original.capabilities.experimental {
@ -292,6 +292,40 @@ impl super::LspAdapter for CLspAdapter {
}
Ok(original)
}
fn process_diagnostics(
&self,
params: &mut lsp::PublishDiagnosticsParams,
server_id: LanguageServerId,
buffer_access: Option<&'_ Buffer>,
) {
if let Some(buffer) = buffer_access {
let snapshot = buffer.snapshot();
let inactive_regions = buffer
.get_diagnostics(server_id)
.into_iter()
.flat_map(|v| v.iter())
.filter(|diag| clangd_ext::is_inactive_region(&diag.diagnostic))
.map(move |diag| {
let range =
language::range_to_lsp(diag.range.to_point_utf16(&snapshot)).unwrap();
let mut tags = vec![];
if diag.diagnostic.is_unnecessary {
tags.push(DiagnosticTag::UNNECESSARY);
}
lsp::Diagnostic {
range,
severity: Some(diag.diagnostic.severity),
source: diag.diagnostic.source.clone(),
tags: Some(tags),
message: diag.diagnostic.message.clone(),
code: diag.diagnostic.code.clone(),
..Default::default()
}
});
params.diagnostics.extend(inactive_regions);
}
}
}
async fn get_cached_server_binary(container_dir: PathBuf) -> Option<LanguageServerBinary> {

View file

@ -255,7 +255,12 @@ impl LspAdapter for RustLspAdapter {
Some("rust-analyzer/flycheck".into())
}
fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
fn process_diagnostics(
&self,
params: &mut lsp::PublishDiagnosticsParams,
_: LanguageServerId,
_: Option<&'_ Buffer>,
) {
static REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?m)`([^`]+)\n`$").expect("Failed to create REGEX"));
@ -959,7 +964,7 @@ mod tests {
},
],
};
RustLspAdapter.process_diagnostics(&mut params);
RustLspAdapter.process_diagnostics(&mut params, LanguageServerId(0), None);
assert_eq!(params.diagnostics[0].message, "use of moved value `a`");