2019-09-16 15:17:59 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:17:51 -06:00
|
|
|
func (b *builtinPkg) Files() []source.ParseGoHandle {
|
|
|
|
return b.files
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:17:59 -06:00
|
|
|
// 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.
|
2019-10-02 12:45:55 -06:00
|
|
|
func (v *view) buildBuiltinPackage(ctx context.Context) error {
|
|
|
|
cfg := v.Config(ctx)
|
2019-09-16 15:17:59 -06:00
|
|
|
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 {
|
2019-10-02 12:45:55 -06:00
|
|
|
fh := v.session.GetFile(span.FileURI(filename), source.Go)
|
|
|
|
ph := v.session.cache.ParseGoHandle(fh, source.ParseFull)
|
|
|
|
v.builtin.files = append(v.builtin.files, ph)
|
2019-09-17 09:19:11 -06:00
|
|
|
file, _, _, err := ph.Parse(ctx)
|
|
|
|
if err != nil {
|
2019-09-16 15:17:59 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
files[filename] = file
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
v.ignoredURIsMu.Lock()
|
|
|
|
v.ignoredURIs[span.NewURI(filename)] = struct{}{}
|
|
|
|
v.ignoredURIsMu.Unlock()
|
2019-09-16 15:17:59 -06:00
|
|
|
}
|
2019-10-02 12:45:55 -06:00
|
|
|
v.builtin.pkg, err = ast.NewPackage(cfg.Fset, files, nil, nil)
|
2019-09-16 15:17:59 -06:00
|
|
|
return err
|
|
|
|
}
|