about summary refs log tree commit diff
diff options
context:
space:
mode:
authorrainy-me <github@yue.coffee>2021-11-14 02:32:10 +0900
committerrainy-me <github@yue.coffee>2021-11-14 02:32:10 +0900
commitc9949c040c53534344c5efbd437d169e6dba2a47 (patch)
treed1a8c9e39a9e8a08c798257c80e828d129e27e77
parentd1e756e05aab5410f6176fce26bf021453708b9b (diff)
downloadrust-c9949c040c53534344c5efbd437d169e6dba2a47.tar.gz
rust-c9949c040c53534344c5efbd437d169e6dba2a47.zip
add missing match arms end of last arm
-rw-r--r--crates/ide_assists/src/handlers/add_missing_match_arms.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/crates/ide_assists/src/handlers/add_missing_match_arms.rs b/crates/ide_assists/src/handlers/add_missing_match_arms.rs
index 463fb1c914a..f16112feeff 100644
--- a/crates/ide_assists/src/handlers/add_missing_match_arms.rs
+++ b/crates/ide_assists/src/handlers/add_missing_match_arms.rs
@@ -178,6 +178,19 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
                             None => Cursor::Before(first_new_arm.syntax()),
                         };
                     let snippet = render_snippet(cap, new_match_arm_list.syntax(), cursor);
+                    // remove the second last line if it only contains trailing whitespace
+                    let lines = snippet.lines().collect_vec();
+                    let snippet = lines
+                        .iter()
+                        .enumerate()
+                        .filter_map(|(index, &line)| {
+                            if index + 2 == lines.len() && line.trim().is_empty() {
+                                return None;
+                            }
+                            return Some(line);
+                        })
+                        .join("\n");
+
                     builder.replace_snippet(cap, old_range, snippet);
                 }
                 _ => builder.replace(old_range, new_match_arm_list.to_string()),
@@ -197,6 +210,19 @@ fn cursor_at_trivial_match_arm_list(
         return Some(());
     }
 
+    // match x {
+    //     bar => baz,
+    //     $0
+    // }
+    if let Some(last_arm) = match_arm_list.arms().last() {
+        let last_arm_range = last_arm.syntax().text_range();
+        let match_expr_range = match_expr.syntax().text_range();
+        if last_arm_range.end() <= ctx.offset() && ctx.offset() < match_expr_range.end() {
+            cov_mark::hit!(add_missing_match_arms_end_of_last_arm);
+            return Some(());
+        }
+    }
+
     // match { _$0 => {...} }
     let wild_pat = ctx.find_node_at_offset_with_descend::<ast::WildcardPat>()?;
     let arm = wild_pat.syntax().parent().and_then(ast::MatchArm::cast)?;
@@ -677,6 +703,42 @@ fn main() {
     }
 
     #[test]
+    fn add_missing_match_arms_end_of_last_arm() {
+        cov_mark::check!(add_missing_match_arms_end_of_last_arm);
+        check_assist(
+            add_missing_match_arms,
+            r#"
+enum A { One, Two }
+enum B { One, Two }
+
+fn main() {
+    let a = A::One;
+    let b = B::One;
+    match (a, b) {
+        (A::Two, B::One) => {},
+        $0
+    }
+}
+"#,
+            r#"
+enum A { One, Two }
+enum B { One, Two }
+
+fn main() {
+    let a = A::One;
+    let b = B::One;
+    match (a, b) {
+        (A::Two, B::One) => {},
+        $0(A::One, B::One) => todo!(),
+        (A::One, B::Two) => todo!(),
+        (A::Two, B::Two) => todo!(),
+    }
+}
+"#,
+        );
+    }
+
+    #[test]
     fn add_missing_match_arms_tuple_of_enum() {
         check_assist(
             add_missing_match_arms,