mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-24 12:03:31 +00:00
Merge #6128
6128: Trim all trailing whitespace in onEnter r=matklad a=repnop Fixes #5848 Co-authored-by: Wesley Norris <repnop@outlook.com>
This commit is contained in:
commit
bf1043cac2
1 changed files with 25 additions and 4 deletions
|
@ -51,12 +51,12 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Text
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut remove_last_space = false;
|
let mut remove_trailing_whitespace = false;
|
||||||
// Continuing single-line non-doc comments (like this one :) ) is annoying
|
// Continuing single-line non-doc comments (like this one :) ) is annoying
|
||||||
if prefix == "//" && comment_range.end() == position.offset {
|
if prefix == "//" && comment_range.end() == position.offset {
|
||||||
if comment.text().ends_with(' ') {
|
if comment.text().ends_with(' ') {
|
||||||
mark::hit!(continues_end_of_line_comment_with_space);
|
mark::hit!(continues_end_of_line_comment_with_space);
|
||||||
remove_last_space = true;
|
remove_trailing_whitespace = true;
|
||||||
} else if !followed_by_comment(&comment) {
|
} else if !followed_by_comment(&comment) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -64,8 +64,10 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Text
|
||||||
|
|
||||||
let indent = node_indent(&file, comment.syntax())?;
|
let indent = node_indent(&file, comment.syntax())?;
|
||||||
let inserted = format!("\n{}{} $0", indent, prefix);
|
let inserted = format!("\n{}{} $0", indent, prefix);
|
||||||
let delete = if remove_last_space {
|
let delete = if remove_trailing_whitespace {
|
||||||
TextRange::new(position.offset - TextSize::of(' '), position.offset)
|
let trimmed_len = comment.text().trim_end().len() as u32;
|
||||||
|
let trailing_whitespace_len = comment.text().len() as u32 - trimmed_len;
|
||||||
|
TextRange::new(position.offset - TextSize::from(trailing_whitespace_len), position.offset)
|
||||||
} else {
|
} else {
|
||||||
TextRange::empty(position.offset)
|
TextRange::empty(position.offset)
|
||||||
};
|
};
|
||||||
|
@ -253,4 +255,23 @@ fn main() {
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trims_all_trailing_whitespace() {
|
||||||
|
do_check(
|
||||||
|
"
|
||||||
|
fn main() {
|
||||||
|
// Fix me \t\t <|>
|
||||||
|
let x = 1 + 1;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
"
|
||||||
|
fn main() {
|
||||||
|
// Fix me
|
||||||
|
// $0
|
||||||
|
let x = 1 + 1;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue