Skip to content
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

Offer "Extract variable" assist for statements #15796

Closed
Young-Flash opened this issue Oct 24, 2023 · 8 comments · Fixed by #15809
Closed

Offer "Extract variable" assist for statements #15796

Young-Flash opened this issue Oct 24, 2023 · 8 comments · Fixed by #15809
Labels
C-feature Category: feature request

Comments

@Young-Flash
Copy link
Member

Young-Flash commented Oct 24, 2023

There is a useful feature in Jetbrain IDEA, which is automatically bind a variable for an expression by a keyboard shotcut (ctrl + alt + v), showing below:

auto_binding

I think it would be wonderful if rust analyzer have a an feature (maybe impl via a assist💡), I'd like to make a PR if it's ok with rust analyzer team.

@Young-Flash Young-Flash added the C-feature Category: feature request label Oct 24, 2023
@lnicola
Copy link
Member

lnicola commented Oct 24, 2023

Did you see the "Extract into variable" assist?

image

image

@Young-Flash
Copy link
Member Author

seems not avaiable for the following case, am I miss something?

image

@lnicola
Copy link
Member

lnicola commented Oct 24, 2023

Try selecting it, like in my screenshot.

@Young-Flash
Copy link
Member Author

I got it.

It require manually select the code range, can't trigger when the cursor in the line end. May be we can refactor this?

And, can it bind with a keyboard shorcut? It's much convenient.

@lnicola
Copy link
Member

lnicola commented Oct 24, 2023

It might be a good idea to offer it when the statement is a single function/method call (and the return type is not ()?).

I don't think you can bind a specific assist to a shortcut, but Ctrl-. will open the assists menu, and a second Ctrl-. will invoke the first one, which in this case is "extract into variable", which seems easy enough.

@Young-Flash
Copy link
Member Author

It might be a good idea to offer it when the statement is a single function/method call (and the return type is not ()?).

yeah, I'd try

@lnicola lnicola changed the title feature request: add auto bingding for expression Offer "Extract variable" assist for statements Oct 24, 2023
@bors bors closed this as completed in f493207 Oct 28, 2023
@rsalmei
Copy link

rsalmei commented Nov 3, 2023

A curiosity @lnicola, please:
Is this "assist" popup always the "Refactor..." context menu? I did a few tests triggering them both in some places like the start and end of a line, as well as selecting it, and they seemed identical.

@lnicola
Copy link
Member

lnicola commented Nov 3, 2023

  • this assist is always "refactor" because it's marked as such
  • in other cases, like let x: Vec<_> = vec![10] shows up as Rewrite in the menu
  • Light bulb / Ctrl-. / right click -> Refactor are the same assists, triggered in different ways
  • the latest version of VS Code will also offers assists for the current line, not just the caret position, which makes feat: make extract_variable assist in place #15809 less important

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants