Repository: rust-analyzer/rust-analyzer
Pull request #3786 (2020-03-31 12:53:36)

When adding match arm, don't let the floating comma


bors r+
🤖


Patch:
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index 41bb97928d..8d1af99336 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -1,15 +1,11 @@
-//! FIXME: write short doc here
-
 use std::iter;
 
 use hir::{Adt, HasSource, ModuleDef, Semantics};
 use itertools::Itertools;
 use ra_ide_db::RootDatabase;
+use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
 
 use crate::{Assist, AssistCtx, AssistId};
-use ra_syntax::ast::{self, make, AstNode, NameOwner};
-
-use ast::{MatchArm, Pat};
 
 // Assist: fill_match_arms
 //
@@ -717,4 +713,28 @@ mod tests {
             "#,
         );
     }
+
+    #[test]
+    fn fill_match_arms_placeholder() {
+        check_assist(
+            fill_match_arms,
+            r#"
+            enum A { One, Two, }
+            fn foo(a: A) {
+                match a<|> {
+                    _ => (),
+                }
+            }
+            "#,
+            r#"
+            enum A { One, Two, }
+            fn foo(a: A) {
+                match <|>a {
+                    A::One => {}
+                    A::Two => {}
+                }
+            }
+            "#,
+        );
+    }
 }
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs
index 738fd6f610..291594e2a2 100644
--- a/crates/ra_project_model/src/cargo_workspace.rs
+++ b/crates/ra_project_model/src/cargo_workspace.rs
@@ -75,7 +75,6 @@ pub type Target = Idx<TargetData>;
 
 #[derive(Debug, Clone)]
 pub struct PackageData {
-    pub id: String,
     pub name: String,
     pub manifest: PathBuf,
     pub targets: Vec<Target>,
@@ -181,7 +180,6 @@ impl CargoWorkspace {
                 .with_context(|| format!("Failed to parse edition {}", edition))?;
             let pkg = packages.alloc(PackageData {
                 name,
-                id: id.to_string(),
                 manifest: manifest_path,
                 targets: Vec::new(),
                 is_member,
@@ -251,18 +249,6 @@ impl CargoWorkspace {
     pub fn workspace_root(&self) -> &Path {
         &self.workspace_root
     }
-
-    pub fn package_flag(&self, package: &PackageData) -> String {
-        if self.is_unique(&*package.name) {
-            package.name.clone()
-        } else {
-            package.id.clone()
-        }
-    }
-
-    fn is_unique(&self, name: &str) -> bool {
-        self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
-    }
 }
 
 #[derive(Debug, Clone, Default)]
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 3d023f1891..b69cae234c 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -369,21 +369,32 @@ impl ast::MatchArmList {
 
     #[must_use]
     pub fn remove_placeholder(&self) -> ast::MatchArmList {
-        let placeholder = self.arms().find(|arm| {
-            if let Some(ast::Pat::PlaceholderPat(_)) = arm.pat() {
-                return true;
-            }
-            false
-        });
+        let placeholder =
+            self.arms().find(|arm| matches!(arm.pat(), Some(ast::Pat::PlaceholderPat(_))));
         if let Some(placeholder) = placeholder {
-            let s: SyntaxElement = placeholder.syntax().clone().into();
-            let e = s.clone();
-            self.replace_children(s..=e, &mut iter::empty())
+            self.remove_arm(&placeholder)
         } else {
             self.clone()
         }
     }
 
+    #[must_use]
+    fn remove_arm(&self, arm: &ast::MatchArm) -> ast::MatchArmList {
+        let start = arm.syntax().clone();
+        let end = if let Some(comma) = start
+            .siblings_with_tokens(Direction::Next)
+            .skip(1)
+            .skip_while(|it| it.kind().is_trivia())
+            .next()
+            .filter(|it| it.kind() == T![,])
+        {
+            comma
+        } else {
+            start.clone().into()
+        };
+        self.replace_children(start.into()..=end, None)
+    }
+
     #[must_use]
     pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList {
         let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) {
diff --git a/crates/ra_syntax/src/parsing/text_tree_sink.rs b/crates/ra_syntax/src/parsing/text_tree_sink.rs
index 87bb21cd9a..dd202601d9 100644
--- a/crates/ra_syntax/src/parsing/text_tree_sink.rs
+++ b/crates/ra_syntax/src/parsing/text_tree_sink.rs
@@ -149,21 +149,10 @@ fn n_attached_trivias<'a>(
         MACRO_CALL | CONST_DEF | TYPE_ALIAS_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF
         | TRAIT_DEF | MODULE | RECORD_FIELD_DEF | STATIC_DEF => {
             let mut res = 0;
-            let mut trivias = trivias.enumerate().peekable();
-
-            while let Some((i, (kind, text))) = trivias.next() {
+            for (i, (kind, text)) in trivias.enumerate() {
                 match kind {
                     WHITESPACE => {
                         if text.contains("\n\n") {
-                            // we check whether the next token is a doc-comment
-                            // and skip the whitespace in this case
-                            if let Some((peek_kind, peek_text)) =
-                                trivias.peek().map(|(_, pair)| pair)
-                            {
-                                if *peek_kind == COMMENT && peek_text.starts_with("///") {
-                                    continue;
-                                }
-                            }
                             break;
                         }
                     }
diff --git a/crates/rust-analyzer/src/cargo_target_spec.rs b/crates/rust-analyzer/src/cargo_target_spec.rs
index 942c303287..f87bdcec5c 100644
--- a/crates/rust-analyzer/src/cargo_target_spec.rs
+++ b/crates/rust-analyzer/src/cargo_target_spec.rs
@@ -77,7 +77,7 @@ impl CargoTargetSpec {
             ProjectWorkspace::Cargo { cargo, .. } => {
                 let tgt = cargo.target_by_root(&path)?;
                 Some(CargoTargetSpec {
-                    package: cargo.package_flag(&cargo[cargo[tgt].package]),
+                    package: cargo[cargo[tgt].package].name.clone(),
                     target: cargo[tgt].name.clone(),
                     target_kind: cargo[tgt].kind,
                 })


Test patch:
diff --git a/crates/ra_syntax/test_data/parser/ok/0065_comment_newline.rs b/crates/ra_syntax/test_data/parser/ok/0065_comment_newline.rs
deleted file mode 100644
index 1fafe216b6..0000000000
--- a/crates/ra_syntax/test_data/parser/ok/0065_comment_newline.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-/// Example
-
-fn test() {}
diff --git a/crates/ra_syntax/test_data/parser/ok/0065_comment_newline.txt b/crates/ra_syntax/test_data/parser/ok/0065_comment_newline.txt
deleted file mode 100644
index 91d0c37365..0000000000
--- a/crates/ra_syntax/test_data/parser/ok/0065_comment_newline.txt
+++ /dev/null
@@ -1,17 +0,0 @@
-SOURCE_FILE@[0; 26)
-  FN_DEF@[0; 25)
-    COMMENT@[0; 11) "/// Example"
-    WHITESPACE@[11; 13) "\n\n"
-    FN_KW@[13; 15) "fn"
-    WHITESPACE@[15; 16) " "
-    NAME@[16; 20)
-      IDENT@[16; 20) "test"
-    PARAM_LIST@[20; 22)
-      L_PAREN@[20; 21) "("
-      R_PAREN@[21; 22) ")"
-    WHITESPACE@[22; 23) " "
-    BLOCK_EXPR@[23; 25)
-      BLOCK@[23; 25)
-        L_CURLY@[23; 24) "{"
-        R_CURLY@[24; 25) "}"
-  WHITESPACE@[25; 26) "\n"