Skip to content

Commit

Permalink
gop/packages: support load gop mod for add classfile
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Apr 18, 2024
1 parent 8d8a3aa commit 3305a3f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 68 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ module golang.org/x/tools
go 1.18 // tagx:compat 1.16

require (
github.com/goplus/gop v1.2.5
github.com/goplus/mod v0.13.9
github.com/goplus/gop v1.2.0-pre.1.0.20240418023908-721378627ab2
github.com/goplus/mod v0.13.10
github.com/yuin/goldmark v1.4.13
golang.org/x/mod v0.16.0
golang.org/x/mod v0.17.0
golang.org/x/net v0.22.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.18.0
)

require (
github.com/goplus/gogen v1.15.1 // indirect
github.com/qiniu/x v1.13.9 // indirect
github.com/goplus/gogen v1.15.2 // indirect
github.com/qiniu/x v1.13.10 // indirect
)
24 changes: 14 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
github.com/goplus/gogen v1.15.1 h1:iz/fFpOeldjwmnjLzEdNsZF2mCf+sOHJavbAvV3o7sY=
github.com/goplus/gogen v1.15.1/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gop v1.2.5 h1:kBCcvJ7ONt3IqTUTNGb+n9JO1nwTp5STg1UUSEfvz/k=
github.com/goplus/gop v1.2.5/go.mod h1:5rLlryvlZhWLIBw1Rok8s0uvyO54vcYw/7sFLhqtNTc=
github.com/goplus/mod v0.13.9 h1:B9zZoHi2AzMltTSOFqZNVjqGlSMlhhNTWwEzVqhTQzg=
github.com/goplus/mod v0.13.9/go.mod h1:MibsLSftGmxaQq78YzUzNviyFwB9RtpMaoscufvEKH4=
github.com/qiniu/x v1.13.9 h1:OUcQZSze1oTO5pzrhsepTUvEb9K9WtOiqZomWffFGWE=
github.com/qiniu/x v1.13.9/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/goplus/gogen v1.15.2 h1:Q6XaSx/Zi5tWnjfAziYsQI6Jv6MgODRpFtOYqNkiiqM=
github.com/goplus/gogen v1.15.2/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gop v1.2.0-pre.1.0.20240413142707-f07725aeb257 h1:Zb62NO0mHj7kEIRD4geBbplHB8UtQNZmXyXzLtNv+bA=
github.com/goplus/gop v1.2.0-pre.1.0.20240413142707-f07725aeb257/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/gop v1.2.0-pre.1.0.20240415141958-7e9fba5161f5 h1:H2lFjyjo1iduWFr2hT3v4rIuZTynQpQV6DrXGGc5jiQ=
github.com/goplus/gop v1.2.0-pre.1.0.20240415141958-7e9fba5161f5/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/gop v1.2.0-pre.1.0.20240418023908-721378627ab2 h1:pBj7YAn04gUVo9cJReg+sjiT44i6+SeTpvdXNxPUYOY=
github.com/goplus/gop v1.2.0-pre.1.0.20240418023908-721378627ab2/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE=
github.com/goplus/mod v0.13.10/go.mod h1:HDuPZgpWiaTp3PUolFgsiX+Q77cbUWB/mikVHfYND3c=
github.com/qiniu/x v1.13.10 h1:J4Z3XugYzAq85SlyAfqlKVrbf05glMbAOh+QncsDQpE=
github.com/qiniu/x v1.13.10/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -14,8 +18,8 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down
19 changes: 13 additions & 6 deletions gop/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ type Config = packages.Config
type Package struct {
packages.Package

// GopMod is the gop packages module or nil
GopMod *gopmod.Module

// GopFiles lists the absolute file paths of the package's Go source files.
// It may include files that should not be compiled, for example because
// they contain non-matching build tags, are documentary pseudo-files such as
Expand Down Expand Up @@ -392,25 +395,30 @@ func addGopFiles(ret *Package, ld *loader, dir string, mode LoadMode, test bool)
fsetTemp := token.NewFileSet()
pkgName := ret.Name
var mod *gopmod.Module
var once sync.Once
if ld != nil {
mod = ld.Context.LoadMod(ret.Module)
} else {
mod, _ = gop.LoadMod(dir)
}
ret.GopMod = mod

for _, e := range entries {
fname := e.Name()
if strings.HasPrefix(fname, "_") {
continue
}
fext := path.Ext(fname)
if goputil.FileKind(fext) == goputil.FileUnknown {
continue
if mod == nil || !mod.IsClass(fext) {
continue
}
}
if !test {
if strings.HasSuffix(fname[:len(fname)-len(fext)], "_test") {
continue
}
// check gox class test
if strings.HasSuffix(fname, "test.gox") {
once.Do(func() {
mod, _ = gop.LoadMod(dir)
})
if mod != nil {
if _, ok := mod.ClassKind(fname); ok {
continue
Expand All @@ -428,7 +436,6 @@ func addGopFiles(ret *Package, ld *loader, dir string, mode LoadMode, test bool)
}
if ld != nil && len(ret.CompiledGopFiles) > 0 {
ctx := ld.Context
mod := ctx.LoadMod(ret.Module)
ret.GopSyntax = ld.parseFiles(ret, mod, ret.CompiledGopFiles)
if mode&(NeedTypes|NeedTypesInfo) != 0 {
ret.GopTypesInfo = &typesutil.Info{
Expand Down
10 changes: 5 additions & 5 deletions gopls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.18

require (
github.com/google/go-cmp v0.6.0
github.com/goplus/gogen v1.15.1
github.com/goplus/gop v1.2.5
github.com/goplus/mod v0.13.9
github.com/goplus/gogen v1.15.2
github.com/goplus/gop v1.2.0-pre.1.0.20240418023908-721378627ab2
github.com/goplus/mod v0.13.10
github.com/jba/printsrc v0.2.2
github.com/jba/templatecheck v0.7.0
github.com/qiniu/x v1.13.9
github.com/qiniu/x v1.13.10
github.com/sergi/go-diff v1.1.0
golang.org/x/mod v0.16.0
golang.org/x/mod v0.17.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.18.0
golang.org/x/text v0.14.0
Expand Down
27 changes: 15 additions & 12 deletions gopls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/safehtml v0.1.0 h1:EwLKo8qawTKfsi0orxcQAZzu07cICaBeFMegAU9eaT8=
github.com/google/safehtml v0.1.0/go.mod h1:L4KWwDsUJdECRAEpZoBn3O64bQaywRscowZjJAzjHnU=
github.com/goplus/c2go v0.7.25/go.mod h1:e9oe4jDVhGFMJLEGmPSrVkLuXbLZAEmAu0/uD6fSz5E=
github.com/goplus/gogen v1.15.0/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gogen v1.15.1 h1:iz/fFpOeldjwmnjLzEdNsZF2mCf+sOHJavbAvV3o7sY=
github.com/goplus/gogen v1.15.1/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gop v1.2.5 h1:kBCcvJ7ONt3IqTUTNGb+n9JO1nwTp5STg1UUSEfvz/k=
github.com/goplus/gop v1.2.5/go.mod h1:5rLlryvlZhWLIBw1Rok8s0uvyO54vcYw/7sFLhqtNTc=
github.com/goplus/mod v0.13.9 h1:B9zZoHi2AzMltTSOFqZNVjqGlSMlhhNTWwEzVqhTQzg=
github.com/goplus/mod v0.13.9/go.mod h1:MibsLSftGmxaQq78YzUzNviyFwB9RtpMaoscufvEKH4=
github.com/goplus/c2go v0.7.26/go.mod h1:ePAStubV/ls8mmdPGQo6VfADTVd46rKuBemE4zzBDnA=
github.com/goplus/gogen v1.15.2 h1:Q6XaSx/Zi5tWnjfAziYsQI6Jv6MgODRpFtOYqNkiiqM=
github.com/goplus/gogen v1.15.2/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/gop v1.2.0-pre.1.0.20240413142707-f07725aeb257 h1:Zb62NO0mHj7kEIRD4geBbplHB8UtQNZmXyXzLtNv+bA=
github.com/goplus/gop v1.2.0-pre.1.0.20240413142707-f07725aeb257/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/gop v1.2.0-pre.1.0.20240415141958-7e9fba5161f5 h1:H2lFjyjo1iduWFr2hT3v4rIuZTynQpQV6DrXGGc5jiQ=
github.com/goplus/gop v1.2.0-pre.1.0.20240415141958-7e9fba5161f5/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/gop v1.2.0-pre.1.0.20240418023908-721378627ab2 h1:pBj7YAn04gUVo9cJReg+sjiT44i6+SeTpvdXNxPUYOY=
github.com/goplus/gop v1.2.0-pre.1.0.20240418023908-721378627ab2/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE=
github.com/goplus/mod v0.13.10/go.mod h1:HDuPZgpWiaTp3PUolFgsiX+Q77cbUWB/mikVHfYND3c=
github.com/jba/printsrc v0.2.2 h1:9OHK51UT+/iMAEBlQIIXW04qvKyF3/vvLuwW/hL8tDU=
github.com/jba/printsrc v0.2.2/go.mod h1:1xULjw59sL0dPdWpDoVU06TIEO/Wnfv6AHRpiElTwYM=
github.com/jba/templatecheck v0.7.0 h1:wjTb/VhGgSFeim5zjWVePBdaMo28X74bGLSABZV+zIA=
Expand All @@ -33,8 +36,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qiniu/x v1.13.9 h1:OUcQZSze1oTO5pzrhsepTUvEb9K9WtOiqZomWffFGWE=
github.com/qiniu/x v1.13.9/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/qiniu/x v1.13.10 h1:J4Z3XugYzAq85SlyAfqlKVrbf05glMbAOh+QncsDQpE=
github.com/qiniu/x v1.13.10/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand All @@ -50,8 +53,8 @@ golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2F
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/exp/typeparams v0.0.0-20221212164502-fae10dda9338/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
Expand Down
29 changes: 8 additions & 21 deletions gopls/internal/goxls/parserutil/parser_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
package parserutil

import (
"strings"

"github.com/goplus/gop"
"github.com/goplus/gop/ast"
"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/token"
"github.com/goplus/mod/gopmod"
Expand Down Expand Up @@ -50,25 +48,14 @@ func ParseFileEx(mod *gopmod.Module, fset *token.FileSet, filename string, src i
return
}

const (
casePrefix = "case"
)

func testNameSuffix(testType string) string {
if c := testType[0]; c >= 'A' && c <= 'Z' {
return testType
}
return "_" + testType
}

// GetClassType is get class type from ast.File and filename
func GetClassType(file *ast.File, filename string) (classType string, ok bool) {
if file.IsClass {
ok = true
classType, _, _ = cl.ClassNameAndExt(filename)
if strings.HasSuffix(filename, "test.gox") && !file.IsProj {
classType = casePrefix + testNameSuffix(classType)
func GetClassType(file *ast.File, filename string, getMod func() (*gopmod.Module, error)) (classType string, isTest bool, err error) {
var mod *gopmod.Module
if file.IsProj {
mod, err = getMod()
if err != nil {
return
}
}
classType, isTest = gop.GetFileClassType(mod, file, filename)
return
}
5 changes: 2 additions & 3 deletions gopls/internal/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,8 @@ func buildMetadata(updates map[PackageID]*source.Metadata, pkg *packages.Package
DepsErrors: packagesinternal.GetDepsErrors(pkg),
Standalone: standalone,
}
if len(pkg.GopFiles) > 0 {
m.LoadGopMod()
}

m.LoadGopMod(pkg.GopMod)

updates[id] = m

Expand Down
10 changes: 8 additions & 2 deletions gopls/internal/lsp/source/completion/completion_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/goplus/gop/scanner"
"github.com/goplus/gop/token"
"github.com/goplus/gop/x/typesutil"
"github.com/goplus/mod/gopmod"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/gop/ast/astutil"
"golang.org/x/tools/gopls/internal/goxls"
Expand Down Expand Up @@ -1431,7 +1432,12 @@ func (c *gopCompleter) lexical(ctx context.Context) error {
// position or embedded in interface declarations).
// builtinComparable = types.Universe.Lookup("comparable")
)
classType, isClass := parserutil.GetClassType(c.file, c.filename)
classType, _, err := parserutil.GetClassType(c.file, c.filename, func() (*gopmod.Module, error) {
return c.snapshot.GopModForFile(ctx, c.fh.URI())
})
if err != nil {
return fmt.Errorf("getting classType for gopCompleter: %w", err)
}
// Track seen variables to avoid showing completions for shadowed variables.
// This works since we look at scopes from innermost to outermost.
seen := make(map[string]struct{})
Expand All @@ -1446,7 +1452,7 @@ func (c *gopCompleter) lexical(ctx context.Context) error {
for _, name := range scope.Names() {
declScope, obj := scope.LookupParent(name, c.pos)
// Go+ class
if isClass && name == classType {
if c.file.IsClass && name == classType {
c.methodsAndFields(obj.Type(), true, nil, c.deepState.enqueue)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/source/signature_help_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func GopSignatureHelp(ctx context.Context, snapshot Snapshot, fh FileHandle, pos
}
var offset = int(pos - start)
npos := pos
if len(pgf.File.Code) >= offset {
if len(pgf.File.Code) > offset {
if pgf.File.Code[offset] == '\n' {
offset--
npos--
Expand Down
8 changes: 7 additions & 1 deletion gopls/internal/lsp/source/symbols_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
"github.com/goplus/gop/x/typesutil"
"github.com/goplus/mod/gopmod"
"golang.org/x/tools/gopls/internal/goxls/parserutil"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/internal/event"
Expand All @@ -25,7 +26,12 @@ func GopDocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) (
return nil, fmt.Errorf("getting file for GopDocumentSymbols: %w", err)
}
file := pgf.File
classType, _ := parserutil.GetClassType(file, fh.URI().Filename())
classType, _, err := parserutil.GetClassType(file, fh.URI().Filename(), func() (*gopmod.Module, error) {
return snapshot.GopModForFile(ctx, fh.URI())
})
if err != nil {
return nil, fmt.Errorf("getting classType for GopDocumentSymbols: %w", err)
}
// Build symbols for file declarations. When encountering a declaration with
// errors (typically because positions are invalid), we skip the declaration
// entirely. VS Code fails to show any symbols if one of the top-level
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/lsp/source/view_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func (pgf *ParsedGopFile) PosLocation(start, end token.Pos) (protocol.Location,
return pgf.Mapper.PosLocation(pgf.Tok, start, end)
}

func (m *Metadata) LoadGopMod() {
m.gopMod_, _ = gop.LoadMod(m.LoadDir)
func (m *Metadata) LoadGopMod(mod *gopmod.Module) {
m.gopMod_ = mod
}

func (m *Metadata) GopMod_() *gopmod.Module {
Expand Down

0 comments on commit 3305a3f

Please sign in to comment.