@@ -51,21 +51,23 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Text
5151 return None ;
5252 }
5353
54- let mut remove_last_space = false ;
54+ let mut remove_trailing_whitespace = false ;
5555 // Continuing single-line non-doc comments (like this one :) ) is annoying
5656 if prefix == "//" && comment_range. end ( ) == position. offset {
5757 if comment. text ( ) . ends_with ( ' ' ) {
5858 mark:: hit!( continues_end_of_line_comment_with_space) ;
59- remove_last_space = true ;
59+ remove_trailing_whitespace = true ;
6060 } else if !followed_by_comment ( & comment) {
6161 return None ;
6262 }
6363 }
6464
6565 let indent = node_indent ( & file, comment. syntax ( ) ) ?;
6666 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 )
6971 } else {
7072 TextRange :: empty ( position. offset )
7173 } ;
@@ -253,4 +255,23 @@ fn main() {
253255"# ,
254256 ) ;
255257 }
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+ }
256277}
0 commit comments