Skip to content

Commit 7716772

Browse files
committed
feat: extract_struct_from_function_signature
code action only avaliable in signature part of a function for some reason still does not change anything in the file
1 parent 8b7c029 commit 7716772

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ pub(crate) fn extract_struct_from_function_signature(
3838
// how to get function name and param list/part of param list the is selected seperatly
3939
// or maybe just auto generate random name not based on function name?
4040
let fn_ast = ctx.find_node_at_offset::<ast::Fn>()?;
41+
// we independently get the param list without going through fn (fn_ast.param_list()), because for some reason when we
42+
// go through the fn, the text_range is the whole function.
43+
let params_list = ctx.find_node_at_offset::<ast::ParamList>()?;
4144
let fn_name = fn_ast.name()?;
4245

4346
let fn_hir = ctx.sema.to_def(&fn_ast)?;
@@ -47,11 +50,10 @@ pub(crate) fn extract_struct_from_function_signature(
4750
}
4851

4952
// TODO: does this capture parenthesis
50-
let target = fn_ast.param_list()?.syntax().text_range();
53+
let target = params_list.syntax().text_range();
5154
// TODO: special handiling for self?
5255
// TODO: special handling for destrutered types (or maybe just don't suppurt code action on
5356
// destructed types yet
54-
let params = fn_ast.param_list()?;
5557

5658
let field_list = make::record_field_list(
5759
fn_ast
@@ -75,7 +77,6 @@ pub(crate) fn extract_struct_from_function_signature(
7577
|builder| {
7678
// TODO: update calls to the function
7779
let fn_ast = builder.make_mut(fn_ast);
78-
let params = builder.make_mut(params);
7980
tracing::info!("extract_struct_from_function_signature: starting edit");
8081
builder.edit_file(ctx.vfs_file_id());
8182
tracing::info!("extract_struct_from_function_signature: editing main file");
@@ -93,7 +94,7 @@ pub(crate) fn extract_struct_from_function_signature(
9394
// So I do the resolving while its still param list
9495
// and then apply it into record list after
9596
let field_list = if let Some((target_scope, source_scope)) =
96-
ctx.sema.scope(fn_ast.syntax()).zip(ctx.sema.scope(params.syntax()))
97+
ctx.sema.scope(fn_ast.syntax()).zip(ctx.sema.scope(params_list.syntax()))
9798
{
9899
let field_list = field_list.reset_indent();
99100
let field_list =
@@ -109,7 +110,7 @@ pub(crate) fn extract_struct_from_function_signature(
109110
field_list.clone_for_update()
110111
};
111112
tracing::info!("extract_struct_from_function_signature: collecting fields");
112-
let def = create_struct_def(name.clone(), &fn_ast, &params, &field_list, generics);
113+
let def = create_struct_def(name.clone(), &fn_ast, &params_list, &field_list, generics);
113114
tracing::info!("extract_struct_from_function_signature: creating struct");
114115

115116
let indent = fn_ast.indent_level();
@@ -123,7 +124,8 @@ pub(crate) fn extract_struct_from_function_signature(
123124
],
124125
);
125126
tracing::info!("extract_struct_from_function_signature: inserting struct {def}");
126-
update_function(name, &fn_ast, generic_params.map(|g| g.clone_for_update()));
127+
update_function(name, &fn_ast, generic_params.map(|g| g.clone_for_update())).unwrap();
128+
tracing::info!("extract_struct_from_function_signature: updating function signature and parameter uses");
127129
},
128130
)
129131
}
@@ -144,6 +146,8 @@ fn update_function(
144146

145147
let param = make::param(
146148
// TODO: do we want to destructure the struct
149+
// would make it easier in that we would not have to update all the uses of the variables in
150+
// the function
147151
ast::Pat::IdentPat(make::ident_pat(
148152
false,
149153
fn_ast.param_list()?.params().any(|p| {
@@ -154,9 +158,9 @@ fn update_function(
154158
)),
155159
ty,
156160
);
157-
// TODO: will eventually need to handle self to
158-
let param_list = make::param_list(None, std::iter::once(param)).clone_for_update();
159-
ted::replace(fn_ast.param_list()?.syntax(), param_list.syntax());
161+
// TODO: will eventually need to handle self too
162+
let params_list = make::param_list(None, std::iter::once(param)).clone_for_update();
163+
ted::replace(fn_ast.param_list()?.syntax(), params_list.syntax());
160164
// TODO: update uses of parameters in function, if we do not destructure
161165
Some(())
162166
}

0 commit comments

Comments
 (0)