Skip to content

Commit c9949c0

Browse files
committed
add missing match arms end of last arm
1 parent d1e756e commit c9949c0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

crates/ide_assists/src/handlers/add_missing_match_arms.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
178178
None => Cursor::Before(first_new_arm.syntax()),
179179
};
180180
let snippet = render_snippet(cap, new_match_arm_list.syntax(), cursor);
181+
// remove the second last line if it only contains trailing whitespace
182+
let lines = snippet.lines().collect_vec();
183+
let snippet = lines
184+
.iter()
185+
.enumerate()
186+
.filter_map(|(index, &line)| {
187+
if index + 2 == lines.len() && line.trim().is_empty() {
188+
return None;
189+
}
190+
return Some(line);
191+
})
192+
.join("\n");
193+
181194
builder.replace_snippet(cap, old_range, snippet);
182195
}
183196
_ => builder.replace(old_range, new_match_arm_list.to_string()),
@@ -197,6 +210,19 @@ fn cursor_at_trivial_match_arm_list(
197210
return Some(());
198211
}
199212

213+
// match x {
214+
// bar => baz,
215+
// $0
216+
// }
217+
if let Some(last_arm) = match_arm_list.arms().last() {
218+
let last_arm_range = last_arm.syntax().text_range();
219+
let match_expr_range = match_expr.syntax().text_range();
220+
if last_arm_range.end() <= ctx.offset() && ctx.offset() < match_expr_range.end() {
221+
cov_mark::hit!(add_missing_match_arms_end_of_last_arm);
222+
return Some(());
223+
}
224+
}
225+
200226
// match { _$0 => {...} }
201227
let wild_pat = ctx.find_node_at_offset_with_descend::<ast::WildcardPat>()?;
202228
let arm = wild_pat.syntax().parent().and_then(ast::MatchArm::cast)?;
@@ -676,6 +702,42 @@ fn main() {
676702
);
677703
}
678704

705+
#[test]
706+
fn add_missing_match_arms_end_of_last_arm() {
707+
cov_mark::check!(add_missing_match_arms_end_of_last_arm);
708+
check_assist(
709+
add_missing_match_arms,
710+
r#"
711+
enum A { One, Two }
712+
enum B { One, Two }
713+
714+
fn main() {
715+
let a = A::One;
716+
let b = B::One;
717+
match (a, b) {
718+
(A::Two, B::One) => {},
719+
$0
720+
}
721+
}
722+
"#,
723+
r#"
724+
enum A { One, Two }
725+
enum B { One, Two }
726+
727+
fn main() {
728+
let a = A::One;
729+
let b = B::One;
730+
match (a, b) {
731+
(A::Two, B::One) => {},
732+
$0(A::One, B::One) => todo!(),
733+
(A::One, B::Two) => todo!(),
734+
(A::Two, B::Two) => todo!(),
735+
}
736+
}
737+
"#,
738+
);
739+
}
740+
679741
#[test]
680742
fn add_missing_match_arms_tuple_of_enum() {
681743
check_assist(

0 commit comments

Comments
 (0)