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

x/tools/gopls: add support for extracting a function/variable as a code action #37170

Open
stamblerre opened this issue Feb 11, 2020 · 4 comments
Open
Assignees

Comments

@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Feb 11, 2020

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.

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 3, 2020

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

@gopherbot
Copy link

@gopherbot gopherbot commented Jul 20, 2020

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

@joshbaum
Copy link

@joshbaum joshbaum commented 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:

  • 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.
gopherbot pushed a commit to golang/tools that referenced this issue Jul 20, 2020
Extract function is a code action, similar to extract variable. After
highlighting a selection, if valid, the lightbulb appears to trigger
extraction. The current implementation does not allow users to
extract selections with a return statement.

Updates golang/go#37170

Change-Id: I5fc3b19cf7dbca4407ecf0cc37017661223614d1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/241957
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Josh Baum <joshbaum@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
@gopherbot
Copy link

@gopherbot gopherbot commented Jul 20, 2020

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

@stamblerre stamblerre removed this from the Unreleased milestone Jul 23, 2020
gopherbot pushed a commit to golang/tools that referenced this issue Jul 28, 2020
Previously, users could not extract code that contained a return
statement. Now, users can extract code with return statements, as long
as the statements are nested within an if, case, or other control
flow statement.

Updates golang/go#37170

Change-Id: I2df52d0241517472decabce3666a32392ff257bd
Reviewed-on: https://go-review.googlesource.com/c/tools/+/243650
Run-TryBot: Josh Baum <joshbaum@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.