Draft an expand macro recusively command

This commit is contained in:
Kirill Bulatov 2023-12-11 02:29:32 +02:00
parent db8e58b888
commit e3fc810b3d
13 changed files with 216 additions and 11 deletions

View file

@ -997,6 +997,7 @@ pub mod tests {
movement, movement,
test::{editor_test_context::EditorTestContext, marked_display_snapshot}, test::{editor_test_context::EditorTestContext, marked_display_snapshot},
}; };
use client::Client;
use gpui::{div, font, observe, px, AppContext, Context, Element, Hsla}; use gpui::{div, font, observe, px, AppContext, Context, Element, Hsla};
use language::{ use language::{
language_settings::{AllLanguageSettings, AllLanguageSettingsContent}, language_settings::{AllLanguageSettings, AllLanguageSettingsContent},
@ -1008,7 +1009,10 @@ pub mod tests {
use smol::stream::StreamExt; use smol::stream::StreamExt;
use std::{env, sync::Arc}; use std::{env, sync::Arc};
use theme::{LoadThemes, SyntaxTheme}; use theme::{LoadThemes, SyntaxTheme};
use util::test::{marked_text_ranges, sample_text}; use util::{
http::FakeHttpClient,
test::{marked_text_ranges, sample_text},
};
use Bias::*; use Bias::*;
#[gpui::test(iterations = 100)] #[gpui::test(iterations = 100)]

View file

@ -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;
@ -73,7 +74,7 @@ pub use multi_buffer::{
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
use project::{FormatTrigger, Location, Project, ProjectPath, ProjectTransaction}; use project::{FormatTrigger, Location, Project, ProjectPath, ProjectTransaction};
use rand::prelude::*; use rand::{prelude::*, rngs::adapter};
use rpc::proto::{self, *}; use rpc::proto::{self, *};
use scroll::{ use scroll::{
autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide, autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide,
@ -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,

View file

@ -29,6 +29,7 @@ use std::{cell::RefCell, future::Future, rc::Rc, time::Instant};
use unindent::Unindent; use unindent::Unindent;
use util::{ use util::{
assert_set_eq, assert_set_eq,
http::FakeHttpClient,
test::{marked_text_ranges, marked_text_ranges_by, sample_text, TextRangeMarker}, test::{marked_text_ranges, marked_text_ranges_by, sample_text, TextRangeMarker},
}; };
use workspace::{ use workspace::{

View file

@ -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);

View file

@ -1202,6 +1202,7 @@ pub mod tests {
scroll::{autoscroll::Autoscroll, scroll_amount::ScrollAmount}, scroll::{autoscroll::Autoscroll, scroll_amount::ScrollAmount},
ExcerptRange, ExcerptRange,
}; };
use client::Client;
use futures::StreamExt; use futures::StreamExt;
use gpui::{Context, TestAppContext, View, WindowHandle}; use gpui::{Context, TestAppContext, View, WindowHandle};
use itertools::Itertools; use itertools::Itertools;
@ -1214,6 +1215,7 @@ pub mod tests {
use serde_json::json; use serde_json::json;
use settings::SettingsStore; use settings::SettingsStore;
use text::{Point, ToPoint}; use text::{Point, ToPoint};
use util::http::FakeHttpClient;
use workspace::Workspace; use workspace::Workspace;
use crate::editor_tests::update_test_language_settings; use crate::editor_tests::update_test_language_settings;

View file

@ -460,10 +460,11 @@ mod tests {
test::{editor_test_context::EditorTestContext, marked_display_snapshot}, test::{editor_test_context::EditorTestContext, marked_display_snapshot},
Buffer, DisplayMap, ExcerptRange, InlayId, MultiBuffer, Buffer, DisplayMap, ExcerptRange, InlayId, MultiBuffer,
}; };
use client::Client;
use gpui::{font, Context as _}; use gpui::{font, Context as _};
use project::Project; use project::Project;
use settings::SettingsStore; use settings::SettingsStore;
use util::post_inc; use util::{http::FakeHttpClient, post_inc};
#[gpui::test] #[gpui::test]
fn test_previous_word_start(cx: &mut gpui::AppContext) { fn test_previous_word_start(cx: &mut gpui::AppContext) {

View file

@ -0,0 +1,75 @@
use std::{path::Path, sync::Arc};
use gpui::{AppContext, AsyncAppContext, Model, View, ViewContext, WindowContext};
use language::Buffer;
use lsp::{LanguageServer, LanguageServerId};
use project::{lsp_command::LspCommand, lsp_ext_command::ExpandMacro, Project};
use rpc::proto::{self, PeerId};
use serde::{Deserialize, Serialize};
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| b.read(cx).language().map(|l| l.name()).as_deref() == Some("Rust"))
});
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 multibuffer = editor.buffer().read(cx);
let Some((trigger_anchor, 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)))
.find_map(|(buffer_id, trigger_anchor)| {
let buffer = multibuffer.buffer(buffer_id)?;
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, server.server_id(), buffer.clone()))
} else {
None
}
})
})
else {
return;
};
let z = project.update(cx, |project, cx| {
project.request_lsp(
buffer,
project::LanguageServerToQuery::Other(server_to_query),
ExpandMacro {},
cx,
)
});
// todo!("TODO kb")
}

View file

@ -5,7 +5,9 @@ use std::{
}; };
use anyhow::Result; use anyhow::Result;
use client::Client;
use serde_json::json; use serde_json::json;
use util::http::FakeHttpClient;
use crate::{Editor, ToPoint}; use crate::{Editor, ToPoint};
use collections::HashSet; use collections::HashSet;

View file

@ -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;

View file

@ -0,0 +1,100 @@
use std::{path::Path, sync::Arc};
use async_trait::async_trait;
use gpui::{AppContext, AsyncAppContext, Model};
use language::Buffer;
use lsp::{LanguageServer, LanguageServerId};
use rpc::proto::{self, PeerId};
use serde::{Deserialize, Serialize};
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,
}
pub struct ExpandMacro {}
// TODO kb
#[async_trait(?Send)]
impl LspCommand for ExpandMacro {
type Response = ExpandedMacro;
type LspRequest = LspExpandMacro;
type ProtoRequest = proto::LspExtExpandMacro;
fn to_lsp(
&self,
path: &Path,
buffer: &Buffer,
language_server: &Arc<LanguageServer>,
cx: &AppContext,
) -> ExpandMacroParams {
todo!()
}
async fn response_from_lsp(
self,
message: Option<ExpandedMacro>,
project: Model<Project>,
buffer: Model<Buffer>,
server_id: LanguageServerId,
cx: AsyncAppContext,
) -> anyhow::Result<ExpandedMacro> {
anyhow::bail!("TODO kb")
}
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::LspExtExpandMacro {
todo!()
}
async fn from_proto(
message: Self::ProtoRequest,
project: Model<Project>,
buffer: Model<Buffer>,
cx: AsyncAppContext,
) -> anyhow::Result<Self> {
todo!()
}
fn response_to_proto(
response: ExpandedMacro,
project: &mut Project,
peer_id: PeerId,
buffer_version: &clock::Global,
cx: &mut AppContext,
) -> proto::LspExtExpandMacroResponse {
todo!()
}
async fn response_from_proto(
self,
message: proto::LspExtExpandMacroResponse,
project: Model<Project>,
buffer: Model<Buffer>,
cx: AsyncAppContext,
) -> anyhow::Result<ExpandedMacro> {
todo!()
}
fn buffer_id_from_proto(message: &proto::LspExtExpandMacro) -> u64 {
message.buffer_id
}
}

View file

@ -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,

View file

@ -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,11 @@ 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;
}
message LspExtExpandMacroResponse {
}

View file

@ -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!(