@@ -51,21 +51,23 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Text
51
51
return None ;
52
52
}
53
53
54
- let mut remove_last_space = false ;
54
+ let mut remove_trailing_whitespace = false ;
55
55
// Continuing single-line non-doc comments (like this one :) ) is annoying
56
56
if prefix == "//" && comment_range. end ( ) == position. offset {
57
57
if comment. text ( ) . ends_with ( ' ' ) {
58
58
mark:: hit!( continues_end_of_line_comment_with_space) ;
59
- remove_last_space = true ;
59
+ remove_trailing_whitespace = true ;
60
60
} else if !followed_by_comment ( & comment) {
61
61
return None ;
62
62
}
63
63
}
64
64
65
65
let indent = node_indent ( & file, comment. syntax ( ) ) ?;
66
66
let inserted = format ! ( "\n {}{} $0" , indent, prefix) ;
67
- let delete = if remove_last_space {
68
- TextRange :: new ( position. offset - TextSize :: of ( ' ' ) , position. offset )
67
+ let delete = if remove_trailing_whitespace {
68
+ let trimmed_len = comment. text ( ) . trim_end ( ) . len ( ) as u32 ;
69
+ let trailing_whitespace_len = comment. text ( ) . len ( ) as u32 - trimmed_len;
70
+ TextRange :: new ( position. offset - TextSize :: from ( trailing_whitespace_len) , position. offset )
69
71
} else {
70
72
TextRange :: empty ( position. offset )
71
73
} ;
@@ -253,4 +255,23 @@ fn main() {
253
255
"# ,
254
256
) ;
255
257
}
258
+
259
+ #[ test]
260
+ fn trims_all_trailing_whitespace ( ) {
261
+ do_check (
262
+ "
263
+ fn main() {
264
+ // Fix me \t \t <|>
265
+ let x = 1 + 1;
266
+ }
267
+ " ,
268
+ "
269
+ fn main() {
270
+ // Fix me
271
+ // $0
272
+ let x = 1 + 1;
273
+ }
274
+ " ,
275
+ ) ;
276
+ }
256
277
}
0 commit comments