7676: Make it clear which client-side commands we use r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-02-14 16:40:44 +00:00 committed by GitHub
commit 425c56aa52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 80 deletions

View File

@ -10,8 +10,7 @@
use ide::{
AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, LineIndex,
NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange,
TextEdit,
Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit,
};
use ide_db::SymbolKind;
use itertools::Itertools;
@ -19,12 +18,12 @@
use lsp_types::{
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
CodeActionKind, CodeLens, Command, CompletionItem, Diagnostic, DiagnosticTag,
DocumentFormattingParams, DocumentHighlight, FoldingRange, FoldingRangeParams, HoverContents,
Location, NumberOrString, Position, PrepareRenameResponse, Range, RenameParams,
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
SymbolTag, TextDocumentIdentifier, TextDocumentPositionParams, Url, WorkspaceEdit,
CodeActionKind, CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams,
DocumentHighlight, FoldingRange, FoldingRangeParams, HoverContents, Location, NumberOrString,
Position, PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag,
TextDocumentIdentifier, TextDocumentPositionParams, Url, WorkspaceEdit,
};
use project_model::TargetKind;
use serde::{Deserialize, Serialize};
@ -1422,40 +1421,7 @@ pub(crate) fn handle_open_cargo_toml(
Ok(Some(res))
}
fn run_single_command(runnable: &lsp_ext::Runnable, title: &str) -> Command {
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 {
fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink {
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())
.collect();
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 {
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();
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()));
}
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));
}
@ -1527,7 +1493,7 @@ fn goto_type_action_links(
commands: nav_targets
.iter()
.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()))
})
.collect(),

View File

@ -229,11 +229,7 @@ fn set_score(res: &mut lsp_types::CompletionItem, label: &str) {
}
if completion_item.trigger_call_info() {
res.command = Some(lsp_types::Command {
title: "triggerParameterHints".into(),
command: "editor.action.triggerParameterHints".into(),
arguments: None,
});
res.command = Some(command::trigger_parameter_hints());
}
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 command = if debug {
lsp_types::Command {
title: action.run_title.to_string(),
command: "rust-analyzer.runSingle".into(),
arguments: Some(vec![to_value(r).unwrap()]),
}
command::debug_single(&r)
} else {
lsp_types::Command {
title: "Debug".into(),
command: "rust-analyzer.debugSingle".into(),
arguments: Some(vec![to_value(r).unwrap()]),
}
let title = action.run_title.to_string();
command::run_single(&r, &title)
};
Ok(lsp_types::CodeLens { range: annotation_range, command: Some(command), data: None })
@ -922,7 +911,7 @@ pub(crate) fn code_lens(
})
.collect();
show_references_command(
command::show_references(
implementation_title(locations.len()),
&url,
position,
@ -951,7 +940,12 @@ pub(crate) fn code_lens(
let locations: Vec<lsp_types::Location> =
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 {
@ -963,24 +957,79 @@ pub(crate) fn code_lens(
}
}
pub(crate) fn show_references_command(
title: String,
uri: &lsp_types::Url,
position: lsp_types::Position,
locations: Vec<lsp_types::Location>,
) -> lsp_types::Command {
// We cannot use the 'editor.action.showReferences' command directly
// because that command requires vscode types which we convert in the handler
// on the client side.
pub(crate) mod command {
use ide::{FileRange, NavigationTarget};
use serde_json::to_value;
lsp_types::Command {
title,
command: "rust-analyzer.showReferences".into(),
arguments: Some(vec![
to_value(uri).unwrap(),
to_value(position).unwrap(),
to_value(locations).unwrap(),
]),
use crate::{
global_state::GlobalStateSnapshot,
lsp_ext,
to_proto::{location, location_link},
};
pub(crate) fn show_references(
title: String,
uri: &lsp_types::Url,
position: lsp_types::Position,
locations: Vec<lsp_types::Location>,
) -> lsp_types::Command {
// We cannot use the 'editor.action.showReferences' command directly
// because that command requires vscode types which we convert in the handler
// on the client side.
lsp_types::Command {
title,
command: "rust-analyzer.showReferences".into(),
arguments: Some(vec![
to_value(uri).unwrap(),
to_value(position).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,
}
}
}