@@ -16,7 +16,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
16
16
// fn foo() -> i32 { 42i32 }
17
17
// ```
18
18
pub ( crate ) fn add_return_type ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
19
- let ( fn_type, tail_expr, builder_edit_pos, needs_whitespace ) = extract_tail ( ctx) ?;
19
+ let ( fn_type, tail_expr, builder_edit_pos) = extract_tail ( ctx) ?;
20
20
let module = ctx. sema . scope ( tail_expr. syntax ( ) ) . module ( ) ?;
21
21
let ty = ctx. sema . type_of_expr ( & tail_expr) ?. adjusted ( ) ;
22
22
if ty. is_unit ( ) {
@@ -32,14 +32,13 @@ pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<
32
32
} ,
33
33
tail_expr. syntax ( ) . text_range ( ) ,
34
34
|builder| {
35
- let preceeding_whitespace = if needs_whitespace { " " } else { "" } ;
36
-
37
35
match builder_edit_pos {
38
- InsertOrReplace :: Insert ( insert_pos) => {
36
+ InsertOrReplace :: Insert ( insert_pos, needs_whitespace) => {
37
+ let preceeding_whitespace = if needs_whitespace { " " } else { "" } ;
39
38
builder. insert ( insert_pos, & format ! ( "{}-> {} " , preceeding_whitespace, ty) )
40
39
}
41
40
InsertOrReplace :: Replace ( text_range) => {
42
- builder. replace ( text_range, & format ! ( "{} -> {}" , preceeding_whitespace , ty) )
41
+ builder. replace ( text_range, & format ! ( "-> {}" , ty) )
43
42
}
44
43
}
45
44
if let FnType :: Closure { wrap_expr : true } = fn_type {
@@ -52,7 +51,7 @@ pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<
52
51
}
53
52
54
53
enum InsertOrReplace {
55
- Insert ( TextSize ) ,
54
+ Insert ( TextSize , bool ) ,
56
55
Replace ( TextRange ) ,
57
56
}
58
57
@@ -61,13 +60,13 @@ enum InsertOrReplace {
61
60
fn ret_ty_to_action (
62
61
ret_ty : Option < ast:: RetType > ,
63
62
insert_after : SyntaxToken ,
64
- ) -> Option < ( InsertOrReplace , bool ) > {
63
+ ) -> Option < InsertOrReplace > {
65
64
match ret_ty {
66
65
Some ( ret_ty) => match ret_ty. ty ( ) {
67
66
Some ( ast:: Type :: InferType ( _) ) | None => {
68
67
cov_mark:: hit!( existing_infer_ret_type) ;
69
68
cov_mark:: hit!( existing_infer_ret_type_closure) ;
70
- Some ( ( InsertOrReplace :: Replace ( ret_ty. syntax ( ) . text_range ( ) ) , false ) )
69
+ Some ( InsertOrReplace :: Replace ( ret_ty. syntax ( ) . text_range ( ) ) )
71
70
}
72
71
_ => {
73
72
cov_mark:: hit!( existing_ret_type) ;
@@ -84,7 +83,7 @@ fn ret_ty_to_action(
84
83
_ => ( insert_after_pos, true ) ,
85
84
} ;
86
85
87
- Some ( ( InsertOrReplace :: Insert ( insert_pos) , needs_whitespace) )
86
+ Some ( InsertOrReplace :: Insert ( insert_pos, needs_whitespace) )
88
87
}
89
88
}
90
89
}
@@ -94,13 +93,13 @@ enum FnType {
94
93
Closure { wrap_expr : bool } ,
95
94
}
96
95
97
- fn extract_tail ( ctx : & AssistContext ) -> Option < ( FnType , ast:: Expr , InsertOrReplace , bool ) > {
98
- let ( fn_type, tail_expr, return_type_range, action, needs_whitespace ) =
96
+ fn extract_tail ( ctx : & AssistContext ) -> Option < ( FnType , ast:: Expr , InsertOrReplace ) > {
97
+ let ( fn_type, tail_expr, return_type_range, action) =
99
98
if let Some ( closure) = ctx. find_node_at_offset :: < ast:: ClosureExpr > ( ) {
100
99
let rpipe = closure. param_list ( ) ?. syntax ( ) . last_token ( ) ?;
101
100
let rpipe_pos = rpipe. text_range ( ) . end ( ) ;
102
101
103
- let ( action, needs_whitespace ) = ret_ty_to_action ( closure. ret_type ( ) , rpipe) ?;
102
+ let action = ret_ty_to_action ( closure. ret_type ( ) , rpipe) ?;
104
103
105
104
let body = closure. body ( ) ?;
106
105
let body_start = body. syntax ( ) . first_token ( ) ?. text_range ( ) . start ( ) ;
@@ -110,21 +109,21 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
110
109
} ;
111
110
112
111
let ret_range = TextRange :: new ( rpipe_pos, body_start) ;
113
- ( FnType :: Closure { wrap_expr } , tail_expr, ret_range, action, needs_whitespace )
112
+ ( FnType :: Closure { wrap_expr } , tail_expr, ret_range, action)
114
113
} else {
115
114
let func = ctx. find_node_at_offset :: < ast:: Fn > ( ) ?;
116
115
117
116
let rparen = func. param_list ( ) ?. r_paren_token ( ) ?;
118
117
let rparen_pos = rparen. text_range ( ) . end ( ) ;
119
- let ( action, needs_whitespace ) = ret_ty_to_action ( func. ret_type ( ) , rparen) ?;
118
+ let action = ret_ty_to_action ( func. ret_type ( ) , rparen) ?;
120
119
121
120
let body = func. body ( ) ?;
122
121
let stmt_list = body. stmt_list ( ) ?;
123
122
let tail_expr = stmt_list. tail_expr ( ) ?;
124
123
125
124
let ret_range_end = stmt_list. l_curly_token ( ) ?. text_range ( ) . start ( ) ;
126
125
let ret_range = TextRange :: new ( rparen_pos, ret_range_end) ;
127
- ( FnType :: Function , tail_expr, ret_range, action, needs_whitespace )
126
+ ( FnType :: Function , tail_expr, ret_range, action)
128
127
} ;
129
128
let range = ctx. selection_trimmed ( ) ;
130
129
if return_type_range. contains_range ( range) {
@@ -136,7 +135,7 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
136
135
} else {
137
136
return None ;
138
137
}
139
- Some ( ( fn_type, tail_expr, action, needs_whitespace ) )
138
+ Some ( ( fn_type, tail_expr, action) )
140
139
}
141
140
142
141
#[ cfg( test) ]
0 commit comments