-
Notifications
You must be signed in to change notification settings - Fork 18.1k
x/tools/gopls: add refactor.inline.variable-all code action #70085
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
Comments
Change https://go.dev/cl/623637 mentions this issue: |
Hmm, how could I know two expression is identical regardless their tok.Pos value? Am I supposed to write a util function similar to types.Identical()? |
Change https://go.dev/cl/624035 mentions this issue: |
@adonovan Hey Alan, may you take a look? The logic is relatively simple :) |
Sorry for the delay and thanks for the contribution, but I am overwhelmed with reviews at the moment and am also out of office at a Go conference. I will attend to this next week. |
Thanks! No rush—enjoy the conference! |
…actor.extract.variable.all) This change introduces a new refactoring code action "Extract n occurrences of expression". The original "Extract Variable" is treated as a limited version of "Extract n occurrences of expression", they share the same underlying implementation. The difference is that "extract_all" utilizes a slice of structural equal expression by searching through the entire function body, while the limited version simply append the expression under selection to that slice. Also: - change default variable name from "x" to "newVar", default const name from "k" to "newConst", users will almost always perform a rename after extraction, so this change make it more discoverable, especially when there are multiple matches. - add marker test extract_expressions.txt and extract_expressions_resolve.txt. Updates golang/go#70085 Fixes golang/go#70563 Change-Id: I767b82be8a60d39c7aff087197c65d435b138826 GitHub-Last-Rev: b66a18d GitHub-Pull-Request: #539 Reviewed-on: https://go-review.googlesource.com/c/tools/+/624035 Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
https://go.dev/cl/624035 added "extract n occurrences of expr to a variable"; "inline all" has not one but two dedicated issues (#68567 and #66370). Therefore this issue is complete. |
#66370 only mentions inline functions, this issue is about inlining expressions, are they essentially the same? |
Ah, good point. Let's reopen this issue and narrow its scope to inlining var/const expressions. |
Change https://go.dev/cl/673636 mentions this issue: |
This change adds a new refactor.inline.variable code action that replaces a reference to a local variable by that variable's initializer expression, if possible (not shadowed). The "inline all" case will be implemented in a follow-up. + test, doc, relnote Updates golang/go#70085 Change-Id: I83c2f0ff9715239f8436b91f0b86bfd6a42746e0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/673636 Auto-Submit: Alan Donovan <[email protected]> Commit-Queue: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
[Update: extract.variable and extract-variable.all are done. So is inline-variable. Retitled to narrower scope of inline-all. --adonovan]
Original title: codeaction: replace every use-case of a variable with its defined expression, extract every identical expression
within function to a new variable
gopls version
Build info
golang.org/x/tools/gopls (devel)
golang.org/x/tools/gopls@(devel)
github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/[email protected] h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/[email protected] h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/[email protected] h1:PfPrmVDHfPgLVpiYnf2R1uL8SCXBjkqT51+f/fQHR6Q=
golang.org/x/[email protected] h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/[email protected] => ../
golang.org/x/[email protected] h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
honnef.co/go/[email protected] h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
mvdan.cc/[email protected] h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
mvdan.cc/xurls/[email protected] h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.23.2
go env
What did you do?
iShot_2024-10-29_11.52.53.mp4
the first half part is
:Rafactor inline_var
, and second part is:Rafactor extract_var new_var
, it is from a nvim plugin refactoring.nvim using treesitter under the hood, I use this two quite frequently to eliminate unnecessary variable as well as introduce new variable for some repeated expression, the problem is it fails (or not accurate) in complex situations, so I wish gopls would support this directly.What did you see happen?
Currently gopls supports extract a single expression to a new variable, I'd say extract every repeated expression under selection would be more useful, since the latter contains the former.
What did you expect to see?
see above
Editor and settings
No response
Logs
No response
The text was updated successfully, but these errors were encountered: