Skip to content

Commit

Permalink
fix:overload member reference
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed May 27, 2024
1 parent 5c0fff8 commit b578260
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
6 changes: 2 additions & 4 deletions gopls/internal/lsp/source/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,8 @@ func localReferences(pkg Package, targets map[types.Object]bool, correspond bool
if id, ok := n.(*gopast.Ident); ok {
if obj, ok := pkg.GopTypesInfo().Uses[id]; ok {
// goxls: use overload declaration to match
if overdecl, overloads := pkg.GopTypesInfo().OverloadOf(id); overdecl != nil && overloads != nil {
obj = overdecl
}
if matches(obj) {
overdecl, _ := pkg.GopTypesInfo().OverloadOf(id)
if matches(obj) || (overdecl != nil && matches(overdecl)) {
report(gopMustLocation(pgf, id), false)
}
}
Expand Down
40 changes: 40 additions & 0 deletions gopls/internal/regtest/misc/references_gox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ println mul(100, 7)
println mul("Hello", "World")
println mul(1.2, 3.14)
`
// goxls: overload decl reference
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("def.gop")
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func (mul) = \(`))
Expand All @@ -96,6 +97,26 @@ println mul(1.2, 3.14)
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
}
})
// goxls: overload member reference
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("def.gop")
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func mul = \(\n\s+(mulInt)`))
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:11\n" + // mulInt
"def.gop 9:4-9:10\n" + // overload mulInt
"test.gop 0:8-0:11\n" // use overload mulInt
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
}
})
}

func TestReferencesOnOverloadDecl3(t *testing.T) {
Expand Down Expand Up @@ -141,4 +162,23 @@ var c = a.mul(a)
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
}
})
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("def.gop")
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `\(foo\)\.(mulInt)`))
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 2:14-2:20\n" +
"def.gop 9:10-9:16\n" +
"test.gop 1:10-1:13\n"
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
}
})
}

0 comments on commit b578260

Please sign in to comment.