Skip to content

Commit 644fd3d

Browse files
committed
feat: extract_struct_from_function_signature
only capture comments and attributes around the parameters added some info logging trying to debug salsa query error
1 parent cc515c6 commit 644fd3d

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub(crate) fn extract_struct_from_function_signature(
5151
// TODO: special handiling for self?
5252
// TODO: special handling for destrutered types (or maybe just don't suppurt code action on
5353
// destructed types yet
54+
let params = fn_ast.param_list()?;
55+
5456
let field_list = make::record_field_list(
5557
fn_ast
5658
.param_list()?
@@ -70,14 +72,21 @@ pub(crate) fn extract_struct_from_function_signature(
7072
"Extract struct from signature of a function",
7173
target,
7274
|builder| {
75+
let fn_ast = builder.make_mut(fn_ast);
76+
tracing::info!("extract_struct_from_function_signature: starting edit");
7377
builder.edit_file(ctx.vfs_file_id());
78+
tracing::info!("extract_struct_from_function_signature: editing main file");
7479

7580
let generic_params = fn_ast
7681
.generic_param_list()
77-
.and_then(|known_generics| extract_generic_params(&known_generics, &field_list));
82+
.and_then(|known_generics| extract_generic_params(&known_generics, &field_list));
83+
tracing::info!("extract_struct_from_function_signature: collecting generics");
7884
let generics = generic_params.as_ref().map(|generics| generics.clone_for_update());
7985

8086
// resolve GenericArg in field_list to actual type
87+
// we are getting a query error from salsa, I think it is because the field list is
88+
// constructed in new generation, so maybe do the resolving while its still param list
89+
// and then convert it into record list after
8190
let field_list = if let Some((target_scope, source_scope)) =
8291
ctx.sema.scope(fn_ast.syntax()).zip(ctx.sema.scope(field_list.syntax()))
8392
{
@@ -94,8 +103,9 @@ pub(crate) fn extract_struct_from_function_signature(
94103
} else {
95104
field_list.clone_for_update()
96105
};
97-
98-
let def = create_struct_def(name.clone(), &fn_ast, &field_list, generics);
106+
tracing::info!("extract_struct_from_function_signature: collecting fields");
107+
let def = create_struct_def(name.clone(), &fn_ast, &params, &field_list, generics);
108+
tracing::info!("extract_struct_from_function_signature: creating struct");
99109

100110
let indent = fn_ast.indent_level();
101111
let def = def.indent(indent);
@@ -107,6 +117,7 @@ pub(crate) fn extract_struct_from_function_signature(
107117
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
108118
],
109119
);
120+
tracing::info!("extract_struct_from_function_signature: inserting struct {def}");
110121
},
111122
)
112123
}
@@ -120,6 +131,7 @@ fn pat_to_name(pat: ast::Pat) -> Option<ast::Name> {
120131
fn create_struct_def(
121132
name: ast::Name,
122133
fn_ast: &ast::Fn,
134+
param_ast: &ast::ParamList,
123135
field_list: &ast::RecordFieldList,
124136
generics: Option<ast::GenericParamList>,
125137
) -> ast::Struct {
@@ -146,17 +158,19 @@ fn create_struct_def(
146158

147159
let strukt = make::struct_(fn_vis, name, generics, field_list).clone_for_update();
148160

149-
// take comments from variant
161+
// take comments from only inside signature
150162
ted::insert_all(
151163
ted::Position::first_child_of(strukt.syntax()),
152-
take_all_comments(fn_ast.syntax()),
164+
take_all_comments(param_ast.syntax()),
153165
);
154166

155-
// copy attributes from enum
167+
// TODO: this may not be correct as we shouldn't put all the attributes at the top
168+
// copy attributes from each parameter
156169
ted::insert_all(
157170
ted::Position::first_child_of(strukt.syntax()),
158-
fn_ast
159-
.attrs()
171+
param_ast
172+
.params()
173+
.flat_map(|p| p.attrs())
160174
.flat_map(|it| {
161175
vec![it.syntax().clone_for_update().into(), make::tokens::single_newline().into()]
162176
})

0 commit comments

Comments
 (0)