internal/lsp: check ident exists for comment rename

The identifier in a reference is used to check for a doc comment.
Implicits do not have an ident, so do not use that to look for a doc
comment.

Also set the context.Context for the renamer.

Change-Id: I085d9e6c11d919222592dcb6fb30982eeb0fc7cd
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184042
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Suzy Mueller 2019-06-27 14:09:03 -04:00
parent 86796bd73f
commit 6cfa55603c
4 changed files with 281 additions and 4 deletions

View File

@ -60,6 +60,7 @@ func (i *IdentifierInfo) Rename(ctx context.Context, newName string) (map[span.U
}
r := renamer{
ctx: ctx,
fset: i.File.FileSet(),
pkg: i.pkg,
refs: refs,
@ -78,11 +79,11 @@ func (i *IdentifierInfo) Rename(ctx context.Context, newName string) (map[span.U
return nil, fmt.Errorf(r.errors)
}
return r.update(ctx)
return r.update()
}
// Rename all references to the identifier.
func (r *renamer) update(ctx context.Context) (map[span.URI][]TextEdit, error) {
func (r *renamer) update() (map[span.URI][]TextEdit, error) {
result := make(map[span.URI][]TextEdit)
docRegexp, err := regexp.Compile(`\b` + r.from + `\b`)
@ -99,9 +100,10 @@ func (r *renamer) update(ctx context.Context) (map[span.URI][]TextEdit, error) {
Span: refSpan,
NewText: r.to,
}
result[refSpan.URI()] = append(result[refSpan.URI()], edit)
if !ref.isDeclaration { // not a declaration
if !ref.isDeclaration || ref.ident == nil { // done if it it is a use or does not have an identifier
continue
}

View File

@ -1,6 +1,8 @@
-- GetSum-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
@ -23,9 +25,24 @@ func _() {
_ = p.GetSum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y) //@rename("y", "y1")
case string:
fmt.Printf("%s", y) //@rename("y", "y2")
default:
fmt.Printf("%v", y) //@rename("y", "y3")
}
}
-- myX-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
@ -48,9 +65,24 @@ func _() {
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y) //@rename("y", "y1")
case string:
fmt.Printf("%s", y) //@rename("y", "y2")
default:
fmt.Printf("%v", y) //@rename("y", "y3")
}
}
-- pos-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
@ -73,9 +105,24 @@ func _() {
_ = pos.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y) //@rename("y", "y1")
case string:
fmt.Printf("%s", y) //@rename("y", "y2")
default:
fmt.Printf("%v", y) //@rename("y", "y3")
}
}
-- z-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
@ -98,3 +145,216 @@ func _() {
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y) //@rename("y", "y1")
case string:
fmt.Printf("%s", y) //@rename("y", "y2")
default:
fmt.Printf("%v", y) //@rename("y", "y3")
}
}
-- y0-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
}
func Random2(y int) int { //@rename("y", "z")
return y
}
type Pos struct {
x, y int
}
func (p *Pos) Sum() int {
return p.x + p.y //@rename("x", "myX")
}
func _() {
var p Pos //@rename("p", "pos")
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y0 := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y0) //@rename("y", "y1")
case string:
fmt.Printf("%s", y0) //@rename("y", "y2")
default:
fmt.Printf("%v", y0) //@rename("y", "y3")
}
}
-- y1-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
}
func Random2(y int) int { //@rename("y", "z")
return y
}
type Pos struct {
x, y int
}
func (p *Pos) Sum() int {
return p.x + p.y //@rename("x", "myX")
}
func _() {
var p Pos //@rename("p", "pos")
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y1 := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y1) //@rename("y", "y1")
case string:
fmt.Printf("%s", y1) //@rename("y", "y2")
default:
fmt.Printf("%v", y1) //@rename("y", "y3")
}
}
-- y2-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
}
func Random2(y int) int { //@rename("y", "z")
return y
}
type Pos struct {
x, y int
}
func (p *Pos) Sum() int {
return p.x + p.y //@rename("x", "myX")
}
func _() {
var p Pos //@rename("p", "pos")
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y2 := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y2) //@rename("y", "y1")
case string:
fmt.Printf("%s", y2) //@rename("y", "y2")
default:
fmt.Printf("%v", y2) //@rename("y", "y3")
}
}
-- y3-rename --
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
}
func Random2(y int) int { //@rename("y", "z")
return y
}
type Pos struct {
x, y int
}
func (p *Pos) Sum() int {
return p.x + p.y //@rename("x", "myX")
}
func _() {
var p Pos //@rename("p", "pos")
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y3 := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y3) //@rename("y", "y1")
case string:
fmt.Printf("%s", y3) //@rename("y", "y2")
default:
fmt.Printf("%v", y3) //@rename("y", "y3")
}
}
-- format-rename --
package a
import format "fmt"
func Random() int {
y := 6 + 7
return y
}
func Random2(y int) int { //@rename("y", "z")
return y
}
type Pos struct {
x, y int
}
func (p *Pos) Sum() int {
return p.x + p.y //@rename("x", "myX")
}
func _() {
var p Pos //@rename("p", "pos")
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y := x.(type) { //@rename("y", "y0")
case int:
format.Printf("%d", y) //@rename("y", "y1")
case string:
format.Printf("%s", y) //@rename("y", "y2")
default:
format.Printf("%v", y) //@rename("y", "y3")
}
}

View File

@ -1,5 +1,7 @@
package a
import "fmt"
func Random() int {
y := 6 + 7
return y
@ -21,3 +23,16 @@ func _() {
var p Pos //@rename("p", "pos")
_ = p.Sum() //@rename("Sum", "GetSum")
}
func sw() {
var x interface{}
switch y := x.(type) { //@rename("y", "y0")
case int:
fmt.Printf("%d", y) //@rename("y", "y1")
case string:
fmt.Printf("%s", y) //@rename("y", "y2")
default:
fmt.Printf("%v", y) //@rename("y", "y3")
}
}

View File

@ -34,7 +34,7 @@ const (
ExpectedTypeDefinitionsCount = 2
ExpectedHighlightsCount = 2
ExpectedReferencesCount = 2
ExpectedRenamesCount = 4
ExpectedRenamesCount = 8
ExpectedSymbolsCount = 1
ExpectedSignaturesCount = 20
ExpectedLinksCount = 2