From fae89605547d684d39e0065fc4286f8608428d5f Mon Sep 17 00:00:00 2001 From: Alan Du Date: Fri, 4 Jan 2019 13:36:31 -0500 Subject: Remove extra space when joining lines in use items --- crates/ra_editor/src/typing.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index dd3d0f260..5876f9e20 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -256,6 +256,11 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Opti fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { match left.kind() { L_PAREN | L_BRACK => return "", + L_CURLY => { + if let USE_TREE = right.kind() { + return ""; + } + } _ => (), } match right.kind() { @@ -330,6 +335,20 @@ fn foo() { ); } + #[test] + fn test_join_lines_use_items() { + // No space after the '{' + check_join_lines( + r" +<|>use ra_syntax::{ + TextUnit, TextRange, +};", + r" +<|>use ra_syntax::{TextUnit, TextRange, +};", + ); + } + #[test] fn test_join_lines_use_tree() { check_join_lines( -- cgit v1.2.3 From 182ec76f135d0b213d6abea486521e81796bca46 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Fri, 4 Jan 2019 22:01:11 -0500 Subject: Address join lines use items right } --- crates/ra_editor/src/typing.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 5876f9e20..2befd5266 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -188,10 +188,14 @@ fn remove_newline( edit.delete(TextRange::from_to(prev.range().start(), node.range().end())); } else if prev.kind() == COMMA && next.kind() == R_CURLY { // Removes: comma, newline (incl. surrounding whitespace) - // Adds: a single whitespace + let space = if let Some(USE_TREE) = prev.prev_sibling().map(|p| p.kind()) { + "" + } else { + " " + }; edit.replace( TextRange::from_to(prev.range().start(), node.range().end()), - " ".to_string(), + space.to_string(), ); } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) { // Removes: newline (incl. surrounding whitespace), start of the next comment @@ -336,7 +340,7 @@ fn foo() { } #[test] - fn test_join_lines_use_items() { + fn test_join_lines_use_items_left() { // No space after the '{' check_join_lines( r" @@ -349,6 +353,20 @@ fn foo() { ); } + #[test] + fn test_join_lines_use_items_right() { + // No space after the '{' + check_join_lines( + r" +use ra_syntax::{ +<|> TextUnit, TextRange, +};", + r" +use ra_syntax::{ +<|> TextUnit, TextRange};", + ); + } + #[test] fn test_join_lines_use_tree() { check_join_lines( -- cgit v1.2.3 From 19c641390d2d152cddf9616aef51fa291260d289 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Fri, 4 Jan 2019 22:06:36 -0500 Subject: Fix join_lines use_items right w/ and w/o comma --- crates/ra_editor/src/typing.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 2befd5266..1b568e96c 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -188,8 +188,8 @@ fn remove_newline( edit.delete(TextRange::from_to(prev.range().start(), node.range().end())); } else if prev.kind() == COMMA && next.kind() == R_CURLY { // Removes: comma, newline (incl. surrounding whitespace) - let space = if let Some(USE_TREE) = prev.prev_sibling().map(|p| p.kind()) { - "" + let space = if let Some(left) = prev.prev_sibling() { + compute_ws(left, next) } else { " " }; @@ -269,6 +269,11 @@ fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { } match right.kind() { R_PAREN | R_BRACK => return "", + R_CURLY => { + if let USE_TREE = left.kind() { + return ""; + } + } DOT => return "", _ => (), } @@ -355,7 +360,21 @@ fn foo() { #[test] fn test_join_lines_use_items_right() { - // No space after the '{' + // No space after the '}' + check_join_lines( + r" +use ra_syntax::{ +<|> TextUnit, TextRange +};", + r" +use ra_syntax::{ +<|> TextUnit, TextRange};", + ); + } + + #[test] + fn test_join_lines_use_items_right_comma() { + // No space after the '}' check_join_lines( r" use ra_syntax::{ -- cgit v1.2.3