mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-30 16:11:41 +00:00
Fix make::unnamed_param result a untyped_param
- Add `make::untyped_param`
- Add some basic make tests
Example
---
```rust
make::unnamed_param(make::ty("Vec<T>")),
```
**Before this PR**
```text
PARAM@0..4
IDENT_PAT@0..4
NAME@0..4
IDENT@0..4 "Vec"
```
**After this PR**
```text
PARAM@0..6
PATH_TYPE@0..6
PATH@0..6
PATH_SEGMENT@0..6
NAME_REF@0..3
IDENT@0..3 "Vec"
GENERIC_ARG_LIST@3..6
L_ANGLE@3..4 "<"
TYPE_ARG@4..5
PATH_TYPE@4..5
PATH@4..5
PATH_SEGMENT@4..5
NAME_REF@4..5
IDENT@4..5 "T"
R_ANGLE@5..6 ">"
```
---
Assist: `Generate a type alias for function with unnamed params`
```rust
fn foo$0(x: Vec<i32>) {}
```
**Before this PR**
```rust
type FooFn = fn(Vec);
fn foo(x: Vec<i32>) {}
```
**After this PR**
```rust
type FooFn = fn(Vec<i32>);
fn foo(x: Vec<i32>) {}
```
This commit is contained in:
parent
4bf516ee5a
commit
4fb242e4bf
@ -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#"
|
||||
PARAM@0..3
|
||||
PATH_TYPE@0..3
|
||||
PATH@0..3
|
||||
PATH_SEGMENT@0..3
|
||||
NAME_REF@0..3
|
||||
IDENT@0..3 "Vec"
|
||||
"#]],
|
||||
);
|
||||
|
||||
check(
|
||||
unnamed_param(ty("Vec<T>")),
|
||||
expect![[r#"
|
||||
PARAM@0..6
|
||||
PATH_TYPE@0..6
|
||||
PATH@0..6
|
||||
PATH_SEGMENT@0..6
|
||||
NAME_REF@0..3
|
||||
IDENT@0..3 "Vec"
|
||||
GENERIC_ARG_LIST@3..6
|
||||
L_ANGLE@3..4 "<"
|
||||
TYPE_ARG@4..5
|
||||
PATH_TYPE@4..5
|
||||
PATH@4..5
|
||||
PATH_SEGMENT@4..5
|
||||
NAME_REF@4..5
|
||||
IDENT@4..5 "T"
|
||||
R_ANGLE@5..6 ">"
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_untyped_param() {
|
||||
check(
|
||||
untyped_param(path_pat(ext::ident_path("name"))),
|
||||
expect![[r#"
|
||||
PARAM@0..4
|
||||
IDENT_PAT@0..4
|
||||
NAME@0..4
|
||||
IDENT@0..4 "name"
|
||||
"#]],
|
||||
);
|
||||
|
||||
check(
|
||||
untyped_param(
|
||||
range_pat(
|
||||
Some(path_pat(ext::ident_path("start"))),
|
||||
Some(path_pat(ext::ident_path("end"))),
|
||||
)
|
||||
.into(),
|
||||
),
|
||||
expect![[r#"
|
||||
PARAM@0..10
|
||||
RANGE_PAT@0..10
|
||||
IDENT_PAT@0..5
|
||||
NAME@0..5
|
||||
IDENT@0..5 "start"
|
||||
DOT2@5..7 ".."
|
||||
IDENT_PAT@7..10
|
||||
NAME@7..10
|
||||
IDENT@7..10 "end"
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user