diff options
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/join_lines.rs | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index 482b23cf5..61dcbb399 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::convert::TryFrom; | ||
2 | |||
1 | use ide_assists::utils::extract_trivial_expression; | 3 | use ide_assists::utils::extract_trivial_expression; |
2 | use itertools::Itertools; | 4 | use itertools::Itertools; |
3 | use syntax::{ | 5 | use syntax::{ |
@@ -65,6 +67,14 @@ fn remove_newlines(edit: &mut TextEditBuilder, token: &SyntaxToken, range: TextR | |||
65 | 67 | ||
66 | fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextSize) { | 68 | fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextSize) { |
67 | if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 { | 69 | if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 { |
70 | let n_spaces_after_line_break = { | ||
71 | let suff = &token.text()[TextRange::new( | ||
72 | offset - token.text_range().start() + TextSize::of('\n'), | ||
73 | TextSize::of(token.text()), | ||
74 | )]; | ||
75 | suff.bytes().take_while(|&b| b == b' ').count() | ||
76 | }; | ||
77 | |||
68 | let mut no_space = false; | 78 | let mut no_space = false; |
69 | if let Some(string) = ast::String::cast(token.clone()) { | 79 | if let Some(string) = ast::String::cast(token.clone()) { |
70 | if let Some(range) = string.open_quote_text_range() { | 80 | if let Some(range) = string.open_quote_text_range() { |
@@ -73,18 +83,13 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS | |||
73 | } | 83 | } |
74 | if let Some(range) = string.close_quote_text_range() { | 84 | if let Some(range) = string.close_quote_text_range() { |
75 | cov_mark::hit!(join_string_literal_close_quote); | 85 | cov_mark::hit!(join_string_literal_close_quote); |
76 | no_space |= range.start() == offset + TextSize::of('\n'); | 86 | no_space |= range.start() |
87 | == offset | ||
88 | + TextSize::of('\n') | ||
89 | + TextSize::try_from(n_spaces_after_line_break).unwrap(); | ||
77 | } | 90 | } |
78 | } | 91 | } |
79 | 92 | ||
80 | let n_spaces_after_line_break = { | ||
81 | let suff = &token.text()[TextRange::new( | ||
82 | offset - token.text_range().start() + TextSize::of('\n'), | ||
83 | TextSize::of(token.text()), | ||
84 | )]; | ||
85 | suff.bytes().take_while(|&b| b == b' ').count() | ||
86 | }; | ||
87 | |||
88 | let range = TextRange::at(offset, ((n_spaces_after_line_break + 1) as u32).into()); | 93 | let range = TextRange::at(offset, ((n_spaces_after_line_break + 1) as u32).into()); |
89 | let replace_with = if no_space { "" } else { " " }; | 94 | let replace_with = if no_space { "" } else { " " }; |
90 | edit.replace(range, replace_with.to_string()); | 95 | edit.replace(range, replace_with.to_string()); |
@@ -835,6 +840,19 @@ fn main() { | |||
835 | } | 840 | } |
836 | "#, | 841 | "#, |
837 | ); | 842 | ); |
843 | check_join_lines( | ||
844 | r#" | ||
845 | fn main() { | ||
846 | $0r"hello | ||
847 | "; | ||
848 | } | ||
849 | "#, | ||
850 | r#" | ||
851 | fn main() { | ||
852 | $0r"hello"; | ||
853 | } | ||
854 | "#, | ||
855 | ); | ||
838 | } | 856 | } |
839 | 857 | ||
840 | check_join_lines( | 858 | check_join_lines( |