@@ -19,23 +19,21 @@ use crate::{AssistContext, AssistId, Assists};
19
19
// fn foo() -> Result<i32, > { Ok(42i32) }
20
20
// ```
21
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 > ( ) ?;
23
- let ret_type = & fn_def. ret_type ( ) ?. type_ref ( ) ?;
24
- if ret_type. syntax ( ) . text ( ) . to_string ( ) . starts_with ( "Result<" ) {
22
+ let ret_type = ctx. find_node_at_offset :: < ast:: RetType > ( ) ?;
23
+ // FIXME: extend to lambdas as well
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
28
return None ;
26
29
}
27
30
28
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
33
acc. add (
36
34
AssistId ( "change_return_type_to_result" ) ,
37
35
"Change return type to Result" ,
38
- ret_type . syntax ( ) . text_range ( ) ,
36
+ type_ref . syntax ( ) . text_range ( ) ,
39
37
|edit| {
40
38
let mut tail_return_expr_collector = TailReturnCollector :: new ( ) ;
41
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
42
for ret_expr_arg in tail_return_expr_collector. exprs_to_wrap {
45
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 ) {
50
- edit. set_cursor ( node_start + TextSize :: of ( & format ! ( "Result<{}, " , ret_type ) ) ) ;
47
+ if let Some ( node_start) = result_insertion_offset ( & type_ref ) {
48
+ edit. set_cursor ( node_start + TextSize :: of ( & format ! ( "Result<{}, " , type_ref ) ) ) ;
51
49
}
52
50
} ,
53
51
)
0 commit comments