Skip to content

Commit

Permalink
update:use modified info.Overload
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Apr 30, 2024
1 parent a8fd6b3 commit 11c9d16
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 47 deletions.
2 changes: 1 addition & 1 deletion gop/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func addGopFiles(ret *Package, ld *loader, dir string, mode LoadMode, test bool)
Scopes: make(map[ast.Node]*types.Scope),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Instances: make(map[*ast.Ident]types.Instance),
Overloads: make(map[*ast.Ident][]types.Object),
Overloads: make(map[*ast.Ident]types.Object),
}
cfg := &types.Config{
Context: ctx.Types,
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cache/check_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newGopTypeInfo() *typesutil.Info {
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
Instances: make(map[*ast.Ident]types.Instance),
Overloads: make(map[*ast.Ident][]types.Object),
Overloads: make(map[*ast.Ident]types.Object),
}
}

Expand Down
21 changes: 3 additions & 18 deletions gopls/internal/lsp/source/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,24 +621,9 @@ func localReferences(pkg Package, targets map[types.Object]bool, correspond bool
gopast.Inspect(pgf.File, func(n gopast.Node) bool {
if id, ok := n.(*gopast.Ident); ok {
if obj, ok := pkg.GopTypesInfo().Uses[id]; ok {
isOvObj := false
//goxls: overload func obj use the origin overload obj
if f, ok := obj.(*types.Func); ok && pkg.GopTypesInfo() != nil {
for ovid, ov := range pkg.GopTypesInfo().Overloads {
for _, o := range ov {
if equalOrigin(o, obj) { // obj is overload
if f.Type().(*types.Signature).Recv() != nil {
} else {
obj = pkg.GetTypes().Scope().Lookup(ovid.Name)
}
isOvObj = true
break
}
}
if isOvObj {
break
}
}
overdecl, overloads := pkg.GopTypesInfo().OverloadOf(id)
if overdecl != nil && overloads != nil { // goxls: use overload declaration to match
obj = overdecl
}
if matches(obj) {
report(gopMustLocation(pgf, id), false)
Expand Down
10 changes: 5 additions & 5 deletions gopls/internal/lsp/source/signature_help_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ FindCall:

activeParam := gopActiveParameter(callExpr, sig.Params().Len(), sig.Variadic(), pos)

objs, overloads := pkg.GopTypesInfo().Overloads[ident]
overdecl, overloads := pkg.GopTypesInfo().OverloadOf(ident)

var (
name string
comment *ast.CommentGroup
)
if obj != nil {
d, err := HoverDocForObject(ctx, snapshot, pkg.FileSet(), obj)
if err != nil && !overloads {
if err != nil && overloads == nil {
return nil, 0, 0, err
}
name = obj.Name()
Expand All @@ -172,10 +172,10 @@ FindCall:
}, nil
}

if overloads {
if overdecl != nil && overloads != nil {
activeSignature := 0
infos := make([]protocol.SignatureInformation, len(objs))
for i, o := range objs {
infos := make([]protocol.SignatureInformation, len(overloads))
for i, o := range overloads {
if o.Name() == obj.Name() {
activeSignature = i
}
Expand Down
42 changes: 20 additions & 22 deletions gopls/internal/regtest/misc/references_gox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ println mul(1.2, 3.14)
}
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
Expand All @@ -100,7 +98,7 @@ println mul(1.2, 3.14)
}

func TestReferencesOnOverloadDecl3(t *testing.T) {
const files = `
const files = `
-- go.mod --
module mod.com
Expand All @@ -122,22 +120,22 @@ func (foo).mul = (
var a *foo
var c = a.mul(100)
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("def.gop")
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func \(foo\)\.(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:11-8:14\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)
}
})
}
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("def.gop")
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func \(foo\)\.(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:11-8:14\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 11c9d16

Please sign in to comment.