Skip to content

Commit

Permalink
fix: upmap ranges in convert_tuple_struct_to_named_struct assist
Browse files Browse the repository at this point in the history
  • Loading branch information
vishruth-thimmaiah committed Jan 20, 2025
1 parent 46e2d6e commit 0b2b04f
Showing 1 changed file with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ fn edit_struct_references(
match_ast! {
match node {
ast::TupleStructPat(tuple_struct_pat) => {
let Some(file_range) = ctx.sema.original_range_opt(&node) else { return None };
edit.edit_file(file_range.file_id);
edit.replace(
tuple_struct_pat.syntax().text_range(),
file_range.range,
ast::make::record_pat_with_fields(
tuple_struct_pat.path()?,
ast::make::record_pat_field_list(tuple_struct_pat.fields().zip(names).map(
Expand Down Expand Up @@ -921,6 +923,104 @@ pub struct $0Foo(#[my_custom_attr] u32);
"#,
r#"
pub struct Foo { #[my_custom_attr] field1: u32 }
"#,
);
}

#[test]
fn convert_in_macro_pattern_args() {
check_assist(
convert_tuple_struct_to_named_struct,
r#"
macro_rules! foo {
($expression:expr, $pattern:pat) => {
match $expression {
$pattern => true,
_ => false
}
};
}
enum Expr {
A$0(usize),
}
fn main() {
let e = Expr::A(0);
foo!(e, Expr::A(0));
}
"#,
r#"
macro_rules! foo {
($expression:expr, $pattern:pat) => {
match $expression {
$pattern => true,
_ => false
}
};
}
enum Expr {
A { field1: usize },
}
fn main() {
let e = Expr::A { field1: 0 };
foo!(e, Expr::A { field1: 0 });
}
"#,
);
}

#[test]
fn convert_in_multi_file_macro_pattern_args() {
check_assist(
convert_tuple_struct_to_named_struct,
r#"
//- /main.rs
mod foo;
enum Test {
A$0(i32)
}
//- /foo.rs
use crate::Test;
macro_rules! foo {
($expression:expr, $pattern:pat) => {
match $expression {
$pattern => true,
_ => false
}
};
}
fn foo() {
let a = Test::A(0);
foo!(a, Test::A(0));
}
"#,
r#"
//- /main.rs
mod foo;
enum Test {
A { field1: i32 }
}
//- /foo.rs
use crate::Test;
macro_rules! foo {
($expression:expr, $pattern:pat) => {
match $expression {
$pattern => true,
_ => false
}
};
}
fn foo() {
let a = Test::A { field1: 0 };
foo!(a, Test::A { field1: 0 });
}
"#,
);
}
Expand Down

0 comments on commit 0b2b04f

Please sign in to comment.