Skip to content

x/tools/gopls: add support for 'refactor.extract.constant' code actions #37170

Closed

Description

@stamblerre

See microsoft/vscode-go#3040.

https://microsoft.github.io/language-server-protocol/specifications/specification-3-15/#textDocument_codeAction has more details on the possibilities here.

The function refactoring can be implemented by making use of guru's freevars code.

Activity

  1. added this to the gopls/v1.0.0 milestone on Feb 11, 2020
  2. added
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    goplsIssues related to the Go language server, gopls.
    on Feb 11, 2020
  3. gopherbot commented on Mar 3, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/221917 mentions this issue: internal/lsp: add code action to extract local variable

  4. added
    FeatureRequestIssues asking for a new feature that does not need a proposal.
    on Mar 12, 2020
  5. removed their assignment
    on Apr 1, 2020
  6. gopherbot commented on Jul 20, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/241957 mentions this issue: internal/lsp: support extract function

  7. joshbaum commented on Jul 20, 2020

    @joshbaum

    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:

    • 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.
  8. gopherbot commented on Jul 20, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/243650 mentions this issue: internal/lsp/source: support return statements in extract function

  9. removed this from the Unreleased milestone on Jul 23, 2020
  10. 23 remaining items

  11. added this to the gopls/backlog milestone on Nov 2, 2023
  12. gopherbot commented on Feb 15, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/564338 mentions this issue: gopls: Enable refactor.extract.constant/variable/function CodeActionKinds

  13. NateWilliams2 commented on Feb 15, 2024

    @NateWilliams2

    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.

  14. findleyr commented on Feb 16, 2024

    @findleyr
    Contributor

    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.

  15. gopherbot commented on Feb 19, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/565235 mentions this issue: gopls: Enable refactor.extract.constant/variable/function CodeActionKinds

  16. gopherbot commented on Feb 26, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/567135 mentions this issue: gopls: Enable refactor.extract.constant/variable/function CodeActionKinds

  17. gopherbot commented on Dec 3, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/633257 mentions this issue: gopls/internal/golang: add "Extract constant" counterpart

  18. self-assigned this
    on Dec 3, 2024
  19. added a commit that references this issue on Dec 4, 2024
    61b2408
  20. gopherbot commented on Dec 4, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/633717 mentions this issue: gopls/internal/golang: add "Extract constant" counterpart

  21. locked and limited conversation to collaborators on Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FeatureRequestIssues asking for a new feature that does not need a proposal.FrozenDueToAgeRefactoringIssues related to refactoring toolsToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.help wanted

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions