Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide_assists/src/handlers/add_missing_match_arms.rs')
-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 463fb1c914..f16112feef 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,