1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/mod/module"
12 "golang.org/x/tools/internal/lsp/source"
13 "golang.org/x/tools/internal/span"
14 errors "golang.org/x/xerrors"
17 // pkg contains the type information needed by the source package.
21 goFiles []*source.ParsedGoFile
22 compiledGoFiles []*source.ParsedGoFile
23 errors []*source.Error
24 imports map[packagePath]*pkg
25 version *module.Version
26 typeErrors []types.Error
29 typesSizes types.Sizes
32 // Declare explicit types for package paths, names, and IDs to ensure that we
33 // never use an ID where a path belongs, and vice versa. If we confused these,
34 // it would result in confusing errors because package IDs often look like
42 // Declare explicit types for files and directories to distinguish between the two.
45 moduleLoadScope string
46 viewLoadScope span.URI
49 func (p *pkg) ID() string {
53 func (p *pkg) Name() string {
54 return string(p.m.name)
57 func (p *pkg) PkgPath() string {
58 return string(p.m.pkgPath)
61 func (p *pkg) CompiledGoFiles() []*source.ParsedGoFile {
62 return p.compiledGoFiles
65 func (p *pkg) File(uri span.URI) (*source.ParsedGoFile, error) {
66 for _, cgf := range p.compiledGoFiles {
71 for _, gf := range p.goFiles {
76 return nil, errors.Errorf("no parsed file for %s in %v", uri, p.m.id)
79 func (p *pkg) GetSyntax() []*ast.File {
80 var syntax []*ast.File
81 for _, pgf := range p.compiledGoFiles {
82 syntax = append(syntax, pgf.File)
87 func (p *pkg) GetErrors() []*source.Error {
91 func (p *pkg) GetTypes() *types.Package {
95 func (p *pkg) GetTypesInfo() *types.Info {
99 func (p *pkg) GetTypesSizes() types.Sizes {
103 func (p *pkg) IsIllTyped() bool {
104 return p.types == nil || p.typesInfo == nil || p.typesSizes == nil
107 func (p *pkg) ForTest() string {
108 return string(p.m.forTest)
111 func (p *pkg) GetImport(pkgPath string) (source.Package, error) {
112 if imp := p.imports[packagePath(pkgPath)]; imp != nil {
115 // Don't return a nil pointer because that still satisfies the interface.
116 return nil, errors.Errorf("no imported package for %s", pkgPath)
119 func (p *pkg) MissingDependencies() []string {
120 // We don't invalidate metadata for import deletions, so check the package
121 // imports via the *types.Package. Only use metadata if p.types is nil.
124 for i := range p.m.missingDeps {
125 md = append(md, string(i))
130 for _, pkg := range p.types.Imports() {
131 if _, ok := p.m.missingDeps[packagePath(pkg.Path())]; ok {
132 md = append(md, pkg.Path())
138 func (p *pkg) Imports() []source.Package {
139 var result []source.Package
140 for _, imp := range p.imports {
141 result = append(result, imp)
146 func (p *pkg) Version() *module.Version {