Skip to content

Commit

Permalink
update:find overload definition cross package
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed May 27, 2024
1 parent 8245450 commit 7edc006
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
65 changes: 55 additions & 10 deletions gopls/internal/lsp/source/definition_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"fmt"
"go/types"
"log"
"strings"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
"golang.org/x/tools/gopls/internal/bug"
"golang.org/x/tools/gopls/internal/goxls"
"golang.org/x/tools/gopls/internal/goxls/parserutil"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/safetoken"
"golang.org/x/tools/gopls/internal/span"
"golang.org/x/tools/internal/event"
)

Expand Down Expand Up @@ -84,16 +87,11 @@ func GopDefinition(ctx context.Context, snapshot Snapshot, fh FileHandle, positi
return nil, nil
}

var anonyOvFunc *ast.FuncLit
if fun, ok := obj.(*types.Func); ok {
for ov := range pkg.GopTypesInfo().Implicits {
if v, ok := ov.(*ast.FuncLit); ok {
if v.Pos() == fun.Pos() {
anonyOvFunc = v
break
}
}
}
var anonyOvFunc *ast.FuncLit //goxls:overload anonymous member
if ovPkg, ovFuncLit, ovObj, ok := IsOverloadAnonymousMember(ctx, snapshot, pkg, obj); ok {
pkg = ovPkg
obj = ovObj
anonyOvFunc = ovFuncLit
}

if goxls.DbgDefinition {
Expand Down Expand Up @@ -299,3 +297,50 @@ func gopImportDefinition(ctx context.Context, s Snapshot, pkg Package, pgf *Pars

return locs, nil
}

// goxls:match in current package & variants
func IsOverloadAnonymousMember(ctx context.Context, snapshot Snapshot, pkg Package, obj types.Object) (Package, *ast.FuncLit, types.Object, bool) {
if _, ok := obj.(*types.Func); !ok {
return nil, nil, nil, false
}

declPosn := safetoken.StartPosition(pkg.FileSet(), obj.Pos())
declURI := span.URIFromPath(declPosn.Filename)

inPkg := func(searchPkg Package) (*ast.FuncLit, types.Object, bool) {
fset := searchPkg.FileSet()
for ov, om := range searchPkg.GopTypesInfo().Implicits {
if anonyOvFunc, ok := ov.(*ast.FuncLit); ok {
funPos := safetoken.StartPosition(fset, anonyOvFunc.Pos())
if declPosn.Offset == funPos.Offset {
return anonyOvFunc, om, true
}
}
}
return nil, nil, false
}

// goxls:match in current package
if funcLit, ovObj, ok := inPkg(pkg); ok {
return pkg, funcLit, ovObj, true
}

// goxls:match in variants package
if strings.HasSuffix(string(declURI), ".gop") {
variants, err := snapshot.MetadataForFile(ctx, declURI)
if err != nil {
return nil, nil, nil, false
}
for _, m := range variants {
varPkgs, err := snapshot.TypeCheck(ctx, m.ID)
if err != nil {
return nil, nil, nil, false
}
varPkg := varPkgs[0]
if funcLit, ovObj, ok := inPkg(varPkg); ok {
return varPkg, funcLit, ovObj, true
}
}
}
return nil, nil, nil, false
}
8 changes: 4 additions & 4 deletions gopls/internal/regtest/misc/definition_gox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const overloadDefinition1 = `
-- go.mod --
module mod.com
go 1.21.4
go 1.19
-- def.gop --
func add = (
func(a, b int) int {
Expand Down Expand Up @@ -43,7 +43,7 @@ const overloadDefinition2 = `
-- go.mod --
module mod.com
go 1.21.4
go 1.19
-- def.gop --
func mulInt(a, b int) int {
return a * b
Expand Down Expand Up @@ -80,7 +80,7 @@ const overloadDefinition3 = `
-- go.mod --
module mod.com
go 1.21.4
go 1.19
-- def.gop --
type foo struct {
}
Expand Down Expand Up @@ -121,7 +121,7 @@ const overloadDefinition4 = `
-- go.mod --
module mod.com
go 1.21.4
go 1.19
-- def.go --
package main
type foo struct {
Expand Down

0 comments on commit 7edc006

Please sign in to comment.