aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-20 09:28:58 +0100
committerAleksey Kladov <[email protected]>2020-05-20 09:28:58 +0100
commit2e74df4e2b79955a44dee79811e06b8fabff4da2 (patch)
tree39f82e22486c8ed3c35485f36f537531342d4762
parent36a5ca9a843aa807b8152d4c5631be605fc80e80 (diff)
Cleanup
-rw-r--r--crates/ra_assists/src/handlers/change_return_type_to_result.rs22
1 files changed, 10 insertions, 12 deletions
diff --git a/crates/ra_assists/src/handlers/change_return_type_to_result.rs b/crates/ra_assists/src/handlers/change_return_type_to_result.rs
index c2ea87dd7..ae8237c5d 100644
--- a/crates/ra_assists/src/handlers/change_return_type_to_result.rs
+++ b/crates/ra_assists/src/handlers/change_return_type_to_result.rs
@@ -19,23 +19,21 @@ use crate::{AssistContext, AssistId, Assists};
19// fn foo() -> Result<i32, > { Ok(42i32) } 19// fn foo() -> Result<i32, > { Ok(42i32) }
20// ``` 20// ```
21pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 21pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
22 let fn_def = ctx.find_node_at_offset::<ast::FnDef>()?; 22 let ret_type = ctx.find_node_at_offset::<ast::RetType>()?;
23 let ret_type = &fn_def.ret_type()?.type_ref()?; 23 // FIXME: extend to lambdas as well
24 if ret_type.syntax().text().to_string().starts_with("Result<") { 24 let fn_def = ret_type.syntax().parent().and_then(ast::FnDef::cast)?;
25
26 let type_ref = &ret_type.type_ref()?;
27 if type_ref.syntax().text().to_string().starts_with("Result<") {
25 return None; 28 return None;
26 } 29 }
27 30
28 let block_expr = &fn_def.body()?; 31 let block_expr = &fn_def.body()?;
29 let cursor_in_ret_type =
30 fn_def.ret_type()?.syntax().text_range().contains_range(ctx.frange.range);
31 if !cursor_in_ret_type {
32 return None;
33 }
34 32
35 acc.add( 33 acc.add(
36 AssistId("change_return_type_to_result"), 34 AssistId("change_return_type_to_result"),
37 "Change return type to Result", 35 "Change return type to Result",
38 ret_type.syntax().text_range(), 36 type_ref.syntax().text_range(),
39 |edit| { 37 |edit| {
40 let mut tail_return_expr_collector = TailReturnCollector::new(); 38 let mut tail_return_expr_collector = TailReturnCollector::new();
41 tail_return_expr_collector.collect_jump_exprs(block_expr, false); 39 tail_return_expr_collector.collect_jump_exprs(block_expr, false);
@@ -44,10 +42,10 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
44 for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap { 42 for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
45 edit.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg)); 43 edit.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg));
46 } 44 }
47 edit.replace_node_and_indent(ret_type.syntax(), format!("Result<{}, >", ret_type)); 45 edit.replace_node_and_indent(type_ref.syntax(), format!("Result<{}, >", type_ref));
48 46
49 if let Some(node_start) = result_insertion_offset(&ret_type) { 47 if let Some(node_start) = result_insertion_offset(&type_ref) {
50 edit.set_cursor(node_start + TextSize::of(&format!("Result<{}, ", ret_type))); 48 edit.set_cursor(node_start + TextSize::of(&format!("Result<{}, ", type_ref)));
51 } 49 }
52 }, 50 },
53 ) 51 )