clangd_ext.rs

 1use std::sync::Arc;
 2
 3use ::serde::{Deserialize, Serialize};
 4use gpui::WeakEntity;
 5use language::{CachedLspAdapter, Diagnostic};
 6use lsp::LanguageServer;
 7use util::ResultExt as _;
 8
 9use crate::LspStore;
10
11pub const CLANGD_SERVER_NAME: &str = "clangd";
12const INACTIVE_REGION_MESSAGE: &str = "inactive region";
13const INACTIVE_DIAGNOSTIC_SEVERITY: lsp::DiagnosticSeverity = lsp::DiagnosticSeverity::INFORMATION;
14
15#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
16#[serde(rename_all = "camelCase")]
17pub struct InactiveRegionsParams {
18    pub text_document: lsp::OptionalVersionedTextDocumentIdentifier,
19    pub regions: Vec<lsp::Range>,
20}
21
22/// InactiveRegions is a clangd extension that marks regions of inactive code.
23pub struct InactiveRegions;
24
25impl lsp::notification::Notification for InactiveRegions {
26    type Params = InactiveRegionsParams;
27    const METHOD: &'static str = "textDocument/inactiveRegions";
28}
29
30pub fn is_inactive_region(diag: &Diagnostic) -> bool {
31    diag.is_unnecessary
32        && diag.severity == INACTIVE_DIAGNOSTIC_SEVERITY
33        && diag.message == INACTIVE_REGION_MESSAGE
34        && diag
35            .source
36            .as_ref()
37            .is_some_and(|v| v == CLANGD_SERVER_NAME)
38}
39
40pub fn register_notifications(
41    lsp_store: WeakEntity<LspStore>,
42    language_server: &LanguageServer,
43    adapter: Arc<CachedLspAdapter>,
44) {
45    if language_server.name().0 != CLANGD_SERVER_NAME {
46        return;
47    }
48    let server_id = language_server.server_id();
49
50    language_server
51        .on_notification::<InactiveRegions, _>({
52            let adapter = adapter.clone();
53            let this = lsp_store;
54
55            move |params: InactiveRegionsParams, cx| {
56                let adapter = adapter.clone();
57                this.update(cx, |this, cx| {
58                    let diagnostics = params
59                        .regions
60                        .into_iter()
61                        .map(|range| lsp::Diagnostic {
62                            range,
63                            severity: Some(INACTIVE_DIAGNOSTIC_SEVERITY),
64                            source: Some(CLANGD_SERVER_NAME.to_string()),
65                            message: INACTIVE_REGION_MESSAGE.to_string(),
66                            tags: Some(vec![lsp::DiagnosticTag::UNNECESSARY]),
67                            ..lsp::Diagnostic::default()
68                        })
69                        .collect();
70                    let mapped_diagnostics = lsp::PublishDiagnosticsParams {
71                        uri: params.text_document.uri,
72                        version: params.text_document.version,
73                        diagnostics,
74                    };
75                    this.merge_diagnostics(
76                        server_id,
77                        mapped_diagnostics,
78                        &adapter.disk_based_diagnostic_sources,
79                        |diag, _| !is_inactive_region(diag),
80                        cx,
81                    )
82                    .log_err();
83                })
84                .ok();
85            }
86        })
87        .detach();
88}