From adfed5c6894602159ca76dd14d9ed26d76c1cc1b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Feb 2020 18:33:25 +0100 Subject: Fix join lines when two rules match --- crates/ra_ide/src/join_lines.rs | 69 +++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index 7deeb3494..552d7f66e 100644 --- a/crates/ra_ide/src/join_lines.rs +++ b/crates/ra_ide/src/join_lines.rs @@ -60,29 +60,6 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU return; } - // Special case that turns something like: - // - // ``` - // my_function({<|> - // - // }) - // ``` - // - // into `my_function()` - if join_single_expr_block(edit, token).is_some() { - return; - } - // ditto for - // - // ``` - // use foo::{<|> - // bar - // }; - // ``` - if join_single_use_tree(edit, token).is_some() { - return; - } - // The node is between two other nodes let prev = token.prev_sibling_or_token().unwrap(); let next = token.next_sibling_or_token().unwrap(); @@ -110,6 +87,29 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), )); } else { + // Special case that turns something like: + // + // ``` + // my_function({<|> + // + // }) + // ``` + // + // into `my_function()` + if join_single_expr_block(edit, token).is_some() { + return; + } + // ditto for + // + // ``` + // use foo::{<|> + // bar + // }; + // ``` + if join_single_use_tree(edit, token).is_some() { + return; + } + // Remove newline but add a computed amount of whitespace characters edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); } @@ -608,4 +608,27 @@ pub fn handle_find_matching_brace() { }", ); } + + #[test] + fn test_join_lines_commented_block() { + check_join_lines( + r" +fn main() { + let _ = { + // <|>foo + // bar + 92 + }; +} + ", + r" +fn main() { + let _ = { + // <|>foo bar + 92 + }; +} + ", + ) + } } -- cgit v1.2.3