rust-lang / rust-lang/rust-analyzer
Assist: “Extract into variable” on multiple fields of a struct
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
Starting from the code
struct Foo {
x: &'static str,
y: &'static str,
}
fn foo() -> Foo {
Foo {
x: "abcdef",
y: "ghi",
}
}
it is currently possible to transform foo() into
fn foo() -> Foo {
let x = "abcdef";
let y = "ghi";
Foo { x, y }
}
by putting the cursor on each of the two field values and invoking the “Extract into variable” assist twice. I think it would be useful to offer this same transformation in a way which
- does not require the cursor to be placed exactly on the value expression (I frequently try to invoke it starting from the field name, before remembering that that doesn’t work), and
- is able to operate on multiple struct fields at once.
In particular, I propose that it should be possible to extract multiple field value expressions into correspondingly named variables when the cursor is
- in the struct name, in which case it should act on all fields,
- in a single field name, or
- a selection of multiple fields within the struct literal, in which case it should act on all fields whose names or value expressions are within the selection.
In these cases, the rename prompt should possibly be skipped, since it is likely that the user wants the variable(s) to be named the same as the field(s).
@rustbot label +A-assists
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the existing “Extract into variable” assist and reproducing the struct-literal example from the issue. Trace how it handles a field value, then define behavior for the struct name, one field name, and selections covering multiple fields; done means the corresponding field values can be extracted together with the proposed naming behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100