From 9852537809a78b73a0e17eb2dfdf2ab5b07f5eae Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 14 Feb 2021 19:36:44 +0300 Subject: Make it clear which client-side commands we use --- crates/rust-analyzer/src/handlers.rs | 58 ++++------------- crates/rust-analyzer/src/to_proto.rs | 119 ++++++++++++++++++++++++----------- 2 files changed, 96 insertions(+), 81 deletions(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index b051c8f6c..2d697c75f 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -10,8 +10,7 @@ use std::{ 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_server::ErrorCode; 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 { - 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(), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 29fac96fb..8a2b4d9bd 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -229,11 +229,7 @@ pub(crate) fn completion_item( } 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 = 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::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) 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, + uri: &lsp_types::Url, + position: lsp_types::Position, + locations: Vec, + ) -> 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 { + 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, + } } } -- cgit v1.2.3