Skip to content

Commit 4ce1c6c

Browse files
committed
Auto merge of #17138 - Kohei316:generate-function-assist-for-new, r=Veykril
feature: Make generate function assist generate a function as a constructor if the generated function has the name "new" and is an asscociated function. close #17050 This PR makes `generate function assist` generate a function as a constructor if the generated function has the name "new" and is an asscociated function. If the asscociate type is a record struct, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { Self { field_1: todo!(), field_2: todo!() } } } ``` If the asscociate type is a tuple struct, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { Self(todo!(), todo!()) } } ``` If the asscociate type is a unit struct, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { Self } } ``` If the asscociate type is another adt, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { todo!() } } ```
2 parents 27fa563 + e2b1c55 commit 4ce1c6c

File tree

2 files changed

+270
-26
lines changed

2 files changed

+270
-26
lines changed

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,14 @@ impl Adt {
14891489
.map(|arena| arena.1.clone())
14901490
}
14911491

1492+
pub fn as_struct(&self) -> Option<Struct> {
1493+
if let Self::Struct(v) = self {
1494+
Some(*v)
1495+
} else {
1496+
None
1497+
}
1498+
}
1499+
14921500
pub fn as_enum(&self) -> Option<Enum> {
14931501
if let Self::Enum(v) = self {
14941502
Some(*v)

0 commit comments

Comments
 (0)