mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
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!() } } ```
This commit is contained in:
commit
84ef3cfa91
2 changed files with 270 additions and 26 deletions
|
@ -1489,6 +1489,14 @@ impl Adt {
|
|||
.map(|arena| arena.1.clone())
|
||||
}
|
||||
|
||||
pub fn as_struct(&self) -> Option<Struct> {
|
||||
if let Self::Struct(v) = self {
|
||||
Some(*v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_enum(&self) -> Option<Enum> {
|
||||
if let Self::Enum(v) = self {
|
||||
Some(*v)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue