Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs')
| -rw-r--r-- | crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs index 65e2a01847..81a639e0b9 100644 --- a/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs @@ -412,6 +412,13 @@ fn reference_to_node( ) -> Option<(ast::PathSegment, SyntaxNode, hir::Module)> { let segment = reference.name.as_name_ref()?.syntax().parent().and_then(ast::PathSegment::cast)?; + + // filter out the reference in marco + let segment_range = segment.syntax().text_range(); + if segment_range != reference.range { + return None; + } + let parent = segment.parent_path().syntax().parent()?; let expr_or_pat = match_ast! { match parent { @@ -433,6 +440,45 @@ mod tests { use super::*; #[test] + fn test_with_marco() { + check_assist( + extract_struct_from_enum_variant, + r#" +macro_rules! foo { + ($x:expr) => { + $x + }; +} + +enum TheEnum { + TheVariant$0 { the_field: u8 }, +} + +fn main() { + foo![TheEnum::TheVariant { the_field: 42 }]; +} +"#, + r#" +macro_rules! foo { + ($x:expr) => { + $x + }; +} + +struct TheVariant{ the_field: u8 } + +enum TheEnum { + TheVariant(TheVariant), +} + +fn main() { + foo![TheEnum::TheVariant { the_field: 42 }]; +} +"#, + ); + } + + #[test] fn issue_16197() { check_assist( extract_struct_from_enum_variant, |