-
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: support extract variable at top level
This CL causes the refactor.extract.{variable,constant} logic to support extractions outside of statement context, inserting a new package-level declaration. Previously, it returned an error. Also, use "k" as the basis for choosing names for new constants. Also, simplify generateAvailableName et al. Fixes golang/go#70665 Change-Id: I769206b14662e4e70ee04ab6c0040e635ac72820 Reviewed-on: https://go-review.googlesource.com/c/tools/+/633597 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Robert Findley <rfindley@google.com>
- Loading branch information
Showing
9 changed files
with
169 additions
and
83 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
51 changes: 51 additions & 0 deletions
51
gopls/internal/test/marker/testdata/codeaction/extract_variable-toplevel.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,51 @@ | ||
This test checks the behavior of the 'extract variable/constant' code action | ||
at top level (outside any function). See issue #70665. | ||
|
||
-- a.go -- | ||
package a | ||
|
||
const length = len("hello") + 2 //@codeaction(`len("hello")`, "refactor.extract.constant", edit=lenhello) | ||
|
||
var slice = append([]int{}, 1, 2, 3) //@codeaction("[]int{}", "refactor.extract.variable", edit=sliceliteral) | ||
|
||
type SHA256 [32]byte //@codeaction("32", "refactor.extract.constant", edit=arraylen) | ||
|
||
func f([2]int) {} //@codeaction("2", "refactor.extract.constant", edit=paramtypearraylen) | ||
|
||
-- @lenhello/a.go -- | ||
@@ -3 +3,2 @@ | ||
-const length = len("hello") + 2 //@codeaction(`len("hello")`, "refactor.extract.constant", edit=lenhello) | ||
+const k = len("hello") | ||
+const length = k + 2 //@codeaction(`len("hello")`, "refactor.extract.constant", edit=lenhello) | ||
-- @sliceliteral/a.go -- | ||
@@ -5 +5,2 @@ | ||
-var slice = append([]int{}, 1, 2, 3) //@codeaction("[]int{}", "refactor.extract.variable", edit=sliceliteral) | ||
+var x = []int{} | ||
+var slice = append(x, 1, 2, 3) //@codeaction("[]int{}", "refactor.extract.variable", edit=sliceliteral) | ||
-- @arraylen/a.go -- | ||
@@ -7 +7,2 @@ | ||
-type SHA256 [32]byte //@codeaction("32", "refactor.extract.constant", edit=arraylen) | ||
+const k = 32 | ||
+type SHA256 [k]byte //@codeaction("32", "refactor.extract.constant", edit=arraylen) | ||
-- @paramtypearraylen/a.go -- | ||
@@ -9 +9,2 @@ | ||
-func f([2]int) {} //@codeaction("2", "refactor.extract.constant", edit=paramtypearraylen) | ||
+const k = 2 | ||
+func f([k]int) {} //@codeaction("2", "refactor.extract.constant", edit=paramtypearraylen) | ||
-- b/b.go -- | ||
package b | ||
|
||
// Check that package- and file-level name collisions are avoided. | ||
|
||
import x3 "errors" | ||
|
||
var x, x1, x2 any // these names are taken already | ||
var _ = x3.New("") | ||
var a, b int | ||
var c = a + b //@codeaction("a + b", "refactor.extract.variable", edit=fresh) | ||
|
||
-- @fresh/b/b.go -- | ||
@@ -10 +10,2 @@ | ||
-var c = a + b //@codeaction("a + b", "refactor.extract.variable", edit=fresh) | ||
+var x4 = a + b | ||
+var c = x4 //@codeaction("a + b", "refactor.extract.variable", edit=fresh) |
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
Oops, something went wrong.