diff options
| author | Anton Firszov <antonfir@gmail.com> | 2021-10-14 18:15:00 +0200 |
|---|---|---|
| committer | Anton Firszov <antonfir@gmail.com> | 2021-10-14 18:15:00 +0200 |
| commit | a17a132617dd484b13196a1e70ef87a0b762d3ed (patch) | |
| tree | 975936d14fa9dbbaa368dab1248d061601d2528b | |
| parent | a871da36937f427624883860eafb580d6349f8da (diff) | |
| download | rust-a17a132617dd484b13196a1e70ef87a0b762d3ed.tar.gz rust-a17a132617dd484b13196a1e70ef87a0b762d3ed.zip | |
Narrow add_missing_match_arms assist range
| -rw-r--r-- | crates/ide_assists/src/handlers/add_missing_match_arms.rs | 52 |
1 files changed, 50 insertions, 2 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 c856ab45d7b..1ad5daeff07 100644 --- a/crates/ide_assists/src/handlers/add_missing_match_arms.rs +++ b/crates/ide_assists/src/handlers/add_missing_match_arms.rs @@ -5,6 +5,7 @@ use hir::{Adt, HasSource, ModuleDef, Semantics}; use ide_db::helpers::{mod_path_to_ast, FamousDefs}; use ide_db::RootDatabase; use itertools::Itertools; +use syntax::TextRange; use syntax::ast::{self, make, AstNode, HasName, MatchArm, Pat}; use crate::{ @@ -40,6 +41,16 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> let match_expr = ctx.find_node_at_offset_with_descend::<ast::MatchExpr>()?; let match_arm_list = match_expr.match_arm_list()?; + let available_range = TextRange::new( + match_expr.syntax().text_range().start(), + match_arm_list.syntax().text_range().start(), + ); + + let cursor_in_range = available_range.contains_range(ctx.frange.range); + if !cursor_in_range { + return None; + } + let expr = match_expr.expr()?; let mut arms: Vec<MatchArm> = match_arm_list.arms().collect(); @@ -121,11 +132,10 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> return None; } - let target = ctx.sema.original_range(match_expr.syntax()).range; acc.add( AssistId("add_missing_match_arms", AssistKind::QuickFix), "Fill match arms", - target, + available_range, |builder| { let new_match_arm_list = match_arm_list.clone_for_update(); let missing_arms = missing_pats @@ -307,6 +317,44 @@ fn main() { } #[test] + fn not_applicable_outside_of_range_left() { + check_assist_not_applicable( + add_missing_match_arms, + r#" +enum A { + X, + Y +} + +fn foo(a: A) { + $0match a { + A::X => { } + } +} + "#, + ); + } + + #[test] + fn not_applicable_outside_of_range_right() { + check_assist_not_applicable( + add_missing_match_arms, + r#" +enum A { + X, + Y +} + +fn foo(a: A) { + match a {$0 + A::X => { } + } +} + "#, + ); + } + + #[test] fn all_boolean_match_arms_provided() { check_assist_not_applicable( add_missing_match_arms, |
