Skip to content

Commit bf1043c

Browse files
bors[bot]repnop
andauthored
Merge #6128
6128: Trim all trailing whitespace in onEnter r=matklad a=repnop Fixes #5848 Co-authored-by: Wesley Norris <repnop@outlook.com>
2 parents 6409d7e + adb3c56 commit bf1043c

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

crates/ide/src/typing/on_enter.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)