Skip to content

Commit cc515c6

Browse files
committed
feat: extract_struct_from_function_signature
now actually uses the parameter name for field. and generates correct struct name.
1 parent 03b7efa commit cc515c6

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

crates/ide-assists/src/handlers/extract_struct_from_function_signature.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
use hir::{Function, ModuleDef, Visibility};
1+
use hir::{Function, ModuleDef};
22
use ide_db::{RootDatabase, assists::AssistId, path_transform::PathTransform};
33
use itertools::Itertools;
4+
use stdx::to_camel_case;
45
use syntax::{
6+
AstNode, SyntaxElement, SyntaxKind, SyntaxNode, T,
57
ast::{
6-
self, edit::{AstNodeEdit, IndentLevel}, make, HasAttrs, HasGenericParams, HasName, HasVisibility
7-
}, match_ast, ted, AstNode, SyntaxElement, SyntaxKind, SyntaxNode, T
8+
self, HasAttrs, HasGenericParams, HasName, HasVisibility,
9+
edit::{AstNodeEdit, IndentLevel},
10+
make,
11+
},
12+
match_ast, ted,
813
};
914

1015
use crate::{AssistContext, Assists};
@@ -50,10 +55,16 @@ pub(crate) fn extract_struct_from_function_signature(
5055
fn_ast
5156
.param_list()?
5257
.params()
53-
.map(|param| Some(make::record_field(None, make::name("todo"), param.ty()?)))
54-
.collect::<Option<Vec<_>>>()?
55-
.into_iter(),
58+
.map(|param| {
59+
Some(make::record_field(
60+
fn_ast.visibility(),
61+
param.pat().and_then(pat_to_name)?,
62+
param.ty()?,
63+
))
64+
})
65+
.collect::<Option<Vec<_>>>()?,
5666
);
67+
let name = make::name(&format!("{}Struct", to_camel_case(fn_name.text_non_mutable())));
5768
acc.add(
5869
AssistId::refactor_rewrite("extract_struct_from_function_signature"),
5970
"Extract struct from signature of a function",
@@ -84,8 +95,7 @@ pub(crate) fn extract_struct_from_function_signature(
8495
field_list.clone_for_update()
8596
};
8697

87-
let def =
88-
create_struct_def(fn_name.clone(), &fn_ast, &field_list, generics);
98+
let def = create_struct_def(name.clone(), &fn_ast, &field_list, generics);
8999

90100
let indent = fn_ast.indent_level();
91101
let def = def.indent(indent);
@@ -100,13 +110,20 @@ pub(crate) fn extract_struct_from_function_signature(
100110
},
101111
)
102112
}
113+
114+
fn pat_to_name(pat: ast::Pat) -> Option<ast::Name> {
115+
match pat {
116+
ast::Pat::IdentPat(ident_pat) => ident_pat.name(),
117+
_ => None,
118+
}
119+
}
103120
fn create_struct_def(
104121
name: ast::Name,
105122
fn_ast: &ast::Fn,
106123
field_list: &ast::RecordFieldList,
107124
generics: Option<ast::GenericParamList>,
108125
) -> ast::Struct {
109-
let enum_vis = fn_ast.visibility();
126+
let fn_vis = fn_ast.visibility();
110127

111128
let insert_vis = |node: &'_ SyntaxNode, vis: &'_ SyntaxNode| {
112129
let vis = vis.clone_for_update();
@@ -115,7 +132,7 @@ fn create_struct_def(
115132

116133
// for fields without any existing visibility, use visibility of enum
117134
let field_list: ast::FieldList = {
118-
if let Some(vis) = &enum_vis {
135+
if let Some(vis) = &fn_vis {
119136
field_list
120137
.fields()
121138
.filter(|field| field.visibility().is_none())
@@ -127,7 +144,7 @@ fn create_struct_def(
127144
};
128145
let field_list = field_list.indent(IndentLevel::single());
129146

130-
let strukt = make::struct_(enum_vis, name, generics, field_list).clone_for_update();
147+
let strukt = make::struct_(fn_vis, name, generics, field_list).clone_for_update();
131148

132149
// take comments from variant
133150
ted::insert_all(

crates/ide-assists/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ mod handlers {
146146
mod extract_function;
147147
mod extract_module;
148148
mod extract_struct_from_enum_variant;
149+
mod extract_struct_from_function_signature;
149150
mod extract_type_alias;
150151
mod extract_variable;
151152
mod fix_visibility;
@@ -233,7 +234,6 @@ mod handlers {
233234
mod unwrap_type_to_generic_arg;
234235
mod wrap_return_type;
235236
mod wrap_unwrap_cfg_attr;
236-
mod extract_struct_from_function_signature;
237237

238238
pub(crate) fn all() -> &'static [Handler] {
239239
&[

0 commit comments

Comments
 (0)