Add an action to expand Rust macros with rust-analyzer (#3580)
Deals with https://github.com/zed-industries/community/issues/1282 If rust-analyzer responds with non-empty message, opens a new singleton multibuffer with the expansion result:  In gpui2, only editors with excerpts containing Rust languages get the action, in gpui1 every editor (even the *.ts one) has this action enabled but it does nothing. Release Notes: - Added an `editor::ExpandMacroRecursively` command to expand Rust macros with rust-analyzer.
This commit is contained in:
commit
cdf7c10b57
15 changed files with 550 additions and 14 deletions
|
@ -12,6 +12,7 @@ mod link_go_to_definition;
|
||||||
mod mouse_context_menu;
|
mod mouse_context_menu;
|
||||||
pub mod movement;
|
pub mod movement;
|
||||||
mod persistence;
|
mod persistence;
|
||||||
|
mod rust_analyzer_ext;
|
||||||
pub mod scroll;
|
pub mod scroll;
|
||||||
pub mod selections_collection;
|
pub mod selections_collection;
|
||||||
|
|
||||||
|
@ -300,6 +301,7 @@ actions!(
|
||||||
DeleteToEndOfLine,
|
DeleteToEndOfLine,
|
||||||
CutToEndOfLine,
|
CutToEndOfLine,
|
||||||
DuplicateLine,
|
DuplicateLine,
|
||||||
|
ExpandMacroRecursively,
|
||||||
MoveLineUp,
|
MoveLineUp,
|
||||||
MoveLineDown,
|
MoveLineDown,
|
||||||
JoinLines,
|
JoinLines,
|
||||||
|
@ -425,6 +427,8 @@ pub fn init_settings(cx: &mut AppContext) {
|
||||||
|
|
||||||
pub fn init(cx: &mut AppContext) {
|
pub fn init(cx: &mut AppContext) {
|
||||||
init_settings(cx);
|
init_settings(cx);
|
||||||
|
|
||||||
|
rust_analyzer_ext::apply_related_actions(cx);
|
||||||
cx.add_action(Editor::new_file);
|
cx.add_action(Editor::new_file);
|
||||||
cx.add_action(Editor::new_file_in_direction);
|
cx.add_action(Editor::new_file_in_direction);
|
||||||
cx.add_action(Editor::cancel);
|
cx.add_action(Editor::cancel);
|
||||||
|
|
98
crates/editor/src/rust_analyzer_ext.rs
Normal file
98
crates/editor/src/rust_analyzer_ext.rs
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::Context as _;
|
||||||
|
use gpui::{AppContext, Task, ViewContext};
|
||||||
|
use language::Language;
|
||||||
|
use multi_buffer::MultiBuffer;
|
||||||
|
use project::lsp_ext_command::ExpandMacro;
|
||||||
|
use text::ToPointUtf16;
|
||||||
|
|
||||||
|
use crate::{Editor, ExpandMacroRecursively};
|
||||||
|
|
||||||
|
pub fn apply_related_actions(cx: &mut AppContext) {
|
||||||
|
cx.add_async_action(expand_macro_recursively);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn expand_macro_recursively(
|
||||||
|
editor: &mut Editor,
|
||||||
|
_: &ExpandMacroRecursively,
|
||||||
|
cx: &mut ViewContext<'_, '_, Editor>,
|
||||||
|
) -> Option<Task<anyhow::Result<()>>> {
|
||||||
|
if editor.selections.count() == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let project = editor.project.as_ref()?;
|
||||||
|
let workspace = editor.workspace(cx)?;
|
||||||
|
let multibuffer = editor.buffer().read(cx);
|
||||||
|
|
||||||
|
let (trigger_anchor, rust_language, server_to_query, buffer) = editor
|
||||||
|
.selections
|
||||||
|
.disjoint_anchors()
|
||||||
|
.into_iter()
|
||||||
|
.filter(|selection| selection.start == selection.end)
|
||||||
|
.filter_map(|selection| Some((selection.start.buffer_id?, selection.start)))
|
||||||
|
.filter_map(|(buffer_id, trigger_anchor)| {
|
||||||
|
let buffer = multibuffer.buffer(buffer_id)?;
|
||||||
|
let rust_language = buffer.read(cx).language_at(trigger_anchor.text_anchor)?;
|
||||||
|
if !is_rust_language(&rust_language) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some((trigger_anchor, rust_language, buffer))
|
||||||
|
})
|
||||||
|
.find_map(|(trigger_anchor, rust_language, buffer)| {
|
||||||
|
project
|
||||||
|
.read(cx)
|
||||||
|
.language_servers_for_buffer(buffer.read(cx), cx)
|
||||||
|
.into_iter()
|
||||||
|
.find_map(|(adapter, server)| {
|
||||||
|
if adapter.name.0.as_ref() == "rust-analyzer" {
|
||||||
|
Some((
|
||||||
|
trigger_anchor,
|
||||||
|
Arc::clone(&rust_language),
|
||||||
|
server.server_id(),
|
||||||
|
buffer.clone(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let project = project.clone();
|
||||||
|
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||||
|
let position = trigger_anchor.text_anchor.to_point_utf16(&buffer_snapshot);
|
||||||
|
let expand_macro_task = project.update(cx, |project, cx| {
|
||||||
|
project.request_lsp(
|
||||||
|
buffer,
|
||||||
|
project::LanguageServerToQuery::Other(server_to_query),
|
||||||
|
ExpandMacro { position },
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
Some(cx.spawn(|_, mut cx| async move {
|
||||||
|
let macro_expansion = expand_macro_task.await.context("expand macro")?;
|
||||||
|
if macro_expansion.is_empty() {
|
||||||
|
log::info!("Empty macro expansion for position {position:?}");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let buffer = project.update(&mut cx, |project, cx| {
|
||||||
|
project.create_buffer(¯o_expansion.expansion, Some(rust_language), cx)
|
||||||
|
})?;
|
||||||
|
workspace.update(&mut cx, |workspace, cx| {
|
||||||
|
let buffer = cx.add_model(|cx| {
|
||||||
|
MultiBuffer::singleton(buffer, cx).with_title(macro_expansion.name)
|
||||||
|
});
|
||||||
|
workspace.add_item(
|
||||||
|
Box::new(cx.add_view(|cx| Editor::for_multibuffer(buffer, Some(project), cx))),
|
||||||
|
cx,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
anyhow::Ok(())
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_rust_language(language: &Language) -> bool {
|
||||||
|
language.name().as_ref() == "Rust"
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ mod link_go_to_definition;
|
||||||
mod mouse_context_menu;
|
mod mouse_context_menu;
|
||||||
pub mod movement;
|
pub mod movement;
|
||||||
mod persistence;
|
mod persistence;
|
||||||
|
mod rust_analyzer_ext;
|
||||||
pub mod scroll;
|
pub mod scroll;
|
||||||
pub mod selections_collection;
|
pub mod selections_collection;
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ use ui::{
|
||||||
use ui::{prelude::*, IconSize};
|
use ui::{prelude::*, IconSize};
|
||||||
use util::{post_inc, RangeExt, ResultExt, TryFutureExt};
|
use util::{post_inc, RangeExt, ResultExt, TryFutureExt};
|
||||||
use workspace::{
|
use workspace::{
|
||||||
item::{ItemEvent, ItemHandle},
|
item::{Item, ItemEvent, ItemHandle},
|
||||||
searchable::SearchEvent,
|
searchable::SearchEvent,
|
||||||
ItemNavHistory, Pane, SplitDirection, ViewId, Workspace,
|
ItemNavHistory, Pane, SplitDirection, ViewId, Workspace,
|
||||||
};
|
};
|
||||||
|
@ -329,6 +330,7 @@ actions!(
|
||||||
DeleteToPreviousSubwordStart,
|
DeleteToPreviousSubwordStart,
|
||||||
DeleteToPreviousWordStart,
|
DeleteToPreviousWordStart,
|
||||||
DuplicateLine,
|
DuplicateLine,
|
||||||
|
ExpandMacroRecursively,
|
||||||
FindAllReferences,
|
FindAllReferences,
|
||||||
Fold,
|
Fold,
|
||||||
FoldSelectedRanges,
|
FoldSelectedRanges,
|
||||||
|
@ -9341,7 +9343,6 @@ impl Render for Editor {
|
||||||
scrollbar_width: px(12.),
|
scrollbar_width: px(12.),
|
||||||
syntax: cx.theme().syntax().clone(),
|
syntax: cx.theme().syntax().clone(),
|
||||||
diagnostic_style: cx.theme().diagnostic_style(),
|
diagnostic_style: cx.theme().diagnostic_style(),
|
||||||
// TODO kb find `HighlightStyle` usages
|
|
||||||
// todo!("what about the rest of the highlight style parts?")
|
// todo!("what about the rest of the highlight style parts?")
|
||||||
inlays_style: HighlightStyle {
|
inlays_style: HighlightStyle {
|
||||||
color: Some(cx.theme().status().hint),
|
color: Some(cx.theme().status().hint),
|
||||||
|
|
|
@ -32,7 +32,7 @@ use gpui::{
|
||||||
Style, Styled, TextRun, TextStyle, View, ViewContext, WeakView, WindowContext, WrappedLine,
|
Style, Styled, TextRun, TextStyle, View, ViewContext, WeakView, WindowContext, WrappedLine,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use language::language_settings::ShowWhitespaceSetting;
|
use language::{language_settings::ShowWhitespaceSetting, Language};
|
||||||
use multi_buffer::Anchor;
|
use multi_buffer::Anchor;
|
||||||
use project::{
|
use project::{
|
||||||
project_settings::{GitGutterSetting, ProjectSettings},
|
project_settings::{GitGutterSetting, ProjectSettings},
|
||||||
|
@ -135,11 +135,13 @@ impl EditorElement {
|
||||||
|
|
||||||
fn register_actions(&self, cx: &mut WindowContext) {
|
fn register_actions(&self, cx: &mut WindowContext) {
|
||||||
let view = &self.editor;
|
let view = &self.editor;
|
||||||
self.editor.update(cx, |editor, cx| {
|
view.update(cx, |editor, cx| {
|
||||||
for action in editor.editor_actions.iter() {
|
for action in editor.editor_actions.iter() {
|
||||||
(action)(cx)
|
(action)(cx)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
crate::rust_analyzer_ext::apply_related_actions(view, cx);
|
||||||
register_action(view, cx, Editor::move_left);
|
register_action(view, cx, Editor::move_left);
|
||||||
register_action(view, cx, Editor::move_right);
|
register_action(view, cx, Editor::move_right);
|
||||||
register_action(view, cx, Editor::move_down);
|
register_action(view, cx, Editor::move_down);
|
||||||
|
|
119
crates/editor2/src/rust_analyzer_ext.rs
Normal file
119
crates/editor2/src/rust_analyzer_ext.rs
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::Context as _;
|
||||||
|
use gpui::{Context, Model, View, ViewContext, VisualContext, WindowContext};
|
||||||
|
use language::Language;
|
||||||
|
use multi_buffer::MultiBuffer;
|
||||||
|
use project::lsp_ext_command::ExpandMacro;
|
||||||
|
use text::ToPointUtf16;
|
||||||
|
|
||||||
|
use crate::{element::register_action, Editor, ExpandMacroRecursively};
|
||||||
|
|
||||||
|
pub fn apply_related_actions(editor: &View<Editor>, cx: &mut WindowContext) {
|
||||||
|
let is_rust_related = editor.update(cx, |editor, cx| {
|
||||||
|
editor
|
||||||
|
.buffer()
|
||||||
|
.read(cx)
|
||||||
|
.all_buffers()
|
||||||
|
.iter()
|
||||||
|
.any(|b| match b.read(cx).language() {
|
||||||
|
Some(l) => is_rust_language(l),
|
||||||
|
None => false,
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if is_rust_related {
|
||||||
|
register_action(editor, cx, expand_macro_recursively);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn expand_macro_recursively(
|
||||||
|
editor: &mut Editor,
|
||||||
|
_: &ExpandMacroRecursively,
|
||||||
|
cx: &mut ViewContext<'_, Editor>,
|
||||||
|
) {
|
||||||
|
if editor.selections.count() == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let Some(project) = &editor.project else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some(workspace) = editor.workspace() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let multibuffer = editor.buffer().read(cx);
|
||||||
|
|
||||||
|
let Some((trigger_anchor, rust_language, server_to_query, buffer)) = editor
|
||||||
|
.selections
|
||||||
|
.disjoint_anchors()
|
||||||
|
.into_iter()
|
||||||
|
.filter(|selection| selection.start == selection.end)
|
||||||
|
.filter_map(|selection| Some((selection.start.buffer_id?, selection.start)))
|
||||||
|
.filter_map(|(buffer_id, trigger_anchor)| {
|
||||||
|
let buffer = multibuffer.buffer(buffer_id)?;
|
||||||
|
let rust_language = buffer.read(cx).language_at(trigger_anchor.text_anchor)?;
|
||||||
|
if !is_rust_language(&rust_language) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some((trigger_anchor, rust_language, buffer))
|
||||||
|
})
|
||||||
|
.find_map(|(trigger_anchor, rust_language, buffer)| {
|
||||||
|
project
|
||||||
|
.read(cx)
|
||||||
|
.language_servers_for_buffer(buffer.read(cx), cx)
|
||||||
|
.into_iter()
|
||||||
|
.find_map(|(adapter, server)| {
|
||||||
|
if adapter.name.0.as_ref() == "rust-analyzer" {
|
||||||
|
Some((
|
||||||
|
trigger_anchor,
|
||||||
|
Arc::clone(&rust_language),
|
||||||
|
server.server_id(),
|
||||||
|
buffer.clone(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let project = project.clone();
|
||||||
|
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||||
|
let position = trigger_anchor.text_anchor.to_point_utf16(&buffer_snapshot);
|
||||||
|
let expand_macro_task = project.update(cx, |project, cx| {
|
||||||
|
project.request_lsp(
|
||||||
|
buffer,
|
||||||
|
project::LanguageServerToQuery::Other(server_to_query),
|
||||||
|
ExpandMacro { position },
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
cx.spawn(|editor, mut cx| async move {
|
||||||
|
let macro_expansion = expand_macro_task.await.context("expand macro")?;
|
||||||
|
if macro_expansion.is_empty() {
|
||||||
|
log::info!("Empty macro expansion for position {position:?}");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let buffer = project.update(&mut cx, |project, cx| {
|
||||||
|
project.create_buffer(¯o_expansion.expansion, Some(rust_language), cx)
|
||||||
|
})??;
|
||||||
|
workspace.update(&mut cx, |workspace, cx| {
|
||||||
|
let buffer = cx.build_model(|cx| {
|
||||||
|
MultiBuffer::singleton(buffer, cx).with_title(macro_expansion.name)
|
||||||
|
});
|
||||||
|
workspace.add_item(
|
||||||
|
Box::new(cx.build_view(|cx| Editor::for_multibuffer(buffer, Some(project), cx))),
|
||||||
|
cx,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.detach_and_log_err(cx);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_rust_language(language: &Language) -> bool {
|
||||||
|
language.name().as_ref() == "Rust"
|
||||||
|
}
|
|
@ -33,7 +33,7 @@ pub fn lsp_formatting_options(tab_size: u32) -> lsp::FormattingOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
pub(crate) trait LspCommand: 'static + Sized {
|
pub trait LspCommand: 'static + Sized {
|
||||||
type Response: 'static + Default + Send;
|
type Response: 'static + Default + Send;
|
||||||
type LspRequest: 'static + Send + lsp::request::Request;
|
type LspRequest: 'static + Send + lsp::request::Request;
|
||||||
type ProtoRequest: 'static + Send + proto::RequestMessage;
|
type ProtoRequest: 'static + Send + proto::RequestMessage;
|
||||||
|
|
137
crates/project/src/lsp_ext_command.rs
Normal file
137
crates/project/src/lsp_ext_command.rs
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
use std::{path::Path, sync::Arc};
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use gpui::{AppContext, AsyncAppContext, ModelHandle};
|
||||||
|
use language::{point_to_lsp, proto::deserialize_anchor, Buffer};
|
||||||
|
use lsp::{LanguageServer, LanguageServerId};
|
||||||
|
use rpc::proto::{self, PeerId};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use text::{PointUtf16, ToPointUtf16};
|
||||||
|
|
||||||
|
use crate::{lsp_command::LspCommand, Project};
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ExpandMacro {
|
||||||
|
pub position: PointUtf16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
|
impl LspCommand for ExpandMacro {
|
||||||
|
type Response = ExpandedMacro;
|
||||||
|
type LspRequest = LspExpandMacro;
|
||||||
|
type ProtoRequest = proto::LspExtExpandMacro;
|
||||||
|
|
||||||
|
fn to_lsp(
|
||||||
|
&self,
|
||||||
|
path: &Path,
|
||||||
|
_: &Buffer,
|
||||||
|
_: &Arc<LanguageServer>,
|
||||||
|
_: &AppContext,
|
||||||
|
) -> ExpandMacroParams {
|
||||||
|
ExpandMacroParams {
|
||||||
|
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<ExpandedMacro>,
|
||||||
|
_: ModelHandle<Project>,
|
||||||
|
_: ModelHandle<Buffer>,
|
||||||
|
_: LanguageServerId,
|
||||||
|
_: AsyncAppContext,
|
||||||
|
) -> 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(),
|
||||||
|
position: Some(language::proto::serialize_anchor(
|
||||||
|
&buffer.anchor_before(self.position),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn from_proto(
|
||||||
|
message: Self::ProtoRequest,
|
||||||
|
_: ModelHandle<Project>,
|
||||||
|
buffer: ModelHandle<Buffer>,
|
||||||
|
mut cx: AsyncAppContext,
|
||||||
|
) -> 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 Project,
|
||||||
|
_: PeerId,
|
||||||
|
_: &clock::Global,
|
||||||
|
_: &mut AppContext,
|
||||||
|
) -> proto::LspExtExpandMacroResponse {
|
||||||
|
proto::LspExtExpandMacroResponse {
|
||||||
|
name: response.name,
|
||||||
|
expansion: response.expansion,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn response_from_proto(
|
||||||
|
self,
|
||||||
|
message: proto::LspExtExpandMacroResponse,
|
||||||
|
_: ModelHandle<Project>,
|
||||||
|
_: ModelHandle<Buffer>,
|
||||||
|
_: AsyncAppContext,
|
||||||
|
) -> anyhow::Result<ExpandedMacro> {
|
||||||
|
Ok(ExpandedMacro {
|
||||||
|
name: message.name,
|
||||||
|
expansion: message.expansion,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn buffer_id_from_proto(message: &proto::LspExtExpandMacro) -> u64 {
|
||||||
|
message.buffer_id
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
mod ignore;
|
mod ignore;
|
||||||
mod lsp_command;
|
pub mod lsp_command;
|
||||||
|
pub mod lsp_ext_command;
|
||||||
mod prettier_support;
|
mod prettier_support;
|
||||||
pub mod project_settings;
|
pub mod project_settings;
|
||||||
pub mod search;
|
pub mod search;
|
||||||
|
@ -174,7 +175,7 @@ struct DelayedDebounced {
|
||||||
cancel_channel: Option<oneshot::Sender<()>>,
|
cancel_channel: Option<oneshot::Sender<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum LanguageServerToQuery {
|
pub enum LanguageServerToQuery {
|
||||||
Primary,
|
Primary,
|
||||||
Other(LanguageServerId),
|
Other(LanguageServerId),
|
||||||
}
|
}
|
||||||
|
@ -626,6 +627,7 @@ impl Project {
|
||||||
client.add_model_request_handler(Self::handle_open_buffer_by_path);
|
client.add_model_request_handler(Self::handle_open_buffer_by_path);
|
||||||
client.add_model_request_handler(Self::handle_save_buffer);
|
client.add_model_request_handler(Self::handle_save_buffer);
|
||||||
client.add_model_message_handler(Self::handle_update_diff_base);
|
client.add_model_message_handler(Self::handle_update_diff_base);
|
||||||
|
client.add_model_request_handler(Self::handle_lsp_command::<lsp_ext_command::ExpandMacro>);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn local(
|
pub fn local(
|
||||||
|
@ -5863,7 +5865,7 @@ impl Project {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_lsp<R: LspCommand>(
|
pub fn request_lsp<R: LspCommand>(
|
||||||
&self,
|
&self,
|
||||||
buffer_handle: ModelHandle<Buffer>,
|
buffer_handle: ModelHandle<Buffer>,
|
||||||
server: LanguageServerToQuery,
|
server: LanguageServerToQuery,
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub fn lsp_formatting_options(tab_size: u32) -> lsp::FormattingOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
pub(crate) trait LspCommand: 'static + Sized + Send {
|
pub trait LspCommand: 'static + Sized + Send {
|
||||||
type Response: 'static + Default + Send;
|
type Response: 'static + Default + Send;
|
||||||
type LspRequest: 'static + Send + lsp::request::Request;
|
type LspRequest: 'static + Send + lsp::request::Request;
|
||||||
type ProtoRequest: 'static + Send + proto::RequestMessage;
|
type ProtoRequest: 'static + Send + proto::RequestMessage;
|
||||||
|
|
137
crates/project2/src/lsp_ext_command.rs
Normal file
137
crates/project2/src/lsp_ext_command.rs
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
use std::{path::Path, sync::Arc};
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use gpui::{AppContext, AsyncAppContext, Model};
|
||||||
|
use language::{point_to_lsp, proto::deserialize_anchor, Buffer};
|
||||||
|
use lsp::{LanguageServer, LanguageServerId};
|
||||||
|
use rpc::proto::{self, PeerId};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use text::{PointUtf16, ToPointUtf16};
|
||||||
|
|
||||||
|
use crate::{lsp_command::LspCommand, Project};
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ExpandMacro {
|
||||||
|
pub position: PointUtf16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
|
impl LspCommand for ExpandMacro {
|
||||||
|
type Response = ExpandedMacro;
|
||||||
|
type LspRequest = LspExpandMacro;
|
||||||
|
type ProtoRequest = proto::LspExtExpandMacro;
|
||||||
|
|
||||||
|
fn to_lsp(
|
||||||
|
&self,
|
||||||
|
path: &Path,
|
||||||
|
_: &Buffer,
|
||||||
|
_: &Arc<LanguageServer>,
|
||||||
|
_: &AppContext,
|
||||||
|
) -> ExpandMacroParams {
|
||||||
|
ExpandMacroParams {
|
||||||
|
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<ExpandedMacro>,
|
||||||
|
_: Model<Project>,
|
||||||
|
_: Model<Buffer>,
|
||||||
|
_: LanguageServerId,
|
||||||
|
_: AsyncAppContext,
|
||||||
|
) -> 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(),
|
||||||
|
position: Some(language::proto::serialize_anchor(
|
||||||
|
&buffer.anchor_before(self.position),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn from_proto(
|
||||||
|
message: Self::ProtoRequest,
|
||||||
|
_: Model<Project>,
|
||||||
|
buffer: Model<Buffer>,
|
||||||
|
mut cx: AsyncAppContext,
|
||||||
|
) -> 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 Project,
|
||||||
|
_: PeerId,
|
||||||
|
_: &clock::Global,
|
||||||
|
_: &mut AppContext,
|
||||||
|
) -> proto::LspExtExpandMacroResponse {
|
||||||
|
proto::LspExtExpandMacroResponse {
|
||||||
|
name: response.name,
|
||||||
|
expansion: response.expansion,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn response_from_proto(
|
||||||
|
self,
|
||||||
|
message: proto::LspExtExpandMacroResponse,
|
||||||
|
_: Model<Project>,
|
||||||
|
_: Model<Buffer>,
|
||||||
|
_: AsyncAppContext,
|
||||||
|
) -> anyhow::Result<ExpandedMacro> {
|
||||||
|
Ok(ExpandedMacro {
|
||||||
|
name: message.name,
|
||||||
|
expansion: message.expansion,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn buffer_id_from_proto(message: &proto::LspExtExpandMacro) -> u64 {
|
||||||
|
message.buffer_id
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
mod ignore;
|
mod ignore;
|
||||||
mod lsp_command;
|
pub mod lsp_command;
|
||||||
|
pub mod lsp_ext_command;
|
||||||
mod prettier_support;
|
mod prettier_support;
|
||||||
pub mod project_settings;
|
pub mod project_settings;
|
||||||
pub mod search;
|
pub mod search;
|
||||||
|
@ -172,7 +173,7 @@ struct DelayedDebounced {
|
||||||
cancel_channel: Option<oneshot::Sender<()>>,
|
cancel_channel: Option<oneshot::Sender<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum LanguageServerToQuery {
|
pub enum LanguageServerToQuery {
|
||||||
Primary,
|
Primary,
|
||||||
Other(LanguageServerId),
|
Other(LanguageServerId),
|
||||||
}
|
}
|
||||||
|
@ -623,6 +624,7 @@ impl Project {
|
||||||
client.add_model_request_handler(Self::handle_open_buffer_by_path);
|
client.add_model_request_handler(Self::handle_open_buffer_by_path);
|
||||||
client.add_model_request_handler(Self::handle_save_buffer);
|
client.add_model_request_handler(Self::handle_save_buffer);
|
||||||
client.add_model_message_handler(Self::handle_update_diff_base);
|
client.add_model_message_handler(Self::handle_update_diff_base);
|
||||||
|
client.add_model_request_handler(Self::handle_lsp_command::<lsp_ext_command::ExpandMacro>);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn local(
|
pub fn local(
|
||||||
|
@ -5933,7 +5935,7 @@ impl Project {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_lsp<R: LspCommand>(
|
pub fn request_lsp<R: LspCommand>(
|
||||||
&self,
|
&self,
|
||||||
buffer_handle: Model<Buffer>,
|
buffer_handle: Model<Buffer>,
|
||||||
server: LanguageServerToQuery,
|
server: LanguageServerToQuery,
|
||||||
|
|
|
@ -178,7 +178,9 @@ message Envelope {
|
||||||
GetNotifications get_notifications = 150;
|
GetNotifications get_notifications = 150;
|
||||||
GetNotificationsResponse get_notifications_response = 151;
|
GetNotificationsResponse get_notifications_response = 151;
|
||||||
DeleteNotification delete_notification = 152;
|
DeleteNotification delete_notification = 152;
|
||||||
MarkNotificationRead mark_notification_read = 153; // Current max
|
MarkNotificationRead mark_notification_read = 153;
|
||||||
|
LspExtExpandMacro lsp_ext_expand_macro = 154;
|
||||||
|
LspExtExpandMacroResponse lsp_ext_expand_macro_response = 155; // Current max
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1619,3 +1621,14 @@ message Notification {
|
||||||
bool is_read = 6;
|
bool is_read = 6;
|
||||||
optional bool response = 7;
|
optional bool response = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message LspExtExpandMacro {
|
||||||
|
uint64 project_id = 1;
|
||||||
|
uint64 buffer_id = 2;
|
||||||
|
Anchor position = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LspExtExpandMacroResponse {
|
||||||
|
string name = 1;
|
||||||
|
string expansion = 2;
|
||||||
|
}
|
||||||
|
|
|
@ -280,6 +280,8 @@ messages!(
|
||||||
(UpdateWorktree, Foreground),
|
(UpdateWorktree, Foreground),
|
||||||
(UpdateWorktreeSettings, Foreground),
|
(UpdateWorktreeSettings, Foreground),
|
||||||
(UsersResponse, Foreground),
|
(UsersResponse, Foreground),
|
||||||
|
(LspExtExpandMacro, Background),
|
||||||
|
(LspExtExpandMacroResponse, Background),
|
||||||
);
|
);
|
||||||
|
|
||||||
request_messages!(
|
request_messages!(
|
||||||
|
@ -363,6 +365,7 @@ request_messages!(
|
||||||
(UpdateParticipantLocation, Ack),
|
(UpdateParticipantLocation, Ack),
|
||||||
(UpdateProject, Ack),
|
(UpdateProject, Ack),
|
||||||
(UpdateWorktree, Ack),
|
(UpdateWorktree, Ack),
|
||||||
|
(LspExtExpandMacro, LspExtExpandMacroResponse),
|
||||||
);
|
);
|
||||||
|
|
||||||
entity_messages!(
|
entity_messages!(
|
||||||
|
@ -415,6 +418,7 @@ entity_messages!(
|
||||||
UpdateProjectCollaborator,
|
UpdateProjectCollaborator,
|
||||||
UpdateWorktree,
|
UpdateWorktree,
|
||||||
UpdateWorktreeSettings,
|
UpdateWorktreeSettings,
|
||||||
|
LspExtExpandMacro,
|
||||||
);
|
);
|
||||||
|
|
||||||
entity_messages!(
|
entity_messages!(
|
||||||
|
|
|
@ -178,7 +178,9 @@ message Envelope {
|
||||||
GetNotifications get_notifications = 150;
|
GetNotifications get_notifications = 150;
|
||||||
GetNotificationsResponse get_notifications_response = 151;
|
GetNotificationsResponse get_notifications_response = 151;
|
||||||
DeleteNotification delete_notification = 152;
|
DeleteNotification delete_notification = 152;
|
||||||
MarkNotificationRead mark_notification_read = 153; // Current max
|
MarkNotificationRead mark_notification_read = 153;
|
||||||
|
LspExtExpandMacro lsp_ext_expand_macro = 154;
|
||||||
|
LspExtExpandMacroResponse lsp_ext_expand_macro_response = 155; // Current max
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1619,3 +1621,14 @@ message Notification {
|
||||||
bool is_read = 6;
|
bool is_read = 6;
|
||||||
optional bool response = 7;
|
optional bool response = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message LspExtExpandMacro {
|
||||||
|
uint64 project_id = 1;
|
||||||
|
uint64 buffer_id = 2;
|
||||||
|
Anchor position = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LspExtExpandMacroResponse {
|
||||||
|
string name = 1;
|
||||||
|
string expansion = 2;
|
||||||
|
}
|
||||||
|
|
|
@ -280,6 +280,8 @@ messages!(
|
||||||
(UpdateWorktree, Foreground),
|
(UpdateWorktree, Foreground),
|
||||||
(UpdateWorktreeSettings, Foreground),
|
(UpdateWorktreeSettings, Foreground),
|
||||||
(UsersResponse, Foreground),
|
(UsersResponse, Foreground),
|
||||||
|
(LspExtExpandMacro, Background),
|
||||||
|
(LspExtExpandMacroResponse, Background),
|
||||||
);
|
);
|
||||||
|
|
||||||
request_messages!(
|
request_messages!(
|
||||||
|
@ -363,6 +365,7 @@ request_messages!(
|
||||||
(UpdateParticipantLocation, Ack),
|
(UpdateParticipantLocation, Ack),
|
||||||
(UpdateProject, Ack),
|
(UpdateProject, Ack),
|
||||||
(UpdateWorktree, Ack),
|
(UpdateWorktree, Ack),
|
||||||
|
(LspExtExpandMacro, LspExtExpandMacroResponse),
|
||||||
);
|
);
|
||||||
|
|
||||||
entity_messages!(
|
entity_messages!(
|
||||||
|
@ -415,6 +418,7 @@ entity_messages!(
|
||||||
UpdateProjectCollaborator,
|
UpdateProjectCollaborator,
|
||||||
UpdateWorktree,
|
UpdateWorktree,
|
||||||
UpdateWorktreeSettings,
|
UpdateWorktreeSettings,
|
||||||
|
LspExtExpandMacro,
|
||||||
);
|
);
|
||||||
|
|
||||||
entity_messages!(
|
entity_messages!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue