From b883a52712fe9eccf8e5120b7a965ac3c9699d6f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 27 Jan 2021 22:32:40 +0100 Subject: Enable fill_match_arms in macros --- crates/assists/src/handlers/fill_match_arms.rs | 38 ++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/assists/src/handlers/fill_match_arms.rs b/crates/assists/src/handlers/fill_match_arms.rs index 4964ddc7d..7086e47d2 100644 --- a/crates/assists/src/handlers/fill_match_arms.rs +++ b/crates/assists/src/handlers/fill_match_arms.rs @@ -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::()?; + let match_expr = ctx.find_node_at_offset_with_descend::()?; let match_arm_list = match_expr.match_arm_list()?; let expr = match_expr.expr()?; @@ -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", @@ -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; @@ -752,4 +752,36 @@ fn foo(opt: Option) { "#, ); } + + #[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 => {} +}); +}"#, + ); + } } -- cgit v1.2.3