1
0
mirror of https://github.com/golang/go synced 2024-11-19 01:34:40 -07:00
go/internal/lsp/cache/builtin.go
Rebecca Stambler fff8d94173 internal/lsp: use ParseGoHandles for the builtin package
This change allows to remove some of the special handling for the
builtin package.

Change-Id: I105fcefd8812af2d42ff42edca954824c98db429
Reviewed-on: https://go-review.googlesource.com/c/tools/+/195758
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-09-16 23:04:25 +00:00

55 lines
1.3 KiB
Go

package cache
import (
"context"
"go/ast"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
type builtinPkg struct {
pkg *ast.Package
files []source.ParseGoHandle
}
func (b *builtinPkg) Lookup(name string) *ast.Object {
if b == nil || b.pkg == nil || b.pkg.Scope == nil {
return nil
}
return b.pkg.Scope.Lookup(name)
}
// buildBuiltinPkg builds the view's builtin package.
// It assumes that the view is not active yet,
// i.e. it has not been added to the session's list of views.
func (view *view) buildBuiltinPackage(ctx context.Context) error {
cfg := view.Config(ctx)
pkgs, err := packages.Load(cfg, "builtin")
if err != nil {
return err
}
if len(pkgs) != 1 {
return err
}
pkg := pkgs[0]
files := make(map[string]*ast.File)
for _, filename := range pkg.GoFiles {
fh := view.session.GetFile(span.FileURI(filename))
ph := view.session.cache.ParseGoHandle(fh, source.ParseFull)
view.builtin.files = append(view.builtin.files, ph)
file, _, err := ph.Parse(ctx)
if file == nil {
return err
}
files[filename] = file
view.ignoredURIsMu.Lock()
view.ignoredURIs[span.NewURI(filename)] = struct{}{}
view.ignoredURIsMu.Unlock()
}
view.builtin.pkg, err = ast.NewPackage(cfg.Fset, files, nil, nil)
return err
}