Skip to content

Commit 16b25d2

Browse files
committed
internal/lsp: print comments that would be lost during extract func
Due to the limitations of comments in ast, it is difficult to move comments. The extract function feature currently does not handle comments at all. This change instead prints the comments that would have been lost above the call to the function, so that the user can easily recover them. Otherwise, it was possible for users to lose comments and not notice. Updates golang/go#37170 Change-Id: I1e2d865f5deddefbb0417732490decbdfcde5f3d Reviewed-on: https://go-review.googlesource.com/c/tools/+/313211 Trust: Suzy Mueller <suzmue@golang.org> Run-TryBot: Suzy Mueller <suzmue@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent 7c72a84 commit 16b25d2

18 files changed

+55
-2
lines changed

internal/lsp/source/extract.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func extractFunction(fset *token.FileSet, rng span.Range, src []byte, file *ast.
487487
declarations = initializeVars(uninitialized, retVars, seenUninitialized, seenVars)
488488
}
489489

490-
var declBuf, replaceBuf, newFuncBuf, ifBuf bytes.Buffer
490+
var declBuf, replaceBuf, newFuncBuf, ifBuf, commentBuf bytes.Buffer
491491
if err := format.Node(&declBuf, fset, declarations); err != nil {
492492
return nil, err
493493
}
@@ -502,6 +502,15 @@ func extractFunction(fset *token.FileSet, rng span.Range, src []byte, file *ast.
502502
if err := format.Node(&newFuncBuf, fset, newFunc); err != nil {
503503
return nil, err
504504
}
505+
// Find all the comments within the range and print them to be put somewhere.
506+
// TODO(suzmue): print these in the extracted function at the correct place.
507+
for _, cg := range file.Comments {
508+
if cg.Pos().IsValid() && cg.Pos() < rng.End && cg.Pos() >= rng.Start {
509+
for _, c := range cg.List {
510+
fmt.Fprintln(&commentBuf, c.Text)
511+
}
512+
}
513+
}
505514

506515
// We're going to replace the whole enclosing function,
507516
// so preserve the text before and after the selected block.
@@ -513,6 +522,10 @@ func extractFunction(fset *token.FileSet, rng span.Range, src []byte, file *ast.
513522

514523
var fullReplacement strings.Builder
515524
fullReplacement.Write(before)
525+
if commentBuf.Len() > 0 {
526+
comments := strings.ReplaceAll(commentBuf.String(), "\n", newLineIndent)
527+
fullReplacement.WriteString(comments)
528+
}
516529
if declBuf.Len() > 0 { // add any initializations, if needed
517530
initializations := strings.ReplaceAll(declBuf.String(), "\n", newLineIndent) +
518531
newLineIndent

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package extract
33

44
func _() {
55
a := 1
6+
//@mark(exSt0, "a")
67
a = fn0(a) //@mark(exEn0, "2")
78
//@extractfunc(exSt0, exEn0)
89
b := a * 2 //@mark(exB, " b")
@@ -24,6 +25,7 @@ func _() {
2425
a = 5 //@mark(exSt0, "a")
2526
a = a + 2 //@mark(exEn0, "2")
2627
//@extractfunc(exSt0, exEn0)
28+
//@mark(exB, " b")
2729
fn0(a) //@mark(exEnd, "4")
2830
//@extractfunc(exB, exEnd)
2931
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package extract
33

44
func _() {
5+
//@mark(exSt1, "a")
56
fn0() //@mark(exEn1, "4")
67
//@extractfunc(exSt1, exEn1)
78
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package extract
2+
3+
func _() {
4+
a := /* comment in the middle of a line */ 1 //@mark(exSt18, "a")
5+
// Comment on its own line
6+
_ = 3 + 4 //@mark(exEn18, "4")
7+
//@extractfunc(exSt18, exEn18)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- functionextraction_extract_basic_comment_4_2 --
2+
package extract
3+
4+
func _() {
5+
/* comment in the middle of a line */
6+
//@mark(exSt18, "a")
7+
// Comment on its own line
8+
fn0() //@mark(exEn18, "4")
9+
//@extractfunc(exSt18, exEn18)
10+
}
11+
12+
func fn0() {
13+
a := 1
14+
15+
_ = 3 + 4
16+
}
17+

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

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package extract
44
import "fmt"
55

66
func main() {
7+
//@mark(exSt9, "x")
78
x := fn0() //@mark(exEn9, "}")
89
//@extractfunc(exSt9, exEn9)
910
fmt.Printf("%x\n", x)

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package extract
33

44
func _() bool {
55
x := 1
6+
//@mark(exSt2, "if")
67
cond0, ret0 := fn0(x)
78
if cond0 {
89
return ret0

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

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package extract
33

44
func _() bool {
5+
//@mark(exSt13, "x")
56
return fn0() //@mark(exEn13, "false")
67
//@extractfunc(exSt13, exEn13)
78
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "fmt"
66
func _() (int, string, error) {
77
x := 1
88
y := "hello"
9+
//@mark(exSt3, "z")
910
z, cond0, ret0, ret1, ret2 := fn0(y, x)
1011
if cond0 {
1112
return ret0, ret1, ret2

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

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "fmt"
66
func _() (int, string, error) {
77
x := 1
88
y := "hello"
9+
//@mark(exSt10, "z")
910
return fn0(y, x) //@mark(exEn10, "nil")
1011
//@extractfunc(exSt10, exEn10)
1112
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "go/ast"
55

66
func _() {
77
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
8+
//@mark(exSt4, "if")
89
cond0, ret0 := fn0(n)
910
if cond0 {
1011
return ret0

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "go/ast"
55

66
func _() {
77
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
8+
//@mark(exSt11, "if")
89
return fn0(n) //@mark(exEn11, "false")
910
})
1011
//@extractfunc(exSt11, exEn11)

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package extract
33

44
func _() string {
55
x := 1
6+
//@mark(exSt5, "if")
67
cond0, ret0 := fn0(x)
78
if cond0 {
89
return ret0

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package extract
33

44
func _() string {
55
x := 1
6+
//@mark(exSt12, "if")
67
return fn0(x) //@mark(exEn12, "\"b\"")
78
//@extractfunc(exSt12, exEn12)
89
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package extract
33

44
func _() {
55
var a []int
6+
//@mark(exSt6, "a")
67
a, b := fn0(a) //@mark(exEn6, "4")
78
//@extractfunc(exSt6, exEn6)
89
a = append(a, b)

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

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package extract
44
func _() {
55
var b []int
66
var a int
7+
//@mark(exSt7, "a")
78
b = fn0(a, b) //@mark(exEn7, ")")
89
b[0] = 1
910
//@extractfunc(exSt7, exEn7)

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

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package extract
44
func _() {
55
var b []int
66
var a int
7+
//@mark(exSt8, "a")
78
a, b = fn0(b) //@mark(exEn8, ")")
89
b[0] = 1
910
if a == 2 {

internal/lsp/testdata/summary.txt.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ FormatCount = 6
1414
ImportCount = 8
1515
SemanticTokenCount = 3
1616
SuggestedFixCount = 40
17-
FunctionExtractionCount = 17
17+
FunctionExtractionCount = 18
1818
DefinitionsCount = 95
1919
TypeDefinitionsCount = 10
2020
HighlightsCount = 69

0 commit comments

Comments
 (0)