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.  --------- Co-authored-by: Peter Tripp <peter@zed.dev> Co-authored-by: Kirill Bulatov <kirill@zed.dev>
This commit is contained in:
parent
af5af9d7c5
commit
829ecda370
9 changed files with 174 additions and 82 deletions
75
crates/project/src/lsp_store/clangd_ext.rs
Normal file
75
crates/project/src/lsp_store/clangd_ext.rs
Normal 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();
|
||||
}
|
365
crates/project/src/lsp_store/lsp_ext_command.rs
Normal file
365
crates/project/src/lsp_store/lsp_ext_command.rs
Normal file
|
@ -0,0 +1,365 @@
|
|||
use crate::{lsp_command::LspCommand, lsp_store::LspStore, make_text_document_identifier};
|
||||
use anyhow::{Context as _, Result};
|
||||
use async_trait::async_trait;
|
||||
use gpui::{App, AsyncApp, Entity};
|
||||
use language::{point_to_lsp, proto::deserialize_anchor, Buffer};
|
||||
use lsp::{LanguageServer, LanguageServerId};
|
||||
use rpc::proto::{self, PeerId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{path::Path, sync::Arc};
|
||||
use text::{BufferId, PointUtf16, ToPointUtf16};
|
||||
|
||||
pub enum LspExpandMacro {}
|
||||
|
||||
impl lsp::request::Request for LspExpandMacro {
|
||||
type Params = ExpandMacroParams;
|
||||
type Result = Option<ExpandedMacro>;
|
||||
const METHOD: &'static str = "rust-analyzer/expandMacro";
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ExpandMacroParams {
|
||||
pub text_document: lsp::TextDocumentIdentifier,
|
||||
pub position: lsp::Position,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ExpandedMacro {
|
||||
pub name: String,
|
||||
pub expansion: String,
|
||||
}
|
||||
|
||||
impl ExpandedMacro {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.name.is_empty() && self.expansion.is_empty()
|
||||
}
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct ExpandMacro {
|
||||
pub position: PointUtf16,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl LspCommand for ExpandMacro {
|
||||
type Response = ExpandedMacro;
|
||||
type LspRequest = LspExpandMacro;
|
||||
type ProtoRequest = proto::LspExtExpandMacro;
|
||||
|
||||
fn display_name(&self) -> &str {
|
||||
"Expand macro"
|
||||
}
|
||||
|
||||
fn to_lsp(
|
||||
&self,
|
||||
path: &Path,
|
||||
_: &Buffer,
|
||||
_: &Arc<LanguageServer>,
|
||||
_: &App,
|
||||
) -> Result<ExpandMacroParams> {
|
||||
Ok(ExpandMacroParams {
|
||||
text_document: make_text_document_identifier(path)?,
|
||||
position: point_to_lsp(self.position),
|
||||
})
|
||||
}
|
||||
|
||||
async fn response_from_lsp(
|
||||
self,
|
||||
message: Option<ExpandedMacro>,
|
||||
_: Entity<LspStore>,
|
||||
_: Entity<Buffer>,
|
||||
_: LanguageServerId,
|
||||
_: AsyncApp,
|
||||
) -> anyhow::Result<ExpandedMacro> {
|
||||
Ok(message
|
||||
.map(|message| ExpandedMacro {
|
||||
name: message.name,
|
||||
expansion: message.expansion,
|
||||
})
|
||||
.unwrap_or_default())
|
||||
}
|
||||
|
||||
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::LspExtExpandMacro {
|
||||
proto::LspExtExpandMacro {
|
||||
project_id,
|
||||
buffer_id: buffer.remote_id().into(),
|
||||
position: Some(language::proto::serialize_anchor(
|
||||
&buffer.anchor_before(self.position),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn from_proto(
|
||||
message: Self::ProtoRequest,
|
||||
_: Entity<LspStore>,
|
||||
buffer: Entity<Buffer>,
|
||||
mut cx: AsyncApp,
|
||||
) -> anyhow::Result<Self> {
|
||||
let position = message
|
||||
.position
|
||||
.and_then(deserialize_anchor)
|
||||
.context("invalid position")?;
|
||||
Ok(Self {
|
||||
position: buffer.update(&mut cx, |buffer, _| position.to_point_utf16(buffer))?,
|
||||
})
|
||||
}
|
||||
|
||||
fn response_to_proto(
|
||||
response: ExpandedMacro,
|
||||
_: &mut LspStore,
|
||||
_: PeerId,
|
||||
_: &clock::Global,
|
||||
_: &mut App,
|
||||
) -> proto::LspExtExpandMacroResponse {
|
||||
proto::LspExtExpandMacroResponse {
|
||||
name: response.name,
|
||||
expansion: response.expansion,
|
||||
}
|
||||
}
|
||||
|
||||
async fn response_from_proto(
|
||||
self,
|
||||
message: proto::LspExtExpandMacroResponse,
|
||||
_: Entity<LspStore>,
|
||||
_: Entity<Buffer>,
|
||||
_: AsyncApp,
|
||||
) -> anyhow::Result<ExpandedMacro> {
|
||||
Ok(ExpandedMacro {
|
||||
name: message.name,
|
||||
expansion: message.expansion,
|
||||
})
|
||||
}
|
||||
|
||||
fn buffer_id_from_proto(message: &proto::LspExtExpandMacro) -> Result<BufferId> {
|
||||
BufferId::new(message.buffer_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum LspOpenDocs {}
|
||||
|
||||
impl lsp::request::Request for LspOpenDocs {
|
||||
type Params = OpenDocsParams;
|
||||
type Result = Option<DocsUrls>;
|
||||
const METHOD: &'static str = "experimental/externalDocs";
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct OpenDocsParams {
|
||||
pub text_document: lsp::TextDocumentIdentifier,
|
||||
pub position: lsp::Position,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DocsUrls {
|
||||
pub web: Option<String>,
|
||||
pub local: Option<String>,
|
||||
}
|
||||
|
||||
impl DocsUrls {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.web.is_none() && self.local.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OpenDocs {
|
||||
pub position: PointUtf16,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl LspCommand for OpenDocs {
|
||||
type Response = DocsUrls;
|
||||
type LspRequest = LspOpenDocs;
|
||||
type ProtoRequest = proto::LspExtOpenDocs;
|
||||
|
||||
fn display_name(&self) -> &str {
|
||||
"Open docs"
|
||||
}
|
||||
|
||||
fn to_lsp(
|
||||
&self,
|
||||
path: &Path,
|
||||
_: &Buffer,
|
||||
_: &Arc<LanguageServer>,
|
||||
_: &App,
|
||||
) -> Result<OpenDocsParams> {
|
||||
Ok(OpenDocsParams {
|
||||
text_document: lsp::TextDocumentIdentifier {
|
||||
uri: lsp::Url::from_file_path(path).unwrap(),
|
||||
},
|
||||
position: point_to_lsp(self.position),
|
||||
})
|
||||
}
|
||||
|
||||
async fn response_from_lsp(
|
||||
self,
|
||||
message: Option<DocsUrls>,
|
||||
_: Entity<LspStore>,
|
||||
_: Entity<Buffer>,
|
||||
_: LanguageServerId,
|
||||
_: AsyncApp,
|
||||
) -> anyhow::Result<DocsUrls> {
|
||||
Ok(message
|
||||
.map(|message| DocsUrls {
|
||||
web: message.web,
|
||||
local: message.local,
|
||||
})
|
||||
.unwrap_or_default())
|
||||
}
|
||||
|
||||
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::LspExtOpenDocs {
|
||||
proto::LspExtOpenDocs {
|
||||
project_id,
|
||||
buffer_id: buffer.remote_id().into(),
|
||||
position: Some(language::proto::serialize_anchor(
|
||||
&buffer.anchor_before(self.position),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn from_proto(
|
||||
message: Self::ProtoRequest,
|
||||
_: Entity<LspStore>,
|
||||
buffer: Entity<Buffer>,
|
||||
mut cx: AsyncApp,
|
||||
) -> anyhow::Result<Self> {
|
||||
let position = message
|
||||
.position
|
||||
.and_then(deserialize_anchor)
|
||||
.context("invalid position")?;
|
||||
Ok(Self {
|
||||
position: buffer.update(&mut cx, |buffer, _| position.to_point_utf16(buffer))?,
|
||||
})
|
||||
}
|
||||
|
||||
fn response_to_proto(
|
||||
response: DocsUrls,
|
||||
_: &mut LspStore,
|
||||
_: PeerId,
|
||||
_: &clock::Global,
|
||||
_: &mut App,
|
||||
) -> proto::LspExtOpenDocsResponse {
|
||||
proto::LspExtOpenDocsResponse {
|
||||
web: response.web,
|
||||
local: response.local,
|
||||
}
|
||||
}
|
||||
|
||||
async fn response_from_proto(
|
||||
self,
|
||||
message: proto::LspExtOpenDocsResponse,
|
||||
_: Entity<LspStore>,
|
||||
_: Entity<Buffer>,
|
||||
_: AsyncApp,
|
||||
) -> anyhow::Result<DocsUrls> {
|
||||
Ok(DocsUrls {
|
||||
web: message.web,
|
||||
local: message.local,
|
||||
})
|
||||
}
|
||||
|
||||
fn buffer_id_from_proto(message: &proto::LspExtOpenDocs) -> Result<BufferId> {
|
||||
BufferId::new(message.buffer_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum LspSwitchSourceHeader {}
|
||||
|
||||
impl lsp::request::Request for LspSwitchSourceHeader {
|
||||
type Params = SwitchSourceHeaderParams;
|
||||
type Result = Option<SwitchSourceHeaderResult>;
|
||||
const METHOD: &'static str = "textDocument/switchSourceHeader";
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SwitchSourceHeaderParams(lsp::TextDocumentIdentifier);
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SwitchSourceHeaderResult(pub String);
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SwitchSourceHeader;
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl LspCommand for SwitchSourceHeader {
|
||||
type Response = SwitchSourceHeaderResult;
|
||||
type LspRequest = LspSwitchSourceHeader;
|
||||
type ProtoRequest = proto::LspExtSwitchSourceHeader;
|
||||
|
||||
fn display_name(&self) -> &str {
|
||||
"Switch source header"
|
||||
}
|
||||
|
||||
fn to_lsp(
|
||||
&self,
|
||||
path: &Path,
|
||||
_: &Buffer,
|
||||
_: &Arc<LanguageServer>,
|
||||
_: &App,
|
||||
) -> Result<SwitchSourceHeaderParams> {
|
||||
Ok(SwitchSourceHeaderParams(make_text_document_identifier(
|
||||
path,
|
||||
)?))
|
||||
}
|
||||
|
||||
async fn response_from_lsp(
|
||||
self,
|
||||
message: Option<SwitchSourceHeaderResult>,
|
||||
_: Entity<LspStore>,
|
||||
_: Entity<Buffer>,
|
||||
_: LanguageServerId,
|
||||
_: AsyncApp,
|
||||
) -> anyhow::Result<SwitchSourceHeaderResult> {
|
||||
Ok(message
|
||||
.map(|message| SwitchSourceHeaderResult(message.0))
|
||||
.unwrap_or_default())
|
||||
}
|
||||
|
||||
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::LspExtSwitchSourceHeader {
|
||||
proto::LspExtSwitchSourceHeader {
|
||||
project_id,
|
||||
buffer_id: buffer.remote_id().into(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn from_proto(
|
||||
_: Self::ProtoRequest,
|
||||
_: Entity<LspStore>,
|
||||
_: Entity<Buffer>,
|
||||
_: AsyncApp,
|
||||
) -> anyhow::Result<Self> {
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
fn response_to_proto(
|
||||
response: SwitchSourceHeaderResult,
|
||||
_: &mut LspStore,
|
||||
_: PeerId,
|
||||
_: &clock::Global,
|
||||
_: &mut App,
|
||||
) -> proto::LspExtSwitchSourceHeaderResponse {
|
||||
proto::LspExtSwitchSourceHeaderResponse {
|
||||
target_file: response.0,
|
||||
}
|
||||
}
|
||||
|
||||
async fn response_from_proto(
|
||||
self,
|
||||
message: proto::LspExtSwitchSourceHeaderResponse,
|
||||
_: Entity<LspStore>,
|
||||
_: Entity<Buffer>,
|
||||
_: AsyncApp,
|
||||
) -> anyhow::Result<SwitchSourceHeaderResult> {
|
||||
Ok(SwitchSourceHeaderResult(message.target_file))
|
||||
}
|
||||
|
||||
fn buffer_id_from_proto(message: &proto::LspExtSwitchSourceHeader) -> Result<BufferId> {
|
||||
BufferId::new(message.buffer_id)
|
||||
}
|
||||
}
|
83
crates/project/src/lsp_store/rust_analyzer_ext.rs
Normal file
83
crates/project/src/lsp_store/rust_analyzer_ext.rs
Normal file
|
@ -0,0 +1,83 @@
|
|||
use ::serde::{Deserialize, Serialize};
|
||||
use gpui::{PromptLevel, WeakEntity};
|
||||
use lsp::LanguageServer;
|
||||
|
||||
use crate::{LanguageServerPromptRequest, LspStore, LspStoreEvent};
|
||||
|
||||
pub const RUST_ANALYZER_NAME: &str = "rust-analyzer";
|
||||
|
||||
/// Experimental: Informs the end user about the state of the server
|
||||
///
|
||||
/// [Rust Analyzer Specification](https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#server-status)
|
||||
#[derive(Debug)]
|
||||
enum ServerStatus {}
|
||||
|
||||
/// Other(String) variant to handle unknown values due to this still being experimental
|
||||
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum ServerHealthStatus {
|
||||
Ok,
|
||||
Warning,
|
||||
Error,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ServerStatusParams {
|
||||
pub health: ServerHealthStatus,
|
||||
pub message: Option<String>,
|
||||
}
|
||||
|
||||
impl lsp::notification::Notification for ServerStatus {
|
||||
type Params = ServerStatusParams;
|
||||
const METHOD: &'static str = "experimental/serverStatus";
|
||||
}
|
||||
|
||||
pub fn register_notifications(lsp_store: WeakEntity<LspStore>, language_server: &LanguageServer) {
|
||||
let name = language_server.name();
|
||||
let server_id = language_server.server_id();
|
||||
|
||||
let this = lsp_store;
|
||||
|
||||
language_server
|
||||
.on_notification::<ServerStatus, _>({
|
||||
let name = name.to_string();
|
||||
move |params, mut cx| {
|
||||
let this = this.clone();
|
||||
let name = name.to_string();
|
||||
if let Some(ref message) = params.message {
|
||||
let message = message.trim();
|
||||
if !message.is_empty() {
|
||||
let formatted_message = format!(
|
||||
"Language server {name} (id {server_id}) status update: {message}"
|
||||
);
|
||||
match params.health {
|
||||
ServerHealthStatus::Ok => log::info!("{}", formatted_message),
|
||||
ServerHealthStatus::Warning => log::warn!("{}", formatted_message),
|
||||
ServerHealthStatus::Error => {
|
||||
log::error!("{}", formatted_message);
|
||||
let (tx, _rx) = smol::channel::bounded(1);
|
||||
let request = LanguageServerPromptRequest {
|
||||
level: PromptLevel::Critical,
|
||||
message: params.message.unwrap_or_default(),
|
||||
actions: Vec::new(),
|
||||
response_channel: tx,
|
||||
lsp_name: name.clone(),
|
||||
};
|
||||
let _ = this
|
||||
.update(&mut cx, |_, cx| {
|
||||
cx.emit(LspStoreEvent::LanguageServerPrompt(request));
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
ServerHealthStatus::Other(status) => {
|
||||
log::info!("Unknown server health: {status}\n{formatted_message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue