You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
x/tools/gopls: add support for 'refactor.extract.constant' code actions #37170
The function refactoring can be implemented by making use of guru's freevars code.
Reacted by Juan Pablo Villaseca Carrasco, Jan Mollowitz, Amir Laher, Vittorio Camisa, Ernesto Alejo, Nils Werner, PL Pery 🐢, Raymond Arias, Jakub Warczarek, Nghia Le Minh and 12 more
Reacted by Amir Laher, Nils Werner, Jakub Warczarek, Alik Khilazhev and Sacha Froment
Extraction does not include comments. Comments are replaced with newlines in the extracted function.
You cannot extract a selection that begins with or ends with a comment.
Control flow statements (break, continue, goto, etc.) are not handled specially. This can produce bad code if extracted poorly.
Defer statements are not handled specially. This can also produce bad code if extracted poorly.
Identifiers can be on the left-hand side of an assignment statement even if they are never used again. This produces a yellow line with the message "variable is never used." This occurs when a variable name is used later in scope, but the first time it is used after the selection, it is defined again (using :=) rather than assigned. This does not produce incorrect code, but should be fixed.
I've started attacking this issue at https://go.dev/cl/564338 and added tests/enabled the usage of refactor.extract.constant, refactor.extract.function, and refactor.extract.variable.
One possible issue is that I have not implemented a new type of code action for constants- the refactor.extract.constant still calls internal/golang/extract.go:extractVariable. The issue here is if someone calls refactor.extract.constant on a non-constant variable, the typical variable extraction will occur rather than an error stating that the target must be a constant.
I am not well-versed in the ast package that extractVariable uses to analyze the type of variable, but I imagine it would be possible to confirm that the target is a constant and then run the extraction. Does anyone have advice here? Thanks in advance.
Thanks, @NateWilliams2. I commented on your CL. In order to check if an expression is constant, we need type information. We can discuss further on the CL, if you want.
Activity
gopherbot commentedon Mar 3, 2020
Change https://golang.org/cl/221917 mentions this issue:
internal/lsp: add code action to extract local variablegopherbot commentedon Jul 20, 2020
Change https://golang.org/cl/241957 mentions this issue:
internal/lsp: support extract functionjoshbaum commentedon Jul 20, 2020
https://golang.org/cl/241957 and https://golang.org/cl/240182 support extracting a function and variable, respectively, as a code action. The following are open issues with function extraction:
internal/lsp: support extract function
gopherbot commentedon Jul 20, 2020
Change https://golang.org/cl/243650 mentions this issue:
internal/lsp/source: support return statements in extract function23 remaining items
gopherbot commentedon Feb 15, 2024
Change https://go.dev/cl/564338 mentions this issue:
gopls: Enable refactor.extract.constant/variable/function CodeActionKindsNateWilliams2 commentedon Feb 15, 2024
I've started attacking this issue at https://go.dev/cl/564338 and added tests/enabled the usage of
refactor.extract.constant,refactor.extract.function, andrefactor.extract.variable.One possible issue is that I have not implemented a new type of code action for constants- the
refactor.extract.constantstill callsinternal/golang/extract.go:extractVariable. The issue here is if someone callsrefactor.extract.constanton a non-constant variable, the typical variable extraction will occur rather than an error stating that the target must be a constant.I am not well-versed in the ast package that
extractVariableuses to analyze the type of variable, but I imagine it would be possible to confirm that the target is a constant and then run the extraction. Does anyone have advice here? Thanks in advance.findleyr commentedon Feb 16, 2024
Thanks, @NateWilliams2. I commented on your CL. In order to check if an expression is constant, we need type information. We can discuss further on the CL, if you want.
gopherbot commentedon Feb 19, 2024
Change https://go.dev/cl/565235 mentions this issue:
gopls: Enable refactor.extract.constant/variable/function CodeActionKindsgopherbot commentedon Feb 26, 2024
Change https://go.dev/cl/567135 mentions this issue:
gopls: Enable refactor.extract.constant/variable/function CodeActionKindsgopherbot commentedon Dec 3, 2024
Change https://go.dev/cl/633257 mentions this issue:
gopls/internal/golang: add "Extract constant" counterpartgopls/internal/golang: add "Extract constant" counterpart
gopherbot commentedon Dec 4, 2024
Change https://go.dev/cl/633717 mentions this issue:
gopls/internal/golang: add "Extract constant" counterpart