1
1
use ide_db:: helpers:: { for_each_tail_expr, node_ext:: walk_expr, FamousDefs } ;
2
+ use itertools:: Itertools ;
2
3
use syntax:: {
3
4
ast:: { self , Expr } ,
4
5
match_ast, AstNode , TextRange , TextSize ,
@@ -73,7 +74,7 @@ pub(crate) fn unwrap_result_return_type(acc: &mut Assists, ctx: &AssistContext)
73
74
ret_type. syntax ( ) . text_range ( ) . start ( ) ,
74
75
ret_type. syntax ( ) . text_range ( ) . end ( ) + TextSize :: from ( 1u32 ) ,
75
76
) ;
76
- builder. replace ( text_range, "" )
77
+ builder. delete ( text_range)
77
78
} else {
78
79
builder. replace (
79
80
type_ref. syntax ( ) . text_range ( ) ,
@@ -88,14 +89,24 @@ pub(crate) fn unwrap_result_return_type(acc: &mut Assists, ctx: &AssistContext)
88
89
let arg_list = ret_expr_arg. syntax ( ) . children ( ) . find_map ( ast:: ArgList :: cast) ;
89
90
if let Some ( arg_list) = arg_list {
90
91
if is_unit_type {
91
- builder. replace ( ret_expr_arg. syntax ( ) . text_range ( ) , "" ) ;
92
+ match ret_expr_arg. syntax ( ) . prev_sibling_or_token ( ) {
93
+ // Useful to delete the entire line without leaving trailing whitespaces
94
+ Some ( whitespace) => {
95
+ let new_range = TextRange :: new (
96
+ whitespace. text_range ( ) . start ( ) ,
97
+ ret_expr_arg. syntax ( ) . text_range ( ) . end ( ) ,
98
+ ) ;
99
+ builder. delete ( new_range) ;
100
+ }
101
+ None => {
102
+ builder. delete ( ret_expr_arg. syntax ( ) . text_range ( ) ) ;
103
+ }
104
+ }
92
105
} else {
93
- let new_ret_expr = arg_list
94
- . args ( )
95
- . map ( |arg| arg. to_string ( ) )
96
- . collect :: < Vec < String > > ( )
97
- . join ( ", " ) ;
98
- builder. replace ( ret_expr_arg. syntax ( ) . text_range ( ) , new_ret_expr) ;
106
+ builder. replace (
107
+ ret_expr_arg. syntax ( ) . text_range ( ) ,
108
+ arg_list. args ( ) . join ( ", " ) ,
109
+ ) ;
99
110
}
100
111
}
101
112
}
@@ -158,7 +169,6 @@ fn foo() -> Result<(), Box<dyn Error$0>> {
158
169
"# ,
159
170
r#"
160
171
fn foo() {
161
-
162
172
}
163
173
"# ,
164
174
) ;
0 commit comments