Skip to content

Commit

Permalink
internal/lsp: print comments that would be lost during extract func
Browse files Browse the repository at this point in the history
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 <[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]>
  • Loading branch information
suzmue committed Apr 28, 2021
1 parent 7c72a84 commit 16b25d2
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 2 deletions.
15 changes: 14 additions & 1 deletion internal/lsp/source/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func extractFunction(fset *token.FileSet, rng span.Range, src []byte, file *ast.
declarations = initializeVars(uninitialized, retVars, seenUninitialized, seenVars)
}

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

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

var fullReplacement strings.Builder
fullReplacement.Write(before)
if commentBuf.Len() > 0 {
comments := strings.ReplaceAll(commentBuf.String(), "\n", newLineIndent)
fullReplacement.WriteString(comments)
}
if declBuf.Len() > 0 { // add any initializations, if needed
initializations := strings.ReplaceAll(declBuf.String(), "\n", newLineIndent) +
newLineIndent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package extract

func _() {
a := 1
//@mark(exSt0, "a")
a = fn0(a) //@mark(exEn0, "2")
//@extractfunc(exSt0, exEn0)
b := a * 2 //@mark(exB, " b")
Expand All @@ -24,6 +25,7 @@ func _() {
a = 5 //@mark(exSt0, "a")
a = a + 2 //@mark(exEn0, "2")
//@extractfunc(exSt0, exEn0)
//@mark(exB, " b")
fn0(a) //@mark(exEnd, "4")
//@extractfunc(exB, exEnd)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package extract

func _() {
//@mark(exSt1, "a")
fn0() //@mark(exEn1, "4")
//@extractfunc(exSt1, exEn1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package extract

func _() {
a := /* comment in the middle of a line */ 1 //@mark(exSt18, "a")
// Comment on its own line
_ = 3 + 4 //@mark(exEn18, "4")
//@extractfunc(exSt18, exEn18)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- functionextraction_extract_basic_comment_4_2 --
package extract

func _() {
/* comment in the middle of a line */
//@mark(exSt18, "a")
// Comment on its own line
fn0() //@mark(exEn18, "4")
//@extractfunc(exSt18, exEn18)
}

func fn0() {
a := 1

_ = 3 + 4
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package extract
import "fmt"

func main() {
//@mark(exSt9, "x")
x := fn0() //@mark(exEn9, "}")
//@extractfunc(exSt9, exEn9)
fmt.Printf("%x\n", x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package extract

func _() bool {
x := 1
//@mark(exSt2, "if")
cond0, ret0 := fn0(x)
if cond0 {
return ret0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package extract

func _() bool {
//@mark(exSt13, "x")
return fn0() //@mark(exEn13, "false")
//@extractfunc(exSt13, exEn13)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "fmt"
func _() (int, string, error) {
x := 1
y := "hello"
//@mark(exSt3, "z")
z, cond0, ret0, ret1, ret2 := fn0(y, x)
if cond0 {
return ret0, ret1, ret2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "fmt"
func _() (int, string, error) {
x := 1
y := "hello"
//@mark(exSt10, "z")
return fn0(y, x) //@mark(exEn10, "nil")
//@extractfunc(exSt10, exEn10)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "go/ast"

func _() {
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
//@mark(exSt4, "if")
cond0, ret0 := fn0(n)
if cond0 {
return ret0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "go/ast"

func _() {
ast.Inspect(ast.NewIdent("a"), func(n ast.Node) bool {
//@mark(exSt11, "if")
return fn0(n) //@mark(exEn11, "false")
})
//@extractfunc(exSt11, exEn11)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package extract

func _() string {
x := 1
//@mark(exSt5, "if")
cond0, ret0 := fn0(x)
if cond0 {
return ret0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package extract

func _() string {
x := 1
//@mark(exSt12, "if")
return fn0(x) //@mark(exEn12, "\"b\"")
//@extractfunc(exSt12, exEn12)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package extract

func _() {
var a []int
//@mark(exSt6, "a")
a, b := fn0(a) //@mark(exEn6, "4")
//@extractfunc(exSt6, exEn6)
a = append(a, b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package extract
func _() {
var b []int
var a int
//@mark(exSt7, "a")
b = fn0(a, b) //@mark(exEn7, ")")
b[0] = 1
//@extractfunc(exSt7, exEn7)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package extract
func _() {
var b []int
var a int
//@mark(exSt8, "a")
a, b = fn0(b) //@mark(exEn8, ")")
b[0] = 1
if a == 2 {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/testdata/summary.txt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FormatCount = 6
ImportCount = 8
SemanticTokenCount = 3
SuggestedFixCount = 40
FunctionExtractionCount = 17
FunctionExtractionCount = 18
DefinitionsCount = 95
TypeDefinitionsCount = 10
HighlightsCount = 69
Expand Down

0 comments on commit 16b25d2

Please sign in to comment.