Skip to content

Commit

Permalink
1. editor: Right-click add command "Show Call Hierarchy" (lapce#3389)
Browse files Browse the repository at this point in the history
* 1. editor: Right-click add command "Show Call Hierarchy"
2. fixed warnings in clippy report

* Remove the right-click menu "Show Call Hierarchy"
  • Loading branch information
jm-observer authored Jul 29, 2024
1 parent 98ebe57 commit 41aaca3
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lapce-app/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ pub enum LapceWorkbenchCommand {
#[strum(message = "Open File")]
OpenFile,

#[strum(serialize = "show_call_hierarchy")]
#[strum(message = "Show Call Hierarchy")]
ShowCallHierarchy,

#[strum(serialize = "reveal_in_file_tree")]
#[strum(message = "Reveal in File Tree")]
RevealInFileTree,
Expand Down
46 changes: 46 additions & 0 deletions lapce-app/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,49 @@ impl EditorData {
);
}

pub fn show_call_hierarchy(&self) {
let doc = self.doc();
let path = match if doc.loaded() {
doc.content.with_untracked(|c| c.path().cloned())
} else {
None
} {
Some(path) => path,
None => return,
};

let offset = self.cursor().with_untracked(|c| c.offset());
let (_start_position, position) = doc.buffer.with_untracked(|buffer| {
let start_offset = buffer.prev_code_boundary(offset);
let start_position = buffer.offset_to_position(start_offset);
let position = buffer.offset_to_position(offset);
(start_position, position)
});

let send = create_ext_action(self.scope, move |_rs| {
tracing::debug!("{:?}", _rs);
});
let proxy = self.common.proxy.clone();
self.common.proxy.show_call_hierarchy(
path.clone(),
position,
move |result| {
if let Ok(ProxyResponse::ShowCallHierarchyResponse {
items, ..
}) = result
{
if let Some(item) = items.and_then(|x| x.into_iter().next()) {
proxy.call_hierarchy_incoming(
path.clone(),
item.clone(),
send,
);
}
}
},
);
}

fn scroll(&self, down: bool, count: usize, mods: Modifiers) {
self.editor.scroll(
self.sticky_header_height.get_untracked(),
Expand Down Expand Up @@ -2582,6 +2625,9 @@ impl EditorData {
vec![
Some(CommandKind::Focus(FocusCommand::GotoDefinition)),
Some(CommandKind::Focus(FocusCommand::GotoTypeDefinition)),
// Some(CommandKind::Workbench(
// LapceWorkbenchCommand::ShowCallHierarchy,
// )),
None,
Some(CommandKind::Focus(FocusCommand::Rename)),
None,
Expand Down
2 changes: 1 addition & 1 deletion lapce-app/src/editor_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl EditorTabChild {
};
let (icon, color, name, confirmed, is_pristine) = match path {
Some((ref path, confirmed, is_pritine)) => {
let (svg, color) = config.file_svg(&path);
let (svg, color) = config.file_svg(path);
(
svg,
color,
Expand Down
7 changes: 7 additions & 0 deletions lapce-app/src/window_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,13 @@ impl WindowTabData {
}
}
}
ShowCallHierarchy => {
if let Some(editor_data) =
self.main_split.active_editor.get_untracked()
{
editor_data.show_call_hierarchy();
}
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions lapce-proxy/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,35 @@ impl ProxyHandler for Dispatcher {
},
);
}
ShowCallHierarchy { path, position } => {
let proxy_rpc = self.proxy_rpc.clone();
self.catalog_rpc.show_call_hierarchy(
&path,
position,
move |_, result| {
let result = result.map(|items| {
ProxyResponse::ShowCallHierarchyResponse { items }
});
proxy_rpc.handle_response(id, result);
},
);
}
CallHierarchyIncoming {
path,
call_hierarchy_item,
} => {
let proxy_rpc = self.proxy_rpc.clone();
self.catalog_rpc.call_hierarchy_incoming(
&path,
call_hierarchy_item,
move |_, result| {
let result = result.map(|items| {
ProxyResponse::CallHierarchyIncomingResponse { items }
});
proxy_rpc.handle_response(id, result);
},
);
}
GetInlayHints { path } => {
let proxy_rpc = self.proxy_rpc.clone();
let buffer = self.buffers.get(&path).unwrap();
Expand Down
68 changes: 66 additions & 2 deletions lapce-proxy/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ use lapce_rpc::{
use lapce_xi_rope::{Rope, RopeDelta};
use lsp_types::{
request::{
CodeActionRequest, CodeActionResolveRequest, CodeLensRequest, Completion,
CallHierarchyIncomingCalls, CallHierarchyPrepare, CodeActionRequest,
CodeActionResolveRequest, CodeLensRequest, Completion,
DocumentSymbolRequest, Formatting, GotoDefinition, GotoTypeDefinition,
GotoTypeDefinitionParams, GotoTypeDefinitionResponse, HoverRequest,
InlayHintRequest, InlineCompletionRequest, PrepareRenameRequest, References,
Rename, Request, ResolveCompletionItem, SelectionRangeRequest,
SemanticTokensFullRequest, SignatureHelpRequest, WorkspaceSymbolRequest,
},
CallHierarchyClientCapabilities, CallHierarchyIncomingCall,
CallHierarchyIncomingCallsParams, CallHierarchyItem, CallHierarchyPrepareParams,
ClientCapabilities, CodeAction, CodeActionCapabilityResolveSupport,
CodeActionClientCapabilities, CodeActionContext, CodeActionKind,
CodeActionKindLiteralSupport, CodeActionLiteralSupport, CodeActionParams,
Expand Down Expand Up @@ -578,6 +581,65 @@ impl PluginCatalogRpcHandler {
);
}

pub fn call_hierarchy_incoming(
&self,
path: &Path,
item: CallHierarchyItem,
cb: impl FnOnce(
PluginId,
Result<Option<Vec<CallHierarchyIncomingCall>>, RpcError>,
) + Clone
+ Send
+ 'static,
) {
let method = CallHierarchyIncomingCalls::METHOD;
let params = CallHierarchyIncomingCallsParams {
item,
work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: Default::default(),
};

let language_id =
Some(language_id_from_path(path).unwrap_or("").to_string());
self.send_request_to_all_plugins(
method,
params,
language_id,
Some(path.to_path_buf()),
cb,
);
}

pub fn show_call_hierarchy(
&self,
path: &Path,
position: Position,
cb: impl FnOnce(PluginId, Result<Option<Vec<CallHierarchyItem>>, RpcError>)
+ Clone
+ Send
+ 'static,
) {
let uri = Url::from_file_path(path).unwrap();
let method = CallHierarchyPrepare::METHOD;
let params = CallHierarchyPrepareParams {
text_document_position_params: TextDocumentPositionParams {
text_document: TextDocumentIdentifier { uri },
position,
},
work_done_progress_params: WorkDoneProgressParams::default(),
};

let language_id =
Some(language_id_from_path(path).unwrap_or("").to_string());
self.send_request_to_all_plugins(
method,
params,
language_id,
Some(path.to_path_buf()),
cb,
);
}

pub fn get_references(
&self,
path: &Path,
Expand Down Expand Up @@ -1558,7 +1620,9 @@ fn client_capabilities() -> ClientCapabilities {
inline_completion: Some(InlineCompletionClientCapabilities {
..Default::default()
}),

call_hierarchy: Some(CallHierarchyClientCapabilities {
dynamic_registration: Some(true),
}),
..Default::default()
}),
window: Some(WindowClientCapabilities {
Expand Down
12 changes: 9 additions & 3 deletions lapce-proxy/src/plugin/psp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ use dyn_clone::DynClone;
use floem_editor_core::buffer::rope_text::{RopeText, RopeTextRef};
use jsonrpc_lite::{Id, JsonRpc, Params};
use lapce_core::{encoding::offset_utf16_to_utf8, rope_text_pos::RopeTextPosition};
use lapce_rpc::core::ServerStatusParams;
use lapce_rpc::{
core::CoreRpcHandler,
core::{CoreRpcHandler, ServerStatusParams},
plugin::{PluginId, VoltID},
style::{LineStyle, Style},
RpcError,
Expand All @@ -30,7 +29,8 @@ use lsp_types::{
ShowMessage,
},
request::{
CodeActionRequest, CodeActionResolveRequest, CodeLensRequest, Completion,
CallHierarchyIncomingCalls, CallHierarchyPrepare, CodeActionRequest,
CodeActionResolveRequest, CodeLensRequest, Completion,
DocumentSymbolRequest, Formatting, GotoDefinition, GotoTypeDefinition,
HoverRequest, Initialize, InlayHintRequest, InlineCompletionRequest,
PrepareRenameRequest, References, RegisterCapability, Rename,
Expand Down Expand Up @@ -784,6 +784,12 @@ impl PluginHostHandler {
CodeLensRequest::METHOD => {
self.server_capabilities.code_lens_provider.is_some()
}
CallHierarchyPrepare::METHOD => {
self.server_capabilities.call_hierarchy_provider.is_some()
}
CallHierarchyIncomingCalls::METHOD => {
self.server_capabilities.call_hierarchy_provider.is_some()
}
_ => false,
}
}
Expand Down
49 changes: 44 additions & 5 deletions lapce-rpc/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use crossbeam_channel::{Receiver, Sender};
use indexmap::IndexMap;
use lapce_xi_rope::RopeDelta;
use lsp_types::{
request::GotoTypeDefinitionResponse, CodeAction, CodeActionResponse, CodeLens,
CompletionItem, Diagnostic, DocumentSymbolResponse, GotoDefinitionResponse,
Hover, InlayHint, InlineCompletionResponse, InlineCompletionTriggerKind,
Location, Position, PrepareRenameResponse, SelectionRange, SymbolInformation,
TextDocumentItem, TextEdit, WorkspaceEdit,
request::GotoTypeDefinitionResponse, CallHierarchyIncomingCall,
CallHierarchyItem, CodeAction, CodeActionResponse, CodeLens, CompletionItem,
Diagnostic, DocumentSymbolResponse, GotoDefinitionResponse, Hover, InlayHint,
InlineCompletionResponse, InlineCompletionTriggerKind, Location, Position,
PrepareRenameResponse, SelectionRange, SymbolInformation, TextDocumentItem,
TextEdit, WorkspaceEdit,
};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -104,6 +105,14 @@ pub enum ProxyRequest {
path: PathBuf,
position: Position,
},
ShowCallHierarchy {
path: PathBuf,
position: Position,
},
CallHierarchyIncoming {
path: PathBuf,
call_hierarchy_item: CallHierarchyItem,
},
GetTypeDefinition {
request_id: usize,
path: PathBuf,
Expand Down Expand Up @@ -358,6 +367,12 @@ pub enum ProxyResponse {
request_id: usize,
definition: GotoDefinitionResponse,
},
ShowCallHierarchyResponse {
items: Option<Vec<CallHierarchyItem>>,
},
CallHierarchyIncomingResponse {
items: Option<Vec<CallHierarchyIncomingCall>>,
},
GetTypeDefinition {
request_id: usize,
definition: GotoTypeDefinitionResponse,
Expand Down Expand Up @@ -838,6 +853,30 @@ impl ProxyRpcHandler {
);
}

pub fn show_call_hierarchy(
&self,
path: PathBuf,
position: Position,
f: impl ProxyCallback + 'static,
) {
self.request_async(ProxyRequest::ShowCallHierarchy { path, position }, f);
}

pub fn call_hierarchy_incoming(
&self,
path: PathBuf,
call_hierarchy_item: CallHierarchyItem,
f: impl ProxyCallback + 'static,
) {
self.request_async(
ProxyRequest::CallHierarchyIncoming {
path,
call_hierarchy_item,
},
f,
);
}

pub fn get_type_definition(
&self,
request_id: usize,
Expand Down

0 comments on commit 41aaca3

Please sign in to comment.