Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gopls/internal/goxls/parserutil: fix GetClassType #266

Open
wants to merge 1 commit into
base: goplus
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
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
9 changes: 8 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,13 @@ 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
Loading