Skip to content

Commit 1e303cc

Browse files
committed
cursor_inside_simple_match_arm_list -- tests
1 parent 68a5015 commit 1e303cc

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

crates/ide_assists/src/handlers/add_missing_match_arms.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir::{Adt, HasSource, ModuleDef, Semantics};
55
use ide_db::helpers::{mod_path_to_ast, FamousDefs};
66
use ide_db::RootDatabase;
77
use itertools::Itertools;
8-
use syntax::ast::{self, make, AstNode, HasName, MatchArm, Pat};
8+
use syntax::ast::{self, AstNode, HasName, MatchArm, MatchArmList, MatchExpr, Pat, make};
99
use syntax::TextRange;
1010

1111
use crate::{
@@ -39,15 +39,21 @@ use crate::{
3939
pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4040
let match_expr = ctx.find_node_at_offset_with_descend::<ast::MatchExpr>()?;
4141
let match_arm_list = match_expr.match_arm_list()?;
42+
let target_range : TextRange;
4243

43-
let available_range = TextRange::new(
44-
ctx.sema.original_range(match_expr.syntax()).range.start(),
45-
ctx.sema.original_range(match_arm_list.syntax()).range.start(),
46-
);
44+
if !cursor_inside_simple_match_arm_list(&ctx, &match_expr, &match_arm_list) {
45+
target_range = TextRange::new(
46+
ctx.sema.original_range(match_expr.syntax()).range.start(),
47+
ctx.sema.original_range(match_arm_list.syntax()).range.start(),
48+
);
4749

48-
let cursor_in_range = available_range.contains_range(ctx.selection_trimmed());
49-
if !cursor_in_range {
50-
return None;
50+
let cursor_in_range = target_range.contains_range(ctx.selection_trimmed());
51+
if !cursor_in_range {
52+
return None;
53+
}
54+
}
55+
else {
56+
target_range = ctx.sema.original_range(match_expr.syntax()).range;
5157
}
5258

5359
let expr = match_expr.expr()?;
@@ -134,7 +140,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
134140
acc.add(
135141
AssistId("add_missing_match_arms", AssistKind::QuickFix),
136142
"Fill match arms",
137-
available_range,
143+
target_range,
138144
|builder| {
139145
let new_match_arm_list = match_arm_list.clone_for_update();
140146
let missing_arms = missing_pats
@@ -186,6 +192,14 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
186192
)
187193
}
188194

195+
fn cursor_inside_simple_match_arm_list(ctx: &AssistContext, match_expr : &MatchExpr, match_arm_list : &MatchArmList) -> bool {
196+
// println!("---\n{:#?}\n{:#?}\n---", match_expr, match_arm_list);
197+
if match_arm_list.arms().next() == None {
198+
return true;
199+
}
200+
false
201+
}
202+
189203
fn is_variant_missing(existing_pats: &[Pat], var: &Pat) -> bool {
190204
!existing_pats.iter().any(|pat| does_pat_match_variant(pat, var))
191205
}
@@ -320,10 +334,7 @@ fn main() {
320334
check_assist_not_applicable(
321335
add_missing_match_arms,
322336
r#"
323-
enum A {
324-
X,
325-
Y
326-
}
337+
enum A { X, Y }
327338
328339
fn foo(a: A) {
329340
$0 match a {
@@ -339,10 +350,7 @@ fn foo(a: A) {
339350
check_assist_not_applicable(
340351
add_missing_match_arms,
341352
r#"
342-
enum A {
343-
X,
344-
Y
345-
}
353+
enum A { X, Y }
346354
347355
fn foo(a: A) {
348356
match a {$0
@@ -637,7 +645,7 @@ enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
637645
638646
fn main() {
639647
let a = A::As;
640-
match a$0 {}
648+
match a {$0}
641649
}
642650
"#,
643651
r#"
@@ -900,7 +908,7 @@ fn foo(a: &mut A) {
900908
}
901909

902910
#[test]
903-
fn add_missing_match_arms_target() {
911+
fn add_missing_match_arms_target_simple() {
904912
check_assist_target(
905913
add_missing_match_arms,
906914
r#"
@@ -909,6 +917,23 @@ enum E { X, Y }
909917
fn main() {
910918
match E::X$0 {}
911919
}
920+
"#,
921+
"match E::X {}",
922+
);
923+
}
924+
925+
#[test]
926+
fn add_missing_match_arms_target_complex() {
927+
check_assist_target(
928+
add_missing_match_arms,
929+
r#"
930+
enum E { X, Y }
931+
932+
fn main() {
933+
match E::X$0 {
934+
E::X => {}
935+
}
936+
}
912937
"#,
913938
"match E::X ",
914939
);
@@ -922,16 +947,16 @@ fn main() {
922947
enum E { X, Y }
923948
924949
fn main() {
925-
match E::X $0 {
926-
_ => {}
950+
match E::X {
951+
$0_ => {}
927952
}
928953
}
929954
"#,
930955
r#"
931956
enum E { X, Y }
932957
933958
fn main() {
934-
match E::X {
959+
match E::X {
935960
$0E::X => todo!(),
936961
E::Y => todo!(),
937962
}

0 commit comments

Comments
 (0)