-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/golang: add "Extract constant" counterpart
If the selection is a constant expression, instead of "Extract variable", gopls now offers "Extract constant", and generates a constant declaration. Regrettably, I noticed pending CL 564338 only while composing this CL description; this CL supersedes that one. Apologies for the redundant efforts. + Test, doc, relnote Fixes golang/go#37170 Updates golang/go#63852 Change-Id: I1376662b50820936de1e156413537e0bcc2292ff Reviewed-on: https://go-review.googlesource.com/c/tools/+/633257 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Loading branch information
Showing
14 changed files
with
220 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
gopls/internal/test/marker/testdata/codeaction/extract_variable-if.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
This test checks the behavior of the 'extract variable/constant' code actions | ||
when the optimal place for the new declaration is within the "if" statement, | ||
like so: | ||
|
||
if x := 1 + 2 or y + y ; true { | ||
} else if x > 0 { | ||
} | ||
|
||
A future refactor.variable implementation that does this should avoid | ||
using a 'const' declaration, which is not legal at that location. | ||
|
||
-- flags -- | ||
-ignore_extra_diags | ||
|
||
-- a.go -- | ||
package a | ||
|
||
func constant() { | ||
if true { | ||
} else if 1 + 2 > 0 { //@ codeaction("1 + 2", "refactor.extract.constant", edit=constant) | ||
} | ||
} | ||
|
||
func variable(y int) { | ||
if true { | ||
} else if y + y > 0 { //@ codeaction("y + y", "refactor.extract.variable", edit=variable) | ||
} | ||
} | ||
|
||
-- @constant/a.go -- | ||
@@ -4 +4 @@ | ||
+ const x = 1 + 2 | ||
@@ -5 +6 @@ | ||
- } else if 1 + 2 > 0 { //@ codeaction("1 + 2", "refactor.extract.constant", edit=constant) | ||
+ } else if x > 0 { //@ codeaction("1 + 2", "refactor.extract.constant", edit=constant) | ||
-- @variable/a.go -- | ||
@@ -10 +10 @@ | ||
+ x := y + y | ||
@@ -11 +12 @@ | ||
- } else if y + y > 0 { //@ codeaction("y + y", "refactor.extract.variable", edit=variable) | ||
+ } else if x > 0 { //@ codeaction("y + y", "refactor.extract.variable", edit=variable) |
Oops, something went wrong.