Skip to content

Commit

Permalink
lsp: support clang inactionRegions extension
Browse files Browse the repository at this point in the history
this should close #13089
  • Loading branch information
naim94a committed Feb 27, 2025
1 parent be1ac78 commit dccbc03
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
41 changes: 39 additions & 2 deletions crates/lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::{
time::{Duration, Instant},
};
use std::{path::Path, process::Stdio};
use util::{ResultExt, TryFutureExt};
use util::{merge_non_null_json_value_into, ResultExt, TryFutureExt};

const JSON_RPC_VERSION: &str = "2.0";
const CONTENT_LEN_HEADER: &str = "Content-Length: ";
Expand Down Expand Up @@ -327,6 +327,21 @@ impl lsp_types::notification::Notification for ServerStatus {
const METHOD: &'static str = "experimental/serverStatus";
}

#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InactiveRegionsParams {
pub text_document: lsp_types::OptionalVersionedTextDocumentIdentifier,
pub regions: Vec<lsp_types::Range>,
}

/// InactiveRegions is a clangd extension that marks regions of inactive code.
pub struct InactiveRegions;

impl lsp_types::notification::Notification for InactiveRegions {
type Params = InactiveRegionsParams;
const METHOD: &'static str = "textDocument/inactiveRegions";
}

impl LanguageServer {
/// Starts a language server process.
pub fn new(
Expand Down Expand Up @@ -814,8 +829,30 @@ impl LanguageServer {
configuration: Arc<DidChangeConfigurationParams>,
cx: &App,
) -> Task<Result<Arc<Self>>> {
/// This is a drop-in replacement for `lsp_types::Initialize`, that allows us to insert non-standard objects into `Params`.
struct InitializeExtended;

impl request::Request for InitializeExtended {
type Params = serde_json::Value;
type Result = InitializeResult;
const METHOD: &'static str = Initialize::METHOD;
}
let mut params = serde_json::to_value(params).unwrap();
merge_non_null_json_value_into(
serde_json::json!({
"capabilities": {
"textDocument": {
"inactiveRegionsCapabilities": {
"inactiveRegions": true,
}
}
}
}),
&mut params,
);

cx.spawn(|_| async move {
let response = self.request::<request::Initialize>(params).await?;
let response = self.request::<InitializeExtended>(params).await?;
if let Some(info) = response.server_info {
self.process_name = info.name.into();
}
Expand Down
50 changes: 46 additions & 4 deletions crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ use lsp::{
notification::DidRenameFiles, CodeActionKind, CompletionContext, DiagnosticSeverity,
DiagnosticTag, DidChangeWatchedFilesRegistrationOptions, Edit, FileOperationFilter,
FileOperationPatternKind, FileOperationRegistrationOptions, FileRename, FileSystemWatcher,
InsertTextFormat, LanguageServer, LanguageServerBinary, LanguageServerBinaryOptions,
LanguageServerId, LanguageServerName, LspRequestFuture, MessageActionItem, MessageType, OneOf,
RenameFilesParams, ServerHealthStatus, ServerStatus, SymbolKind, TextEdit, WillRenameFiles,
WorkDoneProgressCancelParams, WorkspaceFolder,
InactiveRegions, InsertTextFormat, LanguageServer, LanguageServerBinary,
LanguageServerBinaryOptions, LanguageServerId, LanguageServerName, LspRequestFuture,
MessageActionItem, MessageType, OneOf, RenameFilesParams, ServerHealthStatus, ServerStatus,
SymbolKind, TextEdit, WillRenameFiles, WorkDoneProgressCancelParams, WorkspaceFolder,
};
use node_runtime::read_package_installed_version;
use parking_lot::Mutex;
Expand Down Expand Up @@ -479,6 +479,48 @@ impl LocalLspStore {
}
})
.detach();

// clang specific notification setup
if language_server.name().0 == "clangd" {
language_server
.on_notification::<InactiveRegions, _>({
let adapter = adapter.clone();
let this = this.clone();

move |params, 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".to_string()),
message: "inactive region".to_string(),
tags: Some(vec![lsp::DiagnosticTag::UNNECESSARY]),
..Default::default()
})
.collect();
let fake_lsp_diag = lsp::PublishDiagnosticsParams {
uri: params.text_document.uri,
version: params.text_document.version,
diagnostics,
};
this.update_diagnostics(
server_id,
fake_lsp_diag,
&adapter.disk_based_diagnostic_sources,
cx,
)
.log_err();
})
.ok();
}
})
.detach();
}

language_server
.on_request::<lsp::request::WorkspaceConfiguration, _, _>({
let adapter = adapter.adapter.clone();
Expand Down

0 comments on commit dccbc03

Please sign in to comment.