Skip to content

Commit

Permalink
Merge #7468
Browse files Browse the repository at this point in the history
7468: Enable fill_match_arms in macros r=Veykril a=Veykril

Fixes #3936

The indentation is a bit off, but I don't think it's worth trying to fix that up until we have a proper formatting thing set up, as this most likely requires some hand picked specializing making the implementation worse to read(Assuming this can even be fixed for all cases by hardcoding indentation fixes).

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
  • Loading branch information
bors[bot] and Veykril authored Jan 27, 2021
2 parents 671757a + b883a52 commit ad3cb21
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions crates/assists/src/handlers/fill_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
// }
// ```
pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let match_expr = ctx.find_node_at_offset::<ast::MatchExpr>()?;
let match_expr = ctx.find_node_at_offset_with_descend::<ast::MatchExpr>()?;
let match_arm_list = match_expr.match_arm_list()?;

let expr = match_expr.expr()?;
Expand Down Expand Up @@ -103,7 +103,7 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
return None;
}

let target = match_expr.syntax().text_range();
let target = ctx.sema.original_range(match_expr.syntax()).range;
acc.add(
AssistId("fill_match_arms", AssistKind::QuickFix),
"Fill match arms",
Expand All @@ -113,7 +113,7 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
let n_old_arms = new_arm_list.arms().count();
let new_arm_list = new_arm_list.append_arms(missing_arms);
let first_new_arm = new_arm_list.arms().nth(n_old_arms);
let old_range = match_arm_list.syntax().text_range();
let old_range = ctx.sema.original_range(match_arm_list.syntax()).range;
match (first_new_arm, ctx.config.snippet_cap) {
(Some(first_new_arm), Some(cap)) => {
let extend_lifetime;
Expand Down Expand Up @@ -752,4 +752,36 @@ fn foo(opt: Option<i32>) {
"#,
);
}

#[test]
fn works_inside_macro_call() {
check_assist(
fill_match_arms,
r#"
macro_rules! m { ($expr:expr) => {$expr}}
enum Test {
A,
B,
C,
}
fn foo(t: Test) {
m!(match t$0 {});
}"#,
r#"macro_rules! m { ($expr:expr) => {$expr}}
enum Test {
A,
B,
C,
}
fn foo(t: Test) {
m!(match t {
$0Test::A => {}
Test::B => {}
Test::C => {}
});
}"#,
);
}
}

0 comments on commit ad3cb21

Please sign in to comment.