-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add ide-assist: extract_struct_from_function_signature #20343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add ide-assist: extract_struct_from_function_signature #20343
Conversation
I have a few design decisions that I do not know what I should do about:
fn foo(mut i: i32, j: i32) {
i = j
} -> struct FooStruct{ i: i32, j: i32 }
fn foo(FooStruct { mut i, j }: FooStruct) {
i = j
} or 2) struct FooStruct{ i: i32, j: i32 }
fn foo(mut foo_struct FooStruct) {
foo_struct.i = foo_struct.j
}
fn foo((a, b): (i32, i32)) {} |
Also, I think CI is failing because the slow tests need to be updated. |
Lifetime support in r-a is very incomplete. Ideally I think we'd generate one lifetime for each invariant lifetime, and another one for all the covariant. We can't do it now so I think the best will be to always generate a single lifetime.
Assists should focus on one change. Therefore, I think we should destructure the parameter, and have another assist to convert to dot access. Although... Destructuring in parameters isn't that common in idiomatic Rust, so I'm not sure. @rust-lang/rust-analyzer thoughts?
I think the assist should only be enabled when there is a selection, not on empty selection, that's too much clutter. This way you also don't have to fix the slow tests.
Feel free. This can always be added later if desired.
Given what I said about selection, I think it should be treated like any other parameter, i.e. put it only if it's selected. |
What should I do about Also, when the struct has generics, the way I am doing it now only the function signature fully specifies all the generics while uses, rely on inference from the caller. fn foo<'a>($0bar: &'_ &'a i32$0, baz: i32) {
foo(bar, baz)
} -> struct FooStruct<'a, 'b>{ bar: &'b &'a i32 }
// signature (all generics specified)
fn foo<'a>(FooStruct { bar, .. }: FooStruct<'a, '_>, baz: i32) {
// use (bare struct name without any generics specified)
foo(FooStruct { bar: bar }, baz)
} |
I think I won't support doing it for Also, should I be copying relevant |
It's mostly working, there are a few things I could not figure out how to do: If there is a comment in between two parameters, I cannot move into the struct definition between the corresponding fields. Also for a case like this: struct Foo<'a> { baz: &'a i32 }
fn bar(foo: Foo) {} How do I detect that the And I assume I should remove the "feat: extract_struct_from_function_signature" header from all my commit messages? |
This comment has been minimized.
This comment has been minimized.
now actually uses the parameter name for field. and generates correct struct name.
only capture comments and attributes around the parameters added some info logging trying to debug salsa query error
fixed issue with resolving generics (I think) started writing the code to update the original function
code action only avaliable in signature part of a function for some reason still does not change anything in the file
made the edit actually have a effect by putting the edit_file first made the parameter in original function lower snake case made the checking if pre existing struct with new name use the new name as opposed to the original function name fixed autogenerated test
fixed typos
now destructures struct in function signature and only add paramereters that are selected to the struct added another test
working on making updating references. also fixed typos fixed and added more tests
starting to work on lifetime part
the signature of the function now uses the struct with all the generic args while uses don't specifiy any generics
do not support if there is impl trait also ideas for when handling self parameter cleaned up comments
made it work for normal parameters in methods
cleanup and follow stye guide a bit
fixed bug where if extracting from function/method in impl and was referenced somewhere else in impl it would break down b/c making the impl mut would happen after the reference was updated
mostly copies attributes and comments properly (edge case where comment in between to parameters is lost) reference types with no lifetimes now get an autogenerated lifetime (still need to figure out how to do it for types with only lifetimes that do not have explicit lifetimes)
fixed formatting
2a07efa
to
a1bb839
Compare
When you select the signature of a function, you should have the ability to extract all or part of the function's parameters to a struct.
->
Still very much a work in progress.
Closes: #20331