Make it clear which client-side commands we use

This commit is contained in:
Aleksey Kladov 2021-02-14 19:36:44 +03:00
parent d50a37d3aa
commit 9852537809
2 changed files with 95 additions and 80 deletions

View File

@ -10,8 +10,7 @@ use std::{
use ide::{ use ide::{
AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, LineIndex, AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, LineIndex,
NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit,
TextEdit,
}; };
use ide_db::SymbolKind; use ide_db::SymbolKind;
use itertools::Itertools; use itertools::Itertools;
@ -19,12 +18,12 @@ use lsp_server::ErrorCode;
use lsp_types::{ use lsp_types::{
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem, CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams, CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
CodeActionKind, CodeLens, Command, CompletionItem, Diagnostic, DiagnosticTag, CodeActionKind, CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams,
DocumentFormattingParams, DocumentHighlight, FoldingRange, FoldingRangeParams, HoverContents, DocumentHighlight, FoldingRange, FoldingRangeParams, HoverContents, Location, NumberOrString,
Location, NumberOrString, Position, PrepareRenameResponse, Range, RenameParams, Position, PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
SymbolTag, TextDocumentIdentifier, TextDocumentPositionParams, Url, WorkspaceEdit, TextDocumentIdentifier, TextDocumentPositionParams, Url, WorkspaceEdit,
}; };
use project_model::TargetKind; use project_model::TargetKind;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -1422,40 +1421,7 @@ pub(crate) fn handle_open_cargo_toml(
Ok(Some(res)) Ok(Some(res))
} }
fn run_single_command(runnable: &lsp_ext::Runnable, title: &str) -> Command { fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
Command {
title: title.to_string(),
command: "rust-analyzer.runSingle".into(),
arguments: Some(vec![to_value(runnable).unwrap()]),
}
}
fn debug_single_command(runnable: &lsp_ext::Runnable) -> Command {
Command {
title: "Debug".into(),
command: "rust-analyzer.debugSingle".into(),
arguments: Some(vec![to_value(runnable).unwrap()]),
}
}
fn goto_location_command(snap: &GlobalStateSnapshot, nav: &NavigationTarget) -> Option<Command> {
let value = if snap.config.location_link() {
let link = to_proto::location_link(snap, None, nav.clone()).ok()?;
to_value(link).ok()?
} else {
let range = FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() };
let location = to_proto::location(snap, range).ok()?;
to_value(location).ok()?
};
Some(Command {
title: nav.name.to_string(),
command: "rust-analyzer.gotoLocation".into(),
arguments: Some(vec![value]),
})
}
fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink {
lsp_ext::CommandLink { tooltip: Some(tooltip), command } lsp_ext::CommandLink { tooltip: Some(tooltip), command }
} }
@ -1474,7 +1440,7 @@ fn show_impl_command_link(
.filter_map(|nav| to_proto::location_from_nav(snap, nav).ok()) .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok())
.collect(); .collect();
let title = to_proto::implementation_title(locations.len()); let title = to_proto::implementation_title(locations.len());
let command = to_proto::show_references_command(title, &uri, position, locations); let command = to_proto::command::show_references(title, &uri, position, locations);
return Some(lsp_ext::CommandLinkGroup { return Some(lsp_ext::CommandLinkGroup {
commands: vec![to_command_link(command, "Go to implementations".into())], commands: vec![to_command_link(command, "Go to implementations".into())],
@ -1501,12 +1467,12 @@ fn runnable_action_links(
let mut group = lsp_ext::CommandLinkGroup::default(); let mut group = lsp_ext::CommandLinkGroup::default();
if hover_config.run { if hover_config.run {
let run_command = run_single_command(&r, action.run_title); let run_command = to_proto::command::run_single(&r, action.run_title);
group.commands.push(to_command_link(run_command, r.label.clone())); group.commands.push(to_command_link(run_command, r.label.clone()));
} }
if hover_config.debug { if hover_config.debug {
let dbg_command = debug_single_command(&r); let dbg_command = to_proto::command::debug_single(&r);
group.commands.push(to_command_link(dbg_command, r.label)); group.commands.push(to_command_link(dbg_command, r.label));
} }
@ -1527,7 +1493,7 @@ fn goto_type_action_links(
commands: nav_targets commands: nav_targets
.iter() .iter()
.filter_map(|it| { .filter_map(|it| {
goto_location_command(snap, &it.nav) to_proto::command::goto_location(snap, &it.nav)
.map(|cmd| to_command_link(cmd, it.mod_path.clone())) .map(|cmd| to_command_link(cmd, it.mod_path.clone()))
}) })
.collect(), .collect(),

View File

@ -229,11 +229,7 @@ pub(crate) fn completion_item(
} }
if completion_item.trigger_call_info() { if completion_item.trigger_call_info() {
res.command = Some(lsp_types::Command { res.command = Some(command::trigger_parameter_hints());
title: "triggerParameterHints".into(),
command: "editor.action.triggerParameterHints".into(),
arguments: None,
});
} }
let mut all_results = match completion_item.ref_match() { let mut all_results = match completion_item.ref_match() {
@ -878,17 +874,10 @@ pub(crate) fn code_lens(
let r = runnable(&snap, run.nav.file_id, run)?; let r = runnable(&snap, run.nav.file_id, run)?;
let command = if debug { let command = if debug {
lsp_types::Command { command::debug_single(&r)
title: action.run_title.to_string(),
command: "rust-analyzer.runSingle".into(),
arguments: Some(vec![to_value(r).unwrap()]),
}
} else { } else {
lsp_types::Command { let title = action.run_title.to_string();
title: "Debug".into(), command::run_single(&r, &title)
command: "rust-analyzer.debugSingle".into(),
arguments: Some(vec![to_value(r).unwrap()]),
}
}; };
Ok(lsp_types::CodeLens { range: annotation_range, command: Some(command), data: None }) Ok(lsp_types::CodeLens { range: annotation_range, command: Some(command), data: None })
@ -922,7 +911,7 @@ pub(crate) fn code_lens(
}) })
.collect(); .collect();
show_references_command( command::show_references(
implementation_title(locations.len()), implementation_title(locations.len()),
&url, &url,
position, position,
@ -951,7 +940,12 @@ pub(crate) fn code_lens(
let locations: Vec<lsp_types::Location> = let locations: Vec<lsp_types::Location> =
ranges.into_iter().filter_map(|range| location(snap, range).ok()).collect(); ranges.into_iter().filter_map(|range| location(snap, range).ok()).collect();
show_references_command(reference_title(locations.len()), &url, position, locations) command::show_references(
reference_title(locations.len()),
&url,
position,
locations,
)
}); });
Ok(lsp_types::CodeLens { Ok(lsp_types::CodeLens {
@ -963,12 +957,22 @@ pub(crate) fn code_lens(
} }
} }
pub(crate) fn show_references_command( pub(crate) mod command {
use ide::{FileRange, NavigationTarget};
use serde_json::to_value;
use crate::{
global_state::GlobalStateSnapshot,
lsp_ext,
to_proto::{location, location_link},
};
pub(crate) fn show_references(
title: String, title: String,
uri: &lsp_types::Url, uri: &lsp_types::Url,
position: lsp_types::Position, position: lsp_types::Position,
locations: Vec<lsp_types::Location>, locations: Vec<lsp_types::Location>,
) -> lsp_types::Command { ) -> lsp_types::Command {
// We cannot use the 'editor.action.showReferences' command directly // We cannot use the 'editor.action.showReferences' command directly
// because that command requires vscode types which we convert in the handler // because that command requires vscode types which we convert in the handler
// on the client side. // on the client side.
@ -982,6 +986,51 @@ pub(crate) fn show_references_command(
to_value(locations).unwrap(), to_value(locations).unwrap(),
]), ]),
} }
}
pub(crate) fn run_single(runnable: &lsp_ext::Runnable, title: &str) -> lsp_types::Command {
lsp_types::Command {
title: title.to_string(),
command: "rust-analyzer.runSingle".into(),
arguments: Some(vec![to_value(runnable).unwrap()]),
}
}
pub(crate) fn debug_single(runnable: &lsp_ext::Runnable) -> lsp_types::Command {
lsp_types::Command {
title: "Debug".into(),
command: "rust-analyzer.debugSingle".into(),
arguments: Some(vec![to_value(runnable).unwrap()]),
}
}
pub(crate) fn goto_location(
snap: &GlobalStateSnapshot,
nav: &NavigationTarget,
) -> Option<lsp_types::Command> {
let value = if snap.config.location_link() {
let link = location_link(snap, None, nav.clone()).ok()?;
to_value(link).ok()?
} else {
let range = FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() };
let location = location(snap, range).ok()?;
to_value(location).ok()?
};
Some(lsp_types::Command {
title: nav.name.to_string(),
command: "rust-analyzer.gotoLocation".into(),
arguments: Some(vec![value]),
})
}
pub(crate) fn trigger_parameter_hints() -> lsp_types::Command {
lsp_types::Command {
title: "triggerParameterHints".into(),
command: "editor.action.triggerParameterHints".into(),
arguments: None,
}
}
} }
pub(crate) fn implementation_title(count: usize) -> String { pub(crate) fn implementation_title(count: usize) -> String {