Skip to content

Commit

Permalink
update:support overload function reference
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Apr 29, 2024
1 parent ab79a37 commit c05339a
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
25 changes: 23 additions & 2 deletions gopls/internal/lsp/source/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,29 @@ func localReferences(pkg Package, targets map[types.Object]bool, correspond bool
for _, pgf := range pkg.CompiledGopFiles() {
gopast.Inspect(pgf.File, func(n gopast.Node) bool {
if id, ok := n.(*gopast.Ident); ok {
if obj, ok := pkg.GopTypesInfo().Uses[id]; ok && matches(obj) {
report(gopMustLocation(pgf, id), false)
if obj, ok := pkg.GopTypesInfo().Uses[id]; ok {
isOvObj := false
// file pos
idPos := pkg.FileSet().Position(id.Pos())
_ = idPos
//goxls: overload func obj use the origin overload obj
if _, ok := obj.(*types.Func); ok && pkg.GopTypesInfo() != nil {
for ovid, ov := range pkg.GopTypesInfo().Overloads {
for _, o := range ov {
if o == obj {
obj = pkg.GetTypes().Scope().Lookup(ovid.Name)
isOvObj = true
break
}
}
if isOvObj {
break
}
}
}
if matches(obj) {
report(gopMustLocation(pgf, id), false)
}
}
}
return true
Expand Down
100 changes: 100 additions & 0 deletions gopls/internal/regtest/misc/references_gox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package misc

import (
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
. "golang.org/x/tools/gopls/internal/lsp/regtest"
)

func TestReferencesOnOverloadDecl1(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- def.gop --
func add = (
func(a, b int) int {
return a + b
}
func(a, b string) string {
return a + b
}
)
-- test.gop --
println add(1,2)
println add("Hello", "World")
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("def.gop")
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `add`))
refs, err := env.Editor.References(env.Ctx, loc)
if err != nil {
t.Fatalf("references on (*s).Error failed: %v", err)
}
var buf strings.Builder
for _, ref := range refs {
fmt.Fprintf(&buf, "%s %s\n", env.Sandbox.Workdir.URIToPath(ref.URI), ref.Range)
}
got := buf.String()
want := "def.gop 0:5-0:8\n" + // overload decl
"test.gop 0:8-0:11\n" + // overload int call
"test.gop 1:8-1:11\n" // overload string call
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
}
})
}
func TestReferencesOnOverloadDecl2(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- def.gop --
func mulInt(a, b int) int {
return a * b
}
func mulFloat(a, b float64) float64 {
return a * b
}
func mul = (
mulInt
func(a, b string) string {
return a + b
}
mulFloat
)
-- test.gop --
println mul(100, 7)
println mul("Hello", "World")
println mul(1.2, 3.14)
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("def.gop")
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func (mul) = \(`))
refs, err := env.Editor.References(env.Ctx, loc)
if err != nil {
t.Fatalf("references on (*s).Error failed: %v", err)
}
var buf strings.Builder
for _, ref := range refs {
fmt.Fprintf(&buf, "%s %s\n", env.Sandbox.Workdir.URIToPath(ref.URI), ref.Range)
}
got := buf.String()
want := "def.gop 8:5-8:8\n" + // overload defintion
"def.gop 9:4-9:10\n" + // mutInt
"def.gop 13:4-13:12\n" + // mutFloat
"test.gop 0:8-0:11\n" + // overload int call
"test.gop 1:8-1:11\n" + // overload string call
"test.gop 2:8-2:11\n" // overload float call
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
}
})
}

0 comments on commit c05339a

Please sign in to comment.