diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/handlers/change_return_type_to_result.rs | 22 |
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 | // ``` |
21 | pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 21 | pub(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 | ) |