]> git.lizzy.rs Git - rust.git/commitdiff
Merge #427
authorbors[bot] <bors[bot]@users.noreply.github.com>
Sat, 5 Jan 2019 07:16:48 +0000 (07:16 +0000)
committerbors[bot] <bors[bot]@users.noreply.github.com>
Sat, 5 Jan 2019 07:16:48 +0000 (07:16 +0000)
427: Remove extra space when joining lines in use items r=matklad a=alanhdu

Fixes #423.

Co-authored-by: Alan Du <alanhdu@gmail.com>
crates/ra_editor/src/typing.rs

index dd3d0f260d7af4c42afdf6ee05e1d47bc1aecd64..1b568e96cdddee4971e1ab849ebaecc4e6f8f9ea 100644 (file)
@@ -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(left) = prev.prev_sibling() {
+            compute_ws(left, next)
+        } 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
@@ -256,10 +260,20 @@ 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() {
         R_PAREN | R_BRACK => return "",
+        R_CURLY => {
+            if let USE_TREE = left.kind() {
+                return "";
+            }
+        }
         DOT => return "",
         _ => (),
     }
@@ -330,6 +344,48 @@ fn foo() {
         );
     }
 
+    #[test]
+    fn test_join_lines_use_items_left() {
+        // 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() {
+        // 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::{
+<|>    TextUnit, TextRange,
+};",
+            r"
+use ra_syntax::{
+<|>    TextUnit, TextRange};",
+        );
+    }
+
     #[test]
     fn test_join_lines_use_tree() {
         check_join_lines(