lsp: Add support for clangd's inactiveRegions extension (#26146)

Closes #13089 

Here we use `experimental` to advertise our support for
`inactiveRegions`. Note that clangd does not currently have a stable
release that reads the `experimental` object (PR
https://github.com/llvm/llvm-project/pull/116531), this can be tested
with one of clangd's recent "unstable snapshots" in their
[releases](https://github.com/clangd/clangd/releases).

Release Notes:

- Added support for clangd's `inactiveRegions` extension.

![Screen Recording 2025-03-05 at 22 39
58](https://github.com/user-attachments/assets/ceade8bd-4d8e-43c3-9520-ad44efa50d2f)

---------

Co-authored-by: Peter Tripp <peter@zed.dev>
Co-authored-by: Kirill Bulatov <kirill@zed.dev>
This commit is contained in:
Naim A. 2025-03-06 23:30:05 +02:00 committed by GitHub
parent af5af9d7c5
commit 829ecda370
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 174 additions and 82 deletions

View file

@ -0,0 +1,75 @@
use std::sync::Arc;
use ::serde::{Deserialize, Serialize};
use gpui::WeakEntity;
use language::CachedLspAdapter;
use lsp::LanguageServer;
use util::ResultExt as _;
use crate::LspStore;
pub const CLANGD_SERVER_NAME: &str = "clangd";
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InactiveRegionsParams {
pub text_document: lsp::OptionalVersionedTextDocumentIdentifier,
pub regions: Vec<lsp::Range>,
}
/// InactiveRegions is a clangd extension that marks regions of inactive code.
pub struct InactiveRegions;
impl lsp::notification::Notification for InactiveRegions {
type Params = InactiveRegionsParams;
const METHOD: &'static str = "textDocument/inactiveRegions";
}
pub fn register_notifications(
lsp_store: WeakEntity<LspStore>,
language_server: &LanguageServer,
adapter: Arc<CachedLspAdapter>,
) {
if language_server.name().0 != CLANGD_SERVER_NAME {
return;
}
let server_id = language_server.server_id();
language_server
.on_notification::<InactiveRegions, _>({
let adapter = adapter.clone();
let this = lsp_store;
move |params: InactiveRegionsParams, mut cx| {
let adapter = adapter.clone();
this.update(&mut cx, |this, cx| {
let diagnostics = params
.regions
.into_iter()
.map(|range| lsp::Diagnostic {
range,
severity: Some(lsp::DiagnosticSeverity::INFORMATION),
source: Some(CLANGD_SERVER_NAME.to_string()),
message: "inactive region".to_string(),
tags: Some(vec![lsp::DiagnosticTag::UNNECESSARY]),
..Default::default()
})
.collect();
let mapped_diagnostics = lsp::PublishDiagnosticsParams {
uri: params.text_document.uri,
version: params.text_document.version,
diagnostics,
};
this.update_diagnostics(
server_id,
mapped_diagnostics,
&adapter.disk_based_diagnostic_sources,
cx,
)
.log_err();
})
.ok();
}
})
.detach();
}