From b636080f6738dd390fd8a92df5920f5b0df431d0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 3 Apr 2021 11:20:16 +0800 Subject: Fix joinLines panic if run on the empty last line --- crates/ide/src/join_lines.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index d584190f7..fe2a349e6 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs @@ -88,8 +88,11 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS } // The node is between two other nodes - let prev = token.prev_sibling_or_token().unwrap(); - let next = token.next_sibling_or_token().unwrap(); + let (prev, next) = match (token.prev_sibling_or_token(), token.next_sibling_or_token()) { + (Some(prev), Some(next)) => (prev, next), + _ => return, + }; + if is_trailing_comma(prev.kind(), next.kind()) { // Removes: trailing comma, newline (incl. surrounding whitespace) edit.delete(TextRange::new(prev.text_range().start(), token.text_range().end())); @@ -826,6 +829,17 @@ fn main() { $0hello world "; } +"#, + ); + } + #[test] + fn join_last_line_empty() { + check_join_lines( + r#" +fn main() {$0} +"#, + r#" +fn main() {$0} "#, ); } -- cgit v1.2.3