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

@ -2,13 +2,14 @@ use std::sync::Arc;
use ::serde::{Deserialize, Serialize};
use gpui::WeakEntity;
use language::CachedLspAdapter;
use language::{CachedLspAdapter, Diagnostic};
use lsp::LanguageServer;
use util::ResultExt as _;
use crate::LspStore;
pub const CLANGD_SERVER_NAME: &str = "clangd";
const INACTIVE_REGION_MESSAGE: &str = "inactive region";
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
@ -25,6 +26,16 @@ impl lsp::notification::Notification for InactiveRegions {
const METHOD: &'static str = "textDocument/inactiveRegions";
}
pub fn is_inactive_region(diag: &Diagnostic) -> bool {
diag.is_unnecessary
&& diag.severity == lsp::DiagnosticSeverity::INFORMATION
&& diag.message == INACTIVE_REGION_MESSAGE
&& diag
.source
.as_ref()
.is_some_and(|v| v == CLANGD_SERVER_NAME)
}
pub fn register_notifications(
lsp_store: WeakEntity<LspStore>,
language_server: &LanguageServer,
@ -35,10 +46,6 @@ pub fn register_notifications(
}
let server_id = language_server.server_id();
// TODO: inactiveRegions support needs do add diagnostics, not replace them as `this.update_diagnostics` call below does
if true {
return;
}
language_server
.on_notification::<InactiveRegions, _>({
let adapter = adapter.clone();
@ -54,7 +61,7 @@ pub fn register_notifications(
range,
severity: Some(lsp::DiagnosticSeverity::INFORMATION),
source: Some(CLANGD_SERVER_NAME.to_string()),
message: "inactive region".to_string(),
message: INACTIVE_REGION_MESSAGE.to_string(),
tags: Some(vec![lsp::DiagnosticTag::UNNECESSARY]),
..Default::default()
})
@ -64,10 +71,11 @@ pub fn register_notifications(
version: params.text_document.version,
diagnostics,
};
this.update_diagnostics(
this.merge_diagnostics(
server_id,
mapped_diagnostics,
&adapter.disk_based_diagnostic_sources,
|diag| !is_inactive_region(diag),
cx,
)
.log_err();