Skip to content

Commit 3f01ede

Browse files
bors[bot]matklad
andauthored
Merge #8779
8779: fix: join lines doesn't add space before closing quote r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents b43921c + e0da3da commit 3f01ede

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

crates/ide/src/join_lines.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ fn remove_newlines(edit: &mut TextEditBuilder, token: &SyntaxToken, range: TextR
6565

6666
fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextSize) {
6767
if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 {
68-
let mut string_open_quote = false;
68+
let mut no_space = false;
6969
if let Some(string) = ast::String::cast(token.clone()) {
7070
if let Some(range) = string.open_quote_text_range() {
71-
cov_mark::hit!(join_string_literal);
72-
string_open_quote = range.end() == offset;
71+
cov_mark::hit!(join_string_literal_open_quote);
72+
no_space |= range.end() == offset;
73+
}
74+
if let Some(range) = string.close_quote_text_range() {
75+
cov_mark::hit!(join_string_literal_close_quote);
76+
no_space |= range.start() == offset + TextSize::of('\n');
7377
}
7478
}
7579

@@ -82,7 +86,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS
8286
};
8387

8488
let range = TextRange::at(offset, ((n_spaces_after_line_break + 1) as u32).into());
85-
let replace_with = if string_open_quote { "" } else { " " };
89+
let replace_with = if no_space { "" } else { " " };
8690
edit.replace(range, replace_with.to_string());
8791
return;
8892
}
@@ -797,22 +801,41 @@ fn foo() {
797801

798802
#[test]
799803
fn join_string_literal() {
800-
cov_mark::check!(join_string_literal);
801-
check_join_lines(
802-
r#"
804+
{
805+
cov_mark::check!(join_string_literal_open_quote);
806+
check_join_lines(
807+
r#"
803808
fn main() {
804809
$0"
805810
hello
806811
";
807812
}
808813
"#,
809-
r#"
814+
r#"
810815
fn main() {
811816
$0"hello
812817
";
813818
}
814819
"#,
815-
);
820+
);
821+
}
822+
823+
{
824+
cov_mark::check!(join_string_literal_close_quote);
825+
check_join_lines(
826+
r#"
827+
fn main() {
828+
$0"hello
829+
";
830+
}
831+
"#,
832+
r#"
833+
fn main() {
834+
$0"hello";
835+
}
836+
"#,
837+
);
838+
}
816839

817840
check_join_lines(
818841
r#"

0 commit comments

Comments
 (0)