diff options
Diffstat (limited to 'crates/ide/src/join_lines.rs')
-rw-r--r-- | crates/ide/src/join_lines.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index 4b25135cd..fe2a349e6 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs | |||
@@ -19,6 +19,8 @@ use text_edit::{TextEdit, TextEditBuilder}; | |||
19 | // | 19 | // |
20 | // | VS Code | **Rust Analyzer: Join lines** | 20 | // | VS Code | **Rust Analyzer: Join lines** |
21 | // |=== | 21 | // |=== |
22 | // | ||
23 | // image::https://user-images.githubusercontent.com/48062697/113020661-b6922200-917a-11eb-87c4-b75acc028f11.gif[] | ||
22 | pub(crate) fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { | 24 | pub(crate) fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { |
23 | let range = if range.is_empty() { | 25 | let range = if range.is_empty() { |
24 | let syntax = file.syntax(); | 26 | let syntax = file.syntax(); |
@@ -86,8 +88,11 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS | |||
86 | } | 88 | } |
87 | 89 | ||
88 | // The node is between two other nodes | 90 | // The node is between two other nodes |
89 | let prev = token.prev_sibling_or_token().unwrap(); | 91 | let (prev, next) = match (token.prev_sibling_or_token(), token.next_sibling_or_token()) { |
90 | let next = token.next_sibling_or_token().unwrap(); | 92 | (Some(prev), Some(next)) => (prev, next), |
93 | _ => return, | ||
94 | }; | ||
95 | |||
91 | if is_trailing_comma(prev.kind(), next.kind()) { | 96 | if is_trailing_comma(prev.kind(), next.kind()) { |
92 | // Removes: trailing comma, newline (incl. surrounding whitespace) | 97 | // Removes: trailing comma, newline (incl. surrounding whitespace) |
93 | edit.delete(TextRange::new(prev.text_range().start(), token.text_range().end())); | 98 | edit.delete(TextRange::new(prev.text_range().start(), token.text_range().end())); |
@@ -827,4 +832,15 @@ $0hello world | |||
827 | "#, | 832 | "#, |
828 | ); | 833 | ); |
829 | } | 834 | } |
835 | #[test] | ||
836 | fn join_last_line_empty() { | ||
837 | check_join_lines( | ||
838 | r#" | ||
839 | fn main() {$0} | ||
840 | "#, | ||
841 | r#" | ||
842 | fn main() {$0} | ||
843 | "#, | ||
844 | ); | ||
845 | } | ||
830 | } | 846 | } |