github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/cache/pkg.go (about) 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. 4 5 package cache 6 7 import ( 8 "go/ast" 9 "go/scanner" 10 "go/types" 11 12 "golang.org/x/mod/module" 13 "github.com/powerman/golang-tools/internal/lsp/source" 14 "github.com/powerman/golang-tools/internal/span" 15 errors "golang.org/x/xerrors" 16 ) 17 18 // pkg contains the type information needed by the source package. 19 type pkg struct { 20 m *Metadata 21 mode source.ParseMode 22 goFiles []*source.ParsedGoFile 23 compiledGoFiles []*source.ParsedGoFile 24 diagnostics []*source.Diagnostic 25 imports map[PackagePath]*pkg 26 version *module.Version 27 parseErrors []scanner.ErrorList 28 typeErrors []types.Error 29 types *types.Package 30 typesInfo *types.Info 31 typesSizes types.Sizes 32 hasFixedFiles bool // if true, AST was sufficiently mangled that we should hide type errors 33 } 34 35 // Declare explicit types for files and directories to distinguish between the two. 36 type ( 37 fileURI span.URI 38 moduleLoadScope string 39 viewLoadScope span.URI 40 ) 41 42 func (p *pkg) ID() string { 43 return string(p.m.ID) 44 } 45 46 func (p *pkg) Name() string { 47 return string(p.m.Name) 48 } 49 50 func (p *pkg) PkgPath() string { 51 return string(p.m.PkgPath) 52 } 53 54 func (p *pkg) ParseMode() source.ParseMode { 55 return p.mode 56 } 57 58 func (p *pkg) CompiledGoFiles() []*source.ParsedGoFile { 59 return p.compiledGoFiles 60 } 61 62 func (p *pkg) File(uri span.URI) (*source.ParsedGoFile, error) { 63 for _, cgf := range p.compiledGoFiles { 64 if cgf.URI == uri { 65 return cgf, nil 66 } 67 } 68 for _, gf := range p.goFiles { 69 if gf.URI == uri { 70 return gf, nil 71 } 72 } 73 return nil, errors.Errorf("no parsed file for %s in %v", uri, p.m.ID) 74 } 75 76 func (p *pkg) GetSyntax() []*ast.File { 77 var syntax []*ast.File 78 for _, pgf := range p.compiledGoFiles { 79 syntax = append(syntax, pgf.File) 80 } 81 return syntax 82 } 83 84 func (p *pkg) GetTypes() *types.Package { 85 return p.types 86 } 87 88 func (p *pkg) GetTypesInfo() *types.Info { 89 return p.typesInfo 90 } 91 92 func (p *pkg) GetTypesSizes() types.Sizes { 93 return p.typesSizes 94 } 95 96 func (p *pkg) IsIllTyped() bool { 97 return p.types == nil || p.typesInfo == nil || p.typesSizes == nil 98 } 99 100 func (p *pkg) ForTest() string { 101 return string(p.m.ForTest) 102 } 103 104 func (p *pkg) GetImport(pkgPath string) (source.Package, error) { 105 if imp := p.imports[PackagePath(pkgPath)]; imp != nil { 106 return imp, nil 107 } 108 // Don't return a nil pointer because that still satisfies the interface. 109 return nil, errors.Errorf("no imported package for %s", pkgPath) 110 } 111 112 func (p *pkg) MissingDependencies() []string { 113 // We don't invalidate metadata for import deletions, so check the package 114 // imports via the *types.Package. Only use metadata if p.types is nil. 115 if p.types == nil { 116 var md []string 117 for i := range p.m.MissingDeps { 118 md = append(md, string(i)) 119 } 120 return md 121 } 122 var md []string 123 for _, pkg := range p.types.Imports() { 124 if _, ok := p.m.MissingDeps[PackagePath(pkg.Path())]; ok { 125 md = append(md, pkg.Path()) 126 } 127 } 128 return md 129 } 130 131 func (p *pkg) Imports() []source.Package { 132 var result []source.Package 133 for _, imp := range p.imports { 134 result = append(result, imp) 135 } 136 return result 137 } 138 139 func (p *pkg) Version() *module.Version { 140 return p.version 141 } 142 143 func (p *pkg) HasListOrParseErrors() bool { 144 return len(p.m.Errors) != 0 || len(p.parseErrors) != 0 145 } 146 147 func (p *pkg) HasTypeErrors() bool { 148 return len(p.typeErrors) != 0 149 }