diff options
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 58 | ||||
-rw-r--r-- | 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::{ | |||
10 | 10 | ||
11 | use ide::{ | 11 | use ide::{ |
12 | AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, LineIndex, | 12 | AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, LineIndex, |
13 | NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, | 13 | Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit, |
14 | TextEdit, | ||
15 | }; | 14 | }; |
16 | use ide_db::SymbolKind; | 15 | use ide_db::SymbolKind; |
17 | use itertools::Itertools; | 16 | use itertools::Itertools; |
@@ -19,12 +18,12 @@ use lsp_server::ErrorCode; | |||
19 | use lsp_types::{ | 18 | use lsp_types::{ |
20 | CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem, | 19 | CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem, |
21 | CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams, | 20 | CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams, |
22 | CodeActionKind, CodeLens, Command, CompletionItem, Diagnostic, DiagnosticTag, | 21 | CodeActionKind, CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, |
23 | DocumentFormattingParams, DocumentHighlight, FoldingRange, FoldingRangeParams, HoverContents, | 22 | DocumentHighlight, FoldingRange, FoldingRangeParams, HoverContents, Location, NumberOrString, |
24 | Location, NumberOrString, Position, PrepareRenameResponse, Range, RenameParams, | 23 | Position, PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams, |
25 | SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams, | 24 | SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams, |
26 | SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, | 25 | SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag, |
27 | SymbolTag, TextDocumentIdentifier, TextDocumentPositionParams, Url, WorkspaceEdit, | 26 | TextDocumentIdentifier, TextDocumentPositionParams, Url, WorkspaceEdit, |
28 | }; | 27 | }; |
29 | use project_model::TargetKind; | 28 | use project_model::TargetKind; |
30 | use serde::{Deserialize, Serialize}; | 29 | use serde::{Deserialize, Serialize}; |
@@ -1422,40 +1421,7 @@ pub(crate) fn handle_open_cargo_toml( | |||
1422 | Ok(Some(res)) | 1421 | Ok(Some(res)) |
1423 | } | 1422 | } |
1424 | 1423 | ||
1425 | fn run_single_command(runnable: &lsp_ext::Runnable, title: &str) -> Command { | 1424 | fn to_command_link(command: lsp_types::Command, tooltip: String) -> lsp_ext::CommandLink { |
1426 | Command { | ||
1427 | title: title.to_string(), | ||
1428 | command: "rust-analyzer.runSingle".into(), | ||
1429 | arguments: Some(vec![to_value(runnable).unwrap()]), | ||
1430 | } | ||
1431 | } | ||
1432 | |||
1433 | fn debug_single_command(runnable: &lsp_ext::Runnable) -> Command { | ||
1434 | Command { | ||
1435 | title: "Debug".into(), | ||
1436 | command: "rust-analyzer.debugSingle".into(), | ||
1437 | arguments: Some(vec![to_value(runnable).unwrap()]), | ||
1438 | } | ||
1439 | } | ||
1440 | |||
1441 | fn goto_location_command(snap: &GlobalStateSnapshot, nav: &NavigationTarget) -> Option<Command> { | ||
1442 | let value = if snap.config.location_link() { | ||
1443 | let link = to_proto::location_link(snap, None, nav.clone()).ok()?; | ||
1444 | to_value(link).ok()? | ||
1445 | } else { | ||
1446 | let range = FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() }; | ||
1447 | let location = to_proto::location(snap, range).ok()?; | ||
1448 | to_value(location).ok()? | ||
1449 | }; | ||
1450 | |||
1451 | Some(Command { | ||
1452 | title: nav.name.to_string(), | ||
1453 | command: "rust-analyzer.gotoLocation".into(), | ||
1454 | arguments: Some(vec![value]), | ||
1455 | }) | ||
1456 | } | ||
1457 | |||
1458 | fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink { | ||
1459 | lsp_ext::CommandLink { tooltip: Some(tooltip), command } | 1425 | lsp_ext::CommandLink { tooltip: Some(tooltip), command } |
1460 | } | 1426 | } |
1461 | 1427 | ||
@@ -1474,7 +1440,7 @@ fn show_impl_command_link( | |||
1474 | .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok()) | 1440 | .filter_map(|nav| to_proto::location_from_nav(snap, nav).ok()) |
1475 | .collect(); | 1441 | .collect(); |
1476 | let title = to_proto::implementation_title(locations.len()); | 1442 | let title = to_proto::implementation_title(locations.len()); |
1477 | let command = to_proto::show_references_command(title, &uri, position, locations); | 1443 | let command = to_proto::command::show_references(title, &uri, position, locations); |
1478 | 1444 | ||
1479 | return Some(lsp_ext::CommandLinkGroup { | 1445 | return Some(lsp_ext::CommandLinkGroup { |
1480 | commands: vec![to_command_link(command, "Go to implementations".into())], | 1446 | commands: vec![to_command_link(command, "Go to implementations".into())], |
@@ -1501,12 +1467,12 @@ fn runnable_action_links( | |||
1501 | let mut group = lsp_ext::CommandLinkGroup::default(); | 1467 | let mut group = lsp_ext::CommandLinkGroup::default(); |
1502 | 1468 | ||
1503 | if hover_config.run { | 1469 | if hover_config.run { |
1504 | let run_command = run_single_command(&r, action.run_title); | 1470 | let run_command = to_proto::command::run_single(&r, action.run_title); |
1505 | group.commands.push(to_command_link(run_command, r.label.clone())); | 1471 | group.commands.push(to_command_link(run_command, r.label.clone())); |
1506 | } | 1472 | } |
1507 | 1473 | ||
1508 | if hover_config.debug { | 1474 | if hover_config.debug { |
1509 | let dbg_command = debug_single_command(&r); | 1475 | let dbg_command = to_proto::command::debug_single(&r); |
1510 | group.commands.push(to_command_link(dbg_command, r.label)); | 1476 | group.commands.push(to_command_link(dbg_command, r.label)); |
1511 | } | 1477 | } |
1512 | 1478 | ||
@@ -1527,7 +1493,7 @@ fn goto_type_action_links( | |||
1527 | commands: nav_targets | 1493 | commands: nav_targets |
1528 | .iter() | 1494 | .iter() |
1529 | .filter_map(|it| { | 1495 | .filter_map(|it| { |
1530 | goto_location_command(snap, &it.nav) | 1496 | to_proto::command::goto_location(snap, &it.nav) |
1531 | .map(|cmd| to_command_link(cmd, it.mod_path.clone())) | 1497 | .map(|cmd| to_command_link(cmd, it.mod_path.clone())) |
1532 | }) | 1498 | }) |
1533 | .collect(), | 1499 | .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( | |||
229 | } | 229 | } |
230 | 230 | ||
231 | if completion_item.trigger_call_info() { | 231 | if completion_item.trigger_call_info() { |
232 | res.command = Some(lsp_types::Command { | 232 | res.command = Some(command::trigger_parameter_hints()); |
233 | title: "triggerParameterHints".into(), | ||
234 | command: "editor.action.triggerParameterHints".into(), | ||
235 | arguments: None, | ||
236 | }); | ||
237 | } | 233 | } |
238 | 234 | ||
239 | let mut all_results = match completion_item.ref_match() { | 235 | let mut all_results = match completion_item.ref_match() { |
@@ -878,17 +874,10 @@ pub(crate) fn code_lens( | |||
878 | let r = runnable(&snap, run.nav.file_id, run)?; | 874 | let r = runnable(&snap, run.nav.file_id, run)?; |
879 | 875 | ||
880 | let command = if debug { | 876 | let command = if debug { |
881 | lsp_types::Command { | 877 | command::debug_single(&r) |
882 | title: action.run_title.to_string(), | ||
883 | command: "rust-analyzer.runSingle".into(), | ||
884 | arguments: Some(vec![to_value(r).unwrap()]), | ||
885 | } | ||
886 | } else { | 878 | } else { |
887 | lsp_types::Command { | 879 | let title = action.run_title.to_string(); |
888 | title: "Debug".into(), | 880 | command::run_single(&r, &title) |
889 | command: "rust-analyzer.debugSingle".into(), | ||
890 | arguments: Some(vec![to_value(r).unwrap()]), | ||
891 | } | ||
892 | }; | 881 | }; |
893 | 882 | ||
894 | Ok(lsp_types::CodeLens { range: annotation_range, command: Some(command), data: None }) | 883 | Ok(lsp_types::CodeLens { range: annotation_range, command: Some(command), data: None }) |
@@ -922,7 +911,7 @@ pub(crate) fn code_lens( | |||
922 | }) | 911 | }) |
923 | .collect(); | 912 | .collect(); |
924 | 913 | ||
925 | show_references_command( | 914 | command::show_references( |
926 | implementation_title(locations.len()), | 915 | implementation_title(locations.len()), |
927 | &url, | 916 | &url, |
928 | position, | 917 | position, |
@@ -951,7 +940,12 @@ pub(crate) fn code_lens( | |||
951 | let locations: Vec<lsp_types::Location> = | 940 | let locations: Vec<lsp_types::Location> = |
952 | ranges.into_iter().filter_map(|range| location(snap, range).ok()).collect(); | 941 | ranges.into_iter().filter_map(|range| location(snap, range).ok()).collect(); |
953 | 942 | ||
954 | show_references_command(reference_title(locations.len()), &url, position, locations) | 943 | command::show_references( |
944 | reference_title(locations.len()), | ||
945 | &url, | ||
946 | position, | ||
947 | locations, | ||
948 | ) | ||
955 | }); | 949 | }); |
956 | 950 | ||
957 | Ok(lsp_types::CodeLens { | 951 | Ok(lsp_types::CodeLens { |
@@ -963,24 +957,79 @@ pub(crate) fn code_lens( | |||
963 | } | 957 | } |
964 | } | 958 | } |
965 | 959 | ||
966 | pub(crate) fn show_references_command( | 960 | pub(crate) mod command { |
967 | title: String, | 961 | use ide::{FileRange, NavigationTarget}; |
968 | uri: &lsp_types::Url, | 962 | use serde_json::to_value; |
969 | position: lsp_types::Position, | 963 | |
970 | locations: Vec<lsp_types::Location>, | 964 | use crate::{ |
971 | ) -> lsp_types::Command { | 965 | global_state::GlobalStateSnapshot, |
972 | // We cannot use the 'editor.action.showReferences' command directly | 966 | lsp_ext, |
973 | // because that command requires vscode types which we convert in the handler | 967 | to_proto::{location, location_link}, |
974 | // on the client side. | 968 | }; |
975 | 969 | ||
976 | lsp_types::Command { | 970 | pub(crate) fn show_references( |
977 | title, | 971 | title: String, |
978 | command: "rust-analyzer.showReferences".into(), | 972 | uri: &lsp_types::Url, |
979 | arguments: Some(vec![ | 973 | position: lsp_types::Position, |
980 | to_value(uri).unwrap(), | 974 | locations: Vec<lsp_types::Location>, |
981 | to_value(position).unwrap(), | 975 | ) -> lsp_types::Command { |
982 | to_value(locations).unwrap(), | 976 | // We cannot use the 'editor.action.showReferences' command directly |
983 | ]), | 977 | // because that command requires vscode types which we convert in the handler |
978 | // on the client side. | ||
979 | |||
980 | lsp_types::Command { | ||
981 | title, | ||
982 | command: "rust-analyzer.showReferences".into(), | ||
983 | arguments: Some(vec![ | ||
984 | to_value(uri).unwrap(), | ||
985 | to_value(position).unwrap(), | ||
986 | to_value(locations).unwrap(), | ||
987 | ]), | ||
988 | } | ||
989 | } | ||
990 | |||
991 | pub(crate) fn run_single(runnable: &lsp_ext::Runnable, title: &str) -> lsp_types::Command { | ||
992 | lsp_types::Command { | ||
993 | title: title.to_string(), | ||
994 | command: "rust-analyzer.runSingle".into(), | ||
995 | arguments: Some(vec![to_value(runnable).unwrap()]), | ||
996 | } | ||
997 | } | ||
998 | |||
999 | pub(crate) fn debug_single(runnable: &lsp_ext::Runnable) -> lsp_types::Command { | ||
1000 | lsp_types::Command { | ||
1001 | title: "Debug".into(), | ||
1002 | command: "rust-analyzer.debugSingle".into(), | ||
1003 | arguments: Some(vec![to_value(runnable).unwrap()]), | ||
1004 | } | ||
1005 | } | ||
1006 | |||
1007 | pub(crate) fn goto_location( | ||
1008 | snap: &GlobalStateSnapshot, | ||
1009 | nav: &NavigationTarget, | ||
1010 | ) -> Option<lsp_types::Command> { | ||
1011 | let value = if snap.config.location_link() { | ||
1012 | let link = location_link(snap, None, nav.clone()).ok()?; | ||
1013 | to_value(link).ok()? | ||
1014 | } else { | ||
1015 | let range = FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() }; | ||
1016 | let location = location(snap, range).ok()?; | ||
1017 | to_value(location).ok()? | ||
1018 | }; | ||
1019 | |||
1020 | Some(lsp_types::Command { | ||
1021 | title: nav.name.to_string(), | ||
1022 | command: "rust-analyzer.gotoLocation".into(), | ||
1023 | arguments: Some(vec![value]), | ||
1024 | }) | ||
1025 | } | ||
1026 | |||
1027 | pub(crate) fn trigger_parameter_hints() -> lsp_types::Command { | ||
1028 | lsp_types::Command { | ||
1029 | title: "triggerParameterHints".into(), | ||
1030 | command: "editor.action.triggerParameterHints".into(), | ||
1031 | arguments: None, | ||
1032 | } | ||
984 | } | 1033 | } |
985 | } | 1034 | } |
986 | 1035 | ||