Skip to content

Commit ccff732

Browse files
ShoshinNikitastamblerre
authored andcommitted
internal/lsp/source: fix comment update during rename for short variable declarations
*ast.AssignStmt doesn't have an associated comment group. So, we should try to find and return a comment just before the identifier. Fixes golang/go#42134 Change-Id: Ie40717a4973ccfdbd99c3df891c2cfffbb21742d GitHub-Last-Rev: da75fde GitHub-Pull-Request: #323 Reviewed-on: https://go-review.googlesource.com/c/tools/+/327229 Reviewed-by: Rebecca Stambler <[email protected]> Trust: Rebecca Stambler <[email protected]> Trust: Robert Findley <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent a7dfe3d commit ccff732

File tree

10 files changed

+119
-1
lines changed

10 files changed

+119
-1
lines changed

internal/lsp/source/rename.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,38 @@ func (r *renamer) docComment(pkg Package, id *ast.Ident) *ast.CommentGroup {
289289
return decl.Doc
290290
}
291291
case *ast.Ident:
292+
case *ast.AssignStmt:
293+
// *ast.AssignStmt doesn't have an associated comment group.
294+
// So, we try to find a comment just before the identifier.
295+
296+
// Try to find a comment group only for short variable declarations (:=).
297+
if decl.Tok != token.DEFINE {
298+
return nil
299+
}
300+
301+
var file *ast.File
302+
for _, f := range pkg.GetSyntax() {
303+
if f.Pos() <= id.Pos() && id.Pos() <= f.End() {
304+
file = f
305+
break
306+
}
307+
}
308+
if file == nil {
309+
return nil
310+
}
311+
312+
identLine := r.fset.Position(id.Pos()).Line
313+
for _, comment := range file.Comments {
314+
if comment.Pos() > id.Pos() {
315+
// Comment is after the identifier.
316+
continue
317+
}
318+
319+
lastCommentLine := r.fset.Position(comment.End()).Line
320+
if lastCommentLine+1 == identLine {
321+
return comment
322+
}
323+
}
292324
default:
293325
return nil
294326
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package issue42134
2+
3+
func _() {
4+
// foo computes things.
5+
foo := func() {}
6+
7+
foo() //@rename("foo", "bar")
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- bar-rename --
2+
package issue42134
3+
4+
func _() {
5+
// bar computes things.
6+
bar := func() {}
7+
8+
bar() //@rename("foo", "bar")
9+
}
10+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package issue42134
2+
3+
import "fmt"
4+
5+
func _() {
6+
// minNumber is a min number.
7+
// Second line.
8+
minNumber := min(1, 2)
9+
fmt.Println(minNumber) //@rename("minNumber", "res")
10+
}
11+
12+
func min(a, b int) int { return a }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- res-rename --
2+
package issue42134
3+
4+
import "fmt"
5+
6+
func _() {
7+
// res is a min number.
8+
// Second line.
9+
res := min(1, 2)
10+
fmt.Println(res) //@rename("minNumber", "res")
11+
}
12+
13+
func min(a, b int) int { return a }
14+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package issue42134
2+
3+
func _() {
4+
/*
5+
tests contains test cases
6+
*/
7+
tests := []struct { //@rename("tests", "testCases")
8+
in, out string
9+
}{}
10+
_ = tests
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- testCases-rename --
2+
package issue42134
3+
4+
func _() {
5+
/*
6+
testCases contains test cases
7+
*/
8+
testCases := []struct { //@rename("tests", "testCases")
9+
in, out string
10+
}{}
11+
_ = testCases
12+
}
13+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package issue42134
2+
3+
func _() {
4+
// a is equal to 5. Comment must stay the same
5+
6+
a := 5
7+
_ = a //@rename("a", "b")
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- b-rename --
2+
package issue42134
3+
4+
func _() {
5+
// a is equal to 5. Comment must stay the same
6+
7+
b := 5
8+
_ = b //@rename("a", "b")
9+
}
10+

internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ DefinitionsCount = 95
1919
TypeDefinitionsCount = 18
2020
HighlightsCount = 69
2121
ReferencesCount = 25
22-
RenamesCount = 33
22+
RenamesCount = 37
2323
PrepareRenamesCount = 7
2424
SymbolsCount = 5
2525
WorkspaceSymbolsCount = 20

0 commit comments

Comments
 (0)