@@ -38,6 +38,9 @@ pub(crate) fn extract_struct_from_function_signature(
38
38
// how to get function name and param list/part of param list the is selected seperatly
39
39
// or maybe just auto generate random name not based on function name?
40
40
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 > ( ) ?;
41
44
let fn_name = fn_ast. name ( ) ?;
42
45
43
46
let fn_hir = ctx. sema . to_def ( & fn_ast) ?;
@@ -47,11 +50,10 @@ pub(crate) fn extract_struct_from_function_signature(
47
50
}
48
51
49
52
// TODO: does this capture parenthesis
50
- let target = fn_ast . param_list ( ) ? . syntax ( ) . text_range ( ) ;
53
+ let target = params_list . syntax ( ) . text_range ( ) ;
51
54
// TODO: special handiling for self?
52
55
// TODO: special handling for destrutered types (or maybe just don't suppurt code action on
53
56
// destructed types yet
54
- let params = fn_ast. param_list ( ) ?;
55
57
56
58
let field_list = make:: record_field_list (
57
59
fn_ast
@@ -75,7 +77,6 @@ pub(crate) fn extract_struct_from_function_signature(
75
77
|builder| {
76
78
// TODO: update calls to the function
77
79
let fn_ast = builder. make_mut ( fn_ast) ;
78
- let params = builder. make_mut ( params) ;
79
80
tracing:: info!( "extract_struct_from_function_signature: starting edit" ) ;
80
81
builder. edit_file ( ctx. vfs_file_id ( ) ) ;
81
82
tracing:: info!( "extract_struct_from_function_signature: editing main file" ) ;
@@ -93,7 +94,7 @@ pub(crate) fn extract_struct_from_function_signature(
93
94
// So I do the resolving while its still param list
94
95
// and then apply it into record list after
95
96
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 ( ) ) )
97
98
{
98
99
let field_list = field_list. reset_indent ( ) ;
99
100
let field_list =
@@ -109,7 +110,7 @@ pub(crate) fn extract_struct_from_function_signature(
109
110
field_list. clone_for_update ( )
110
111
} ;
111
112
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) ;
113
114
tracing:: info!( "extract_struct_from_function_signature: creating struct" ) ;
114
115
115
116
let indent = fn_ast. indent_level ( ) ;
@@ -123,7 +124,8 @@ pub(crate) fn extract_struct_from_function_signature(
123
124
] ,
124
125
) ;
125
126
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" ) ;
127
129
} ,
128
130
)
129
131
}
@@ -144,6 +146,8 @@ fn update_function(
144
146
145
147
let param = make:: param (
146
148
// 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
147
151
ast:: Pat :: IdentPat ( make:: ident_pat (
148
152
false ,
149
153
fn_ast. param_list ( ) ?. params ( ) . any ( |p| {
@@ -154,9 +158,9 @@ fn update_function(
154
158
) ) ,
155
159
ty,
156
160
) ;
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 ( ) ) ;
160
164
// TODO: update uses of parameters in function, if we do not destructure
161
165
Some ( ( ) )
162
166
}
0 commit comments