diff options
author | Benjamin Coenen <[email protected]> | 2020-05-02 19:27:02 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-05-02 19:27:02 +0100 |
commit | 4613497a7714c6cd87166e6525d764d75f8acefd (patch) | |
tree | 2527ae2c0ef2ef100efee3fcb8899f8e34d0d573 /crates/rust-analyzer/src/main_loop/handlers.rs | |
parent | 19e28888aa41b2845b47adb7314aed99d3c48679 (diff) | |
parent | 89e1f97515c36ab97bd378d972cabec0feb6d77e (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into fix_4202
Diffstat (limited to 'crates/rust-analyzer/src/main_loop/handlers.rs')
-rw-r--r-- | crates/rust-analyzer/src/main_loop/handlers.rs | 87 |
1 files changed, 48 insertions, 39 deletions
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 6caaf5f88..c7a96ba93 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs | |||
@@ -19,8 +19,7 @@ use lsp_types::{ | |||
19 | TextEdit, Url, WorkspaceEdit, | 19 | TextEdit, Url, WorkspaceEdit, |
20 | }; | 20 | }; |
21 | use ra_ide::{ | 21 | use ra_ide::{ |
22 | Assist, AssistId, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind, | 22 | Assist, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind, SearchScope, |
23 | SearchScope, | ||
24 | }; | 23 | }; |
25 | use ra_prof::profile; | 24 | use ra_prof::profile; |
26 | use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize}; | 25 | use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize}; |
@@ -47,11 +46,11 @@ use crate::{ | |||
47 | pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result<String> { | 46 | pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result<String> { |
48 | let _p = profile("handle_analyzer_status"); | 47 | let _p = profile("handle_analyzer_status"); |
49 | let mut buf = world.status(); | 48 | let mut buf = world.status(); |
50 | format_to!(buf, "\n\nrequests:"); | 49 | format_to!(buf, "\n\nrequests:\n"); |
51 | let requests = world.latest_requests.read(); | 50 | let requests = world.latest_requests.read(); |
52 | for (is_last, r) in requests.iter() { | 51 | for (is_last, r) in requests.iter() { |
53 | let mark = if is_last { "*" } else { " " }; | 52 | let mark = if is_last { "*" } else { " " }; |
54 | format_to!(buf, "{}{:4} {:<36}{}ms", mark, r.id, r.method, r.duration.as_millis()); | 53 | format_to!(buf, "{}{:4} {:<36}{}ms\n", mark, r.id, r.method, r.duration.as_millis()); |
55 | } | 54 | } |
56 | Ok(buf) | 55 | Ok(buf) |
57 | } | 56 | } |
@@ -326,10 +325,10 @@ pub fn handle_workspace_symbol( | |||
326 | 325 | ||
327 | pub fn handle_goto_definition( | 326 | pub fn handle_goto_definition( |
328 | world: WorldSnapshot, | 327 | world: WorldSnapshot, |
329 | params: req::TextDocumentPositionParams, | 328 | params: req::GotoDefinitionParams, |
330 | ) -> Result<Option<req::GotoDefinitionResponse>> { | 329 | ) -> Result<Option<req::GotoDefinitionResponse>> { |
331 | let _p = profile("handle_goto_definition"); | 330 | let _p = profile("handle_goto_definition"); |
332 | let position = params.try_conv_with(&world)?; | 331 | let position = params.text_document_position_params.try_conv_with(&world)?; |
333 | let nav_info = match world.analysis().goto_definition(position)? { | 332 | let nav_info = match world.analysis().goto_definition(position)? { |
334 | None => return Ok(None), | 333 | None => return Ok(None), |
335 | Some(it) => it, | 334 | Some(it) => it, |
@@ -340,10 +339,10 @@ pub fn handle_goto_definition( | |||
340 | 339 | ||
341 | pub fn handle_goto_implementation( | 340 | pub fn handle_goto_implementation( |
342 | world: WorldSnapshot, | 341 | world: WorldSnapshot, |
343 | params: req::TextDocumentPositionParams, | 342 | params: req::GotoImplementationParams, |
344 | ) -> Result<Option<req::GotoImplementationResponse>> { | 343 | ) -> Result<Option<req::GotoImplementationResponse>> { |
345 | let _p = profile("handle_goto_implementation"); | 344 | let _p = profile("handle_goto_implementation"); |
346 | let position = params.try_conv_with(&world)?; | 345 | let position = params.text_document_position_params.try_conv_with(&world)?; |
347 | let nav_info = match world.analysis().goto_implementation(position)? { | 346 | let nav_info = match world.analysis().goto_implementation(position)? { |
348 | None => return Ok(None), | 347 | None => return Ok(None), |
349 | Some(it) => it, | 348 | Some(it) => it, |
@@ -354,10 +353,10 @@ pub fn handle_goto_implementation( | |||
354 | 353 | ||
355 | pub fn handle_goto_type_definition( | 354 | pub fn handle_goto_type_definition( |
356 | world: WorldSnapshot, | 355 | world: WorldSnapshot, |
357 | params: req::TextDocumentPositionParams, | 356 | params: req::GotoTypeDefinitionParams, |
358 | ) -> Result<Option<req::GotoTypeDefinitionResponse>> { | 357 | ) -> Result<Option<req::GotoTypeDefinitionResponse>> { |
359 | let _p = profile("handle_goto_type_definition"); | 358 | let _p = profile("handle_goto_type_definition"); |
360 | let position = params.try_conv_with(&world)?; | 359 | let position = params.text_document_position_params.try_conv_with(&world)?; |
361 | let nav_info = match world.analysis().goto_type_definition(position)? { | 360 | let nav_info = match world.analysis().goto_type_definition(position)? { |
362 | None => return Ok(None), | 361 | None => return Ok(None), |
363 | Some(it) => it, | 362 | Some(it) => it, |
@@ -401,11 +400,7 @@ pub fn handle_runnables( | |||
401 | range: Default::default(), | 400 | range: Default::default(), |
402 | label: format!("cargo {} -p {}", cmd, spec.package), | 401 | label: format!("cargo {} -p {}", cmd, spec.package), |
403 | bin: "cargo".to_string(), | 402 | bin: "cargo".to_string(), |
404 | args: { | 403 | args: vec![cmd.to_string(), "--package".to_string(), spec.package.clone()], |
405 | let mut args = vec![cmd.to_string()]; | ||
406 | spec.clone().push_to(&mut args); | ||
407 | args | ||
408 | }, | ||
409 | extra_args: Vec::new(), | 404 | extra_args: Vec::new(), |
410 | env: FxHashMap::default(), | 405 | env: FxHashMap::default(), |
411 | cwd: workspace_root.map(|root| root.to_owned()), | 406 | cwd: workspace_root.map(|root| root.to_owned()), |
@@ -487,10 +482,10 @@ pub fn handle_folding_range( | |||
487 | 482 | ||
488 | pub fn handle_signature_help( | 483 | pub fn handle_signature_help( |
489 | world: WorldSnapshot, | 484 | world: WorldSnapshot, |
490 | params: req::TextDocumentPositionParams, | 485 | params: req::SignatureHelpParams, |
491 | ) -> Result<Option<req::SignatureHelp>> { | 486 | ) -> Result<Option<req::SignatureHelp>> { |
492 | let _p = profile("handle_signature_help"); | 487 | let _p = profile("handle_signature_help"); |
493 | let position = params.try_conv_with(&world)?; | 488 | let position = params.text_document_position_params.try_conv_with(&world)?; |
494 | if let Some(call_info) = world.analysis().call_info(position)? { | 489 | if let Some(call_info) = world.analysis().call_info(position)? { |
495 | let concise = !world.config.call_info_full; | 490 | let concise = !world.config.call_info_full; |
496 | let mut active_parameter = call_info.active_parameter.map(|it| it as i64); | 491 | let mut active_parameter = call_info.active_parameter.map(|it| it as i64); |
@@ -509,12 +504,9 @@ pub fn handle_signature_help( | |||
509 | } | 504 | } |
510 | } | 505 | } |
511 | 506 | ||
512 | pub fn handle_hover( | 507 | pub fn handle_hover(world: WorldSnapshot, params: req::HoverParams) -> Result<Option<Hover>> { |
513 | world: WorldSnapshot, | ||
514 | params: req::TextDocumentPositionParams, | ||
515 | ) -> Result<Option<Hover>> { | ||
516 | let _p = profile("handle_hover"); | 508 | let _p = profile("handle_hover"); |
517 | let position = params.try_conv_with(&world)?; | 509 | let position = params.text_document_position_params.try_conv_with(&world)?; |
518 | let info = match world.analysis().hover(position)? { | 510 | let info = match world.analysis().hover(position)? { |
519 | None => return Ok(None), | 511 | None => return Ok(None), |
520 | Some(info) => info, | 512 | Some(info) => info, |
@@ -705,15 +697,9 @@ fn create_single_code_action(assist: Assist, world: &WorldSnapshot) -> Result<Co | |||
705 | arguments: Some(vec![arg]), | 697 | arguments: Some(vec![arg]), |
706 | }; | 698 | }; |
707 | 699 | ||
708 | let kind = match assist.id { | ||
709 | AssistId("introduce_variable") => Some("refactor.extract.variable".to_string()), | ||
710 | AssistId("add_custom_impl") => Some("refactor.rewrite.add_custom_impl".to_string()), | ||
711 | _ => None, | ||
712 | }; | ||
713 | |||
714 | Ok(CodeAction { | 700 | Ok(CodeAction { |
715 | title, | 701 | title, |
716 | kind, | 702 | kind: Some(String::new()), |
717 | diagnostics: None, | 703 | diagnostics: None, |
718 | edit: None, | 704 | edit: None, |
719 | command: Some(command), | 705 | command: Some(command), |
@@ -815,6 +801,23 @@ pub fn handle_code_action( | |||
815 | } | 801 | } |
816 | } | 802 | } |
817 | 803 | ||
804 | // If the client only supports commands then filter the list | ||
805 | // and remove and actions that depend on edits. | ||
806 | if !world.config.client_caps.code_action_literals { | ||
807 | // FIXME: use drain_filter once it hits stable. | ||
808 | res = res | ||
809 | .into_iter() | ||
810 | .filter_map(|it| match it { | ||
811 | cmd @ lsp_types::CodeActionOrCommand::Command(_) => Some(cmd), | ||
812 | lsp_types::CodeActionOrCommand::CodeAction(action) => match action.command { | ||
813 | Some(cmd) if action.edit.is_none() => { | ||
814 | Some(lsp_types::CodeActionOrCommand::Command(cmd)) | ||
815 | } | ||
816 | _ => None, | ||
817 | }, | ||
818 | }) | ||
819 | .collect(); | ||
820 | } | ||
818 | Ok(Some(res)) | 821 | Ok(Some(res)) |
819 | } | 822 | } |
820 | 823 | ||
@@ -878,8 +881,14 @@ pub fn handle_code_lens( | |||
878 | .map(|it| { | 881 | .map(|it| { |
879 | let range = it.node_range.conv_with(&line_index); | 882 | let range = it.node_range.conv_with(&line_index); |
880 | let pos = range.start; | 883 | let pos = range.start; |
881 | let lens_params = | 884 | let lens_params = req::GotoImplementationParams { |
882 | req::TextDocumentPositionParams::new(params.text_document.clone(), pos); | 885 | text_document_position_params: req::TextDocumentPositionParams::new( |
886 | params.text_document.clone(), | ||
887 | pos, | ||
888 | ), | ||
889 | work_done_progress_params: Default::default(), | ||
890 | partial_result_params: Default::default(), | ||
891 | }; | ||
883 | CodeLens { | 892 | CodeLens { |
884 | range, | 893 | range, |
885 | command: None, | 894 | command: None, |
@@ -894,7 +903,7 @@ pub fn handle_code_lens( | |||
894 | #[derive(Debug, Serialize, Deserialize)] | 903 | #[derive(Debug, Serialize, Deserialize)] |
895 | #[serde(rename_all = "camelCase")] | 904 | #[serde(rename_all = "camelCase")] |
896 | enum CodeLensResolveData { | 905 | enum CodeLensResolveData { |
897 | Impls(req::TextDocumentPositionParams), | 906 | Impls(req::GotoImplementationParams), |
898 | } | 907 | } |
899 | 908 | ||
900 | pub fn handle_code_lens_resolve(world: WorldSnapshot, code_lens: CodeLens) -> Result<CodeLens> { | 909 | pub fn handle_code_lens_resolve(world: WorldSnapshot, code_lens: CodeLens) -> Result<CodeLens> { |
@@ -927,7 +936,7 @@ pub fn handle_code_lens_resolve(world: WorldSnapshot, code_lens: CodeLens) -> Re | |||
927 | title, | 936 | title, |
928 | command: "rust-analyzer.showReferences".into(), | 937 | command: "rust-analyzer.showReferences".into(), |
929 | arguments: Some(vec![ | 938 | arguments: Some(vec![ |
930 | to_value(&lens_params.text_document.uri).unwrap(), | 939 | to_value(&lens_params.text_document_position_params.text_document.uri).unwrap(), |
931 | to_value(code_lens.range.start).unwrap(), | 940 | to_value(code_lens.range.start).unwrap(), |
932 | to_value(locations).unwrap(), | 941 | to_value(locations).unwrap(), |
933 | ]), | 942 | ]), |
@@ -944,16 +953,16 @@ pub fn handle_code_lens_resolve(world: WorldSnapshot, code_lens: CodeLens) -> Re | |||
944 | 953 | ||
945 | pub fn handle_document_highlight( | 954 | pub fn handle_document_highlight( |
946 | world: WorldSnapshot, | 955 | world: WorldSnapshot, |
947 | params: req::TextDocumentPositionParams, | 956 | params: req::DocumentHighlightParams, |
948 | ) -> Result<Option<Vec<DocumentHighlight>>> { | 957 | ) -> Result<Option<Vec<DocumentHighlight>>> { |
949 | let _p = profile("handle_document_highlight"); | 958 | let _p = profile("handle_document_highlight"); |
950 | let file_id = params.text_document.try_conv_with(&world)?; | 959 | let file_id = params.text_document_position_params.text_document.try_conv_with(&world)?; |
951 | let line_index = world.analysis().file_line_index(file_id)?; | 960 | let line_index = world.analysis().file_line_index(file_id)?; |
952 | 961 | ||
953 | let refs = match world | 962 | let refs = match world.analysis().find_all_refs( |
954 | .analysis() | 963 | params.text_document_position_params.try_conv_with(&world)?, |
955 | .find_all_refs(params.try_conv_with(&world)?, Some(SearchScope::single_file(file_id)))? | 964 | Some(SearchScope::single_file(file_id)), |
956 | { | 965 | )? { |
957 | None => return Ok(None), | 966 | None => return Ok(None), |
958 | Some(refs) => refs, | 967 | Some(refs) => refs, |
959 | }; | 968 | }; |