Skip to content

Commit 890984b

Browse files
committed
internal/lsp: change generated variable names to be more verbose
If new functions or variables need to be created during an extract call, then we choose the new names. This change makes those names more descriptive to make the generated names easier to read and understand. Before: cond0, ret0 := fn0() if cond0 { return ret0 } After shouldReturn, returnValue := newFunction() if shouldReturn { return returnValue } Change-Id: I44795dc45185c75d5bf65e48378aa54d6c156212 Reviewed-on: https://go-review.googlesource.com/c/tools/+/326112 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 9f230b5 commit 890984b

21 files changed

+63
-60
lines changed

internal/lsp/source/extract.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ func calculateIndentation(content []byte, tok *token.File, insertBeforeStmt ast.
139139
// Possible collisions include other function and variable names. Returns the next index to check for prefix.
140140
func generateAvailableIdentifier(pos token.Pos, file *ast.File, path []ast.Node, info *types.Info, prefix string, idx int) (string, int) {
141141
scopes := CollectScopes(info, path, pos)
142-
name := prefix + fmt.Sprintf("%d", idx)
142+
name := prefix
143+
if idx != 0 {
144+
name += fmt.Sprintf("%d", idx)
145+
}
143146
for file.Scope.Lookup(name) != nil || !isValidName(name, scopes) {
144147
idx++
145148
name = fmt.Sprintf("%v%d", prefix, idx)
@@ -468,7 +471,7 @@ func extractFunction(fset *token.FileSet, rng span.Range, src []byte, file *ast.
468471
if canDefine {
469472
sym = token.DEFINE
470473
}
471-
funName, _ := generateAvailableIdentifier(rng.Start, file, path, info, "fn", 0)
474+
funName, _ := generateAvailableIdentifier(rng.Start, file, path, info, "newFunction", 0)
472475
extractedFunCall := generateFuncCall(hasNonNestedReturn, hasReturnValues, params,
473476
append(returns, getNames(retVars)...), funName, sym)
474477

@@ -999,7 +1002,7 @@ func generateReturnInfo(enclosing *ast.FuncType, pkg *types.Package, path []ast.
9991002
var cond *ast.Ident
10001003
if !hasNonNestedReturns {
10011004
// Generate information for the added bool value.
1002-
name, _ := generateAvailableIdentifier(pos, file, path, info, "cond", 0)
1005+
name, _ := generateAvailableIdentifier(pos, file, path, info, "shouldReturn", 0)
10031006
cond = &ast.Ident{Name: name}
10041007
retVars = append(retVars, &returnVariable{
10051008
name: cond,
@@ -1022,7 +1025,7 @@ func generateReturnInfo(enclosing *ast.FuncType, pkg *types.Package, path []ast.
10221025
}
10231026
var name string
10241027
name, idx = generateAvailableIdentifier(pos, file,
1025-
path, info, "ret", idx)
1028+
path, info, "returnValue", idx)
10261029
retVars = append(retVars, &returnVariable{
10271030
name: ast.NewIdent(name),
10281031
decl: &ast.Field{Type: expr},

internal/lsp/testdata/extract/extract_function/extract_args_returns.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ package extract
44
func _() {
55
a := 1
66
//@mark(exSt0, "a")
7-
a = fn0(a) //@mark(exEn0, "2")
7+
a = newFunction(a) //@mark(exEn0, "2")
88
//@extractfunc(exSt0, exEn0)
99
b := a * 2 //@mark(exB, " b")
1010
_ = 3 + 4 //@mark(exEnd, "4")
1111
//@extractfunc(exB, exEnd)
1212
}
1313

14-
func fn0(a int) int {
14+
func newFunction(a int) int {
1515
a = 5
1616
a = a + 2
1717
return a
@@ -26,11 +26,11 @@ func _() {
2626
a = a + 2 //@mark(exEn0, "2")
2727
//@extractfunc(exSt0, exEn0)
2828
//@mark(exB, " b")
29-
fn0(a) //@mark(exEnd, "4")
29+
newFunction(a) //@mark(exEnd, "4")
3030
//@extractfunc(exB, exEnd)
3131
}
3232

33-
func fn0(a int) {
33+
func newFunction(a int) {
3434
b := a * 2
3535
_ = 3 + 4
3636
}

internal/lsp/testdata/extract/extract_function/extract_basic.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package extract
33

44
func _() {
55
//@mark(exSt1, "a")
6-
fn0() //@mark(exEn1, "4")
6+
newFunction() //@mark(exEn1, "4")
77
//@extractfunc(exSt1, exEn1)
88
}
99

10-
func fn0() {
10+
func newFunction() {
1111
a := 1
1212
_ = 3 + 4
1313
}

internal/lsp/testdata/extract/extract_function/extract_basic_comment.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ func _() {
55
/* comment in the middle of a line */
66
//@mark(exSt18, "a")
77
// Comment on its own line
8-
fn0() //@mark(exEn18, "4")
8+
newFunction() //@mark(exEn18, "4")
99
//@extractfunc(exSt18, exEn18)
1010
}
1111

12-
func fn0() {
12+
func newFunction() {
1313
a := 1
1414

1515
_ = 3 + 4

internal/lsp/testdata/extract/extract_function/extract_issue_44813.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import "fmt"
55

66
func main() {
77
//@mark(exSt9, "x")
8-
x := fn0() //@mark(exEn9, "}")
8+
x := newFunction() //@mark(exEn9, "}")
99
//@extractfunc(exSt9, exEn9)
1010
fmt.Printf("%x\n", x)
1111
}
1212

13-
func fn0() []rune {
13+
func newFunction() []rune {
1414
x := []rune{}
1515
s := "HELLO"
1616
for _, c := range s {

internal/lsp/testdata/extract/extract_function/extract_redefine.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import "strconv"
55

66
func _() {
77
i, err := strconv.Atoi("1")
8-
u, err := fn0() //@extractfunc("u", ")")
8+
u, err := newFunction() //@extractfunc("u", ")")
99
if i == u || err == nil {
1010
return
1111
}
1212
}
1313

14-
func fn0() (int, error) {
14+
func newFunction() (int, error) {
1515
u, err := strconv.Atoi("2")
1616
return u, err
1717
}

internal/lsp/testdata/extract/extract_function/extract_return_basic.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ package extract
44
func _() bool {
55
x := 1
66
//@mark(exSt2, "if")
7-
cond0, ret0 := fn0(x)
8-
if cond0 {
9-
return ret0
7+
shouldReturn, returnValue := newFunction(x)
8+
if shouldReturn {
9+
return returnValue
1010
} //@mark(exEn2, "}")
1111
return false
1212
//@extractfunc(exSt2, exEn2)
1313
}
1414

15-
func fn0(x int) (bool, bool) {
15+
func newFunction(x int) (bool, bool) {
1616
if x == 0 {
1717
return true, true
1818
}

internal/lsp/testdata/extract/extract_function/extract_return_basic_nonnested.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package extract
33

44
func _() bool {
55
//@mark(exSt13, "x")
6-
return fn0() //@mark(exEn13, "false")
6+
return newFunction() //@mark(exEn13, "false")
77
//@extractfunc(exSt13, exEn13)
88
}
99

10-
func fn0() bool {
10+
func newFunction() bool {
1111
x := 1
1212
if x == 0 {
1313
return true

internal/lsp/testdata/extract/extract_function/extract_return_complex.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ func _() (int, string, error) {
77
x := 1
88
y := "hello"
99
//@mark(exSt3, "z")
10-
z, cond0, ret0, ret1, ret2 := fn0(y, x)
11-
if cond0 {
12-
return ret0, ret1, ret2
10+
z, shouldReturn, returnValue, returnValue1, returnValue2 := newFunction(y, x)
11+
if shouldReturn {
12+
return returnValue, returnValue1, returnValue2
1313
} //@mark(exEn3, "}")
1414
return x, z, nil
1515
//@extractfunc(exSt3, exEn3)
1616
}
1717

18-
func fn0(y string, x int) (string, bool, int, string, error) {
18+
func newFunction(y string, x int) (string, bool, int, string, error) {
1919
z := "bye"
2020
if y == z {
2121
return "", true, x, y, fmt.Errorf("same")

internal/lsp/testdata/extract/extract_function/extract_return_complex_nonnested.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ func _() (int, string, error) {
77
x := 1
88
y := "hello"
99
//@mark(exSt10, "z")
10-
return fn0(y, x) //@mark(exEn10, "nil")
10+
return newFunction(y, x) //@mark(exEn10, "nil")
1111
//@extractfunc(exSt10, exEn10)
1212
}
1313

14-
func fn0(y string, x int) (int, string, error) {
14+
func newFunction(y string, x int) (int, string, error) {
1515
z := "bye"
1616
if y == z {
1717
return x, y, fmt.Errorf("same")

internal/lsp/testdata/extract/extract_function/extract_return_func_lit.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import "go/ast"
66
func _() {
77
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
88
//@mark(exSt4, "if")
9-
cond0, ret0 := fn0(n)
10-
if cond0 {
11-
return ret0
9+
shouldReturn, returnValue := newFunction(n)
10+
if shouldReturn {
11+
return returnValue
1212
} //@mark(exEn4, "}")
1313
return false
1414
})
1515
//@extractfunc(exSt4, exEn4)
1616
}
1717

18-
func fn0(n ast.Node) (bool, bool) {
18+
func newFunction(n ast.Node) (bool, bool) {
1919
if n == nil {
2020
return true, true
2121
}

internal/lsp/testdata/extract/extract_function/extract_return_func_lit_nonnested.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import "go/ast"
66
func _() {
77
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
88
//@mark(exSt11, "if")
9-
return fn0(n) //@mark(exEn11, "false")
9+
return newFunction(n) //@mark(exEn11, "false")
1010
})
1111
//@extractfunc(exSt11, exEn11)
1212
}
1313

14-
func fn0(n ast.Node) bool {
14+
func newFunction(n ast.Node) bool {
1515
if n == nil {
1616
return true
1717
}

internal/lsp/testdata/extract/extract_function/extract_return_init.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ package extract
44
func _() string {
55
x := 1
66
//@mark(exSt5, "if")
7-
cond0, ret0 := fn0(x)
8-
if cond0 {
9-
return ret0
7+
shouldReturn, returnValue := newFunction(x)
8+
if shouldReturn {
9+
return returnValue
1010
} //@mark(exEn5, "}")
1111
x = 2
1212
return "b"
1313
//@extractfunc(exSt5, exEn5)
1414
}
1515

16-
func fn0(x int) (bool, string) {
16+
func newFunction(x int) (bool, string) {
1717
if x == 0 {
1818
x = 3
1919
return true, "a"

internal/lsp/testdata/extract/extract_function/extract_return_init_nonnested.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ package extract
44
func _() string {
55
x := 1
66
//@mark(exSt12, "if")
7-
return fn0(x) //@mark(exEn12, "\"b\"")
7+
return newFunction(x) //@mark(exEn12, "\"b\"")
88
//@extractfunc(exSt12, exEn12)
99
}
1010

11-
func fn0(x int) string {
11+
func newFunction(x int) string {
1212
if x == 0 {
1313
x = 3
1414
return "a"

internal/lsp/testdata/extract/extract_function/extract_scope.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package extract
33

44
func _() {
55
fn0 := 1
6-
fn2(fn0) //@extractfunc("a", "fn0")
6+
newFunction(fn0) //@extractfunc("a", "fn0")
77
}
88

9-
func fn2(fn0 int) {
9+
func newFunction(fn0 int) {
1010
a := fn0
1111
}
1212

internal/lsp/testdata/extract/extract_function/extract_smart_initialization.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ package extract
44
func _() {
55
var a []int
66
//@mark(exSt6, "a")
7-
a, b := fn0(a) //@mark(exEn6, "4")
7+
a, b := newFunction(a) //@mark(exEn6, "4")
88
//@extractfunc(exSt6, exEn6)
99
a = append(a, b)
1010
}
1111

12-
func fn0(a []int) ([]int, int) {
12+
func newFunction(a []int) ([]int, int) {
1313
a = append(a, 2)
1414
b := 4
1515
return a, b

internal/lsp/testdata/extract/extract_function/extract_smart_return.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ func _() {
55
var b []int
66
var a int
77
//@mark(exSt7, "a")
8-
b = fn0(a, b) //@mark(exEn7, ")")
8+
b = newFunction(a, b) //@mark(exEn7, ")")
99
b[0] = 1
1010
//@extractfunc(exSt7, exEn7)
1111
}
1212

13-
func fn0(a int, b []int) []int {
13+
func newFunction(a int, b []int) []int {
1414
a = 2
1515
b = []int{}
1616
b = append(b, a)

internal/lsp/testdata/extract/extract_function/extract_unnecessary_param.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ func _() {
55
var b []int
66
var a int
77
//@mark(exSt8, "a")
8-
a, b = fn0(b) //@mark(exEn8, ")")
8+
a, b = newFunction(b) //@mark(exEn8, ")")
99
b[0] = 1
1010
if a == 2 {
1111
return
1212
}
1313
//@extractfunc(exSt8, exEn8)
1414
}
1515

16-
func fn0(b []int) (int, []int) {
16+
func newFunction(b []int) (int, []int) {
1717
a := 2
1818
b = []int{}
1919
b = append(b, a)

internal/lsp/testdata/extract/extract_variable/extract_basic_lit.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
package extract
33

44
func _() {
5-
x0 := 1
6-
var _ = x0 + 2 //@suggestedfix("1", "refactor.extract")
5+
x := 1
6+
var _ = x + 2 //@suggestedfix("1", "refactor.extract")
77
var _ = 3 + 4 //@suggestedfix("3 + 4", "refactor.extract")
88
}
99

@@ -12,7 +12,7 @@ package extract
1212

1313
func _() {
1414
var _ = 1 + 2 //@suggestedfix("1", "refactor.extract")
15-
x0 := 3 + 4
16-
var _ = x0 //@suggestedfix("3 + 4", "refactor.extract")
15+
x := 3 + 4
16+
var _ = x //@suggestedfix("3 + 4", "refactor.extract")
1717
}
1818

internal/lsp/testdata/extract/extract_variable/extract_func_call.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ package extract
1616
import "strconv"
1717

1818
func _() {
19-
x1 := append([]int{}, 1)
20-
x0 := x1 //@suggestedfix("append([]int{}, 1)", "refactor.extract")
19+
x := append([]int{}, 1)
20+
x0 := x //@suggestedfix("append([]int{}, 1)", "refactor.extract")
2121
str := "1"
2222
b, err := strconv.Atoi(str) //@suggestedfix("strconv.Atoi(str)", "refactor.extract")
2323
}
@@ -30,7 +30,7 @@ import "strconv"
3030
func _() {
3131
x0 := append([]int{}, 1) //@suggestedfix("append([]int{}, 1)", "refactor.extract")
3232
str := "1"
33-
x1, x2 := strconv.Atoi(str)
34-
b, err := x1, x2 //@suggestedfix("strconv.Atoi(str)", "refactor.extract")
33+
x, x1 := strconv.Atoi(str)
34+
b, err := x, x1 //@suggestedfix("strconv.Atoi(str)", "refactor.extract")
3535
}
3636

internal/lsp/testdata/extract/extract_variable/extract_scope.go.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ func _() {
99
y := ast.CompositeLit{} //@suggestedfix("ast.CompositeLit{}", "refactor.extract")
1010
}
1111
if true {
12-
x2 := !false
13-
x1 := x2 //@suggestedfix("!false", "refactor.extract")
12+
x := !false
13+
x1 := x //@suggestedfix("!false", "refactor.extract")
1414
}
1515
}
1616

@@ -22,8 +22,8 @@ import "go/ast"
2222
func _() {
2323
x0 := 0
2424
if true {
25-
x1 := ast.CompositeLit{}
26-
y := x1 //@suggestedfix("ast.CompositeLit{}", "refactor.extract")
25+
x := ast.CompositeLit{}
26+
y := x //@suggestedfix("ast.CompositeLit{}", "refactor.extract")
2727
}
2828
if true {
2929
x1 := !false //@suggestedfix("!false", "refactor.extract")

0 commit comments

Comments
 (0)