Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21044 from A4-Tacks/fix-make-unnamed-param
Fix make::unnamed_param result a untyped_param
| -rw-r--r-- | crates/ide-assists/src/handlers/generate_fn_type_alias.rs | 32 | ||||
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 97 |
2 files changed, 128 insertions, 1 deletions
diff --git a/crates/ide-assists/src/handlers/generate_fn_type_alias.rs b/crates/ide-assists/src/handlers/generate_fn_type_alias.rs index 0b7eca2290..7fd94b4bed 100644 --- a/crates/ide-assists/src/handlers/generate_fn_type_alias.rs +++ b/crates/ide-assists/src/handlers/generate_fn_type_alias.rs @@ -270,6 +270,22 @@ fn foo<A: Trait, B: Trait>(a: A, b: B) -> i32 { return 42; } } #[test] + fn generate_fn_alias_unnamed_complex_types() { + check_assist_by_label( + generate_fn_type_alias, + r#" +fn fo$0o(x: Vec<i32>) {} +"#, + r#" +type ${0:FooFn} = fn(Vec<i32>); + +fn foo(x: Vec<i32>) {} +"#, + ParamStyle::Unnamed.label(), + ); + } + + #[test] fn generate_fn_alias_unnamed_self() { check_assist_by_label( generate_fn_type_alias, @@ -406,6 +422,22 @@ fn foo<A: Trait, B: Trait>(a: A, b: B) -> i32 { return 42; } } #[test] + fn generate_fn_alias_named_complex_types() { + check_assist_by_label( + generate_fn_type_alias, + r#" +fn fo$0o(x: Vec<i32>) {} +"#, + r#" +type ${0:FooFn} = fn(x: Vec<i32>); + +fn foo(x: Vec<i32>) {} +"#, + ParamStyle::Named.label(), + ); + } + + #[test] fn generate_fn_alias_named_self() { check_assist_by_label( generate_fn_type_alias, diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 19019ad08d..b2904ce3c0 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -1016,7 +1016,19 @@ pub fn item_static( } pub fn unnamed_param(ty: ast::Type) -> ast::Param { - ast_from_text(&format!("fn f({ty}) {{ }}")) + quote! { + Param { + #ty + } + } +} + +pub fn untyped_param(pat: ast::Pat) -> ast::Param { + quote! { + Param { + #pat + } + } } pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param { @@ -1456,3 +1468,86 @@ pub mod tokens { } } } + +#[cfg(test)] +mod tests { + use expect_test::expect; + + use super::*; + + #[track_caller] + fn check(node: impl AstNode, expect: expect_test::Expect) { + let node_debug = format!("{:#?}", node.syntax()); + expect.assert_eq(&node_debug); + } + + #[test] + fn test_unnamed_param() { + check( + unnamed_param(ty("Vec")), + expect![[r#" + [email protected] "Vec" + "#]], + ); + + check( + unnamed_param(ty("Vec<T>")), + expect![[r#" + [email protected] "Vec" + [email protected] "<" + [email protected] "T" + [email protected] ">" + "#]], + ); + } + + #[test] + fn test_untyped_param() { + check( + untyped_param(path_pat(ext::ident_path("name"))), + expect![[r#" + [email protected] "name" + "#]], + ); + + check( + untyped_param( + range_pat( + Some(path_pat(ext::ident_path("start"))), + Some(path_pat(ext::ident_path("end"))), + ) + .into(), + ), + expect![[r#" + [email protected] "start" + [email protected] ".." + [email protected] "end" + "#]], + ); + } +} |