github.com/april1989/origin-go-tools@v0.0.32/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/types" 10 11 "github.com/april1989/origin-go-tools/go/packages" 12 "github.com/april1989/origin-go-tools/internal/lsp/source" 13 "github.com/april1989/origin-go-tools/internal/span" 14 errors "golang.org/x/xerrors" 15 ) 16 17 // pkg contains the type information needed by the source package. 18 type pkg struct { 19 m *metadata 20 mode source.ParseMode 21 goFiles []*source.ParsedGoFile 22 compiledGoFiles []*source.ParsedGoFile 23 errors []*source.Error 24 imports map[packagePath]*pkg 25 module *packages.Module 26 typeErrors []types.Error 27 types *types.Package 28 typesInfo *types.Info 29 typesSizes types.Sizes 30 } 31 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 35 // package paths. 36 type ( 37 packageID string 38 packagePath string 39 packageName string 40 ) 41 42 // Declare explicit types for files and directories to distinguish between the two. 43 type ( 44 fileURI span.URI 45 directoryURI span.URI 46 viewLoadScope span.URI 47 ) 48 49 func (p *pkg) ID() string { 50 return string(p.m.id) 51 } 52 53 func (p *pkg) Name() string { 54 return string(p.m.name) 55 } 56 57 func (p *pkg) PkgPath() string { 58 return string(p.m.pkgPath) 59 } 60 61 func (p *pkg) CompiledGoFiles() []*source.ParsedGoFile { 62 return p.compiledGoFiles 63 } 64 65 func (p *pkg) File(uri span.URI) (*source.ParsedGoFile, error) { 66 for _, cgf := range p.compiledGoFiles { 67 if cgf.URI == uri { 68 return cgf, nil 69 } 70 } 71 for _, gf := range p.goFiles { 72 if gf.URI == uri { 73 return gf, nil 74 } 75 } 76 return nil, errors.Errorf("no parsed file for %s in %v", uri, p.m.id) 77 } 78 79 func (p *pkg) GetSyntax() []*ast.File { 80 var syntax []*ast.File 81 for _, pgf := range p.compiledGoFiles { 82 syntax = append(syntax, pgf.File) 83 } 84 return syntax 85 } 86 87 func (p *pkg) GetErrors() []*source.Error { 88 return p.errors 89 } 90 91 func (p *pkg) GetTypes() *types.Package { 92 return p.types 93 } 94 95 func (p *pkg) GetTypesInfo() *types.Info { 96 return p.typesInfo 97 } 98 99 func (p *pkg) GetTypesSizes() types.Sizes { 100 return p.typesSizes 101 } 102 103 func (p *pkg) IsIllTyped() bool { 104 return p.types == nil || p.typesInfo == nil || p.typesSizes == nil 105 } 106 107 func (p *pkg) ForTest() string { 108 return string(p.m.forTest) 109 } 110 111 func (p *pkg) GetImport(pkgPath string) (source.Package, error) { 112 if imp := p.imports[packagePath(pkgPath)]; imp != nil { 113 return imp, nil 114 } 115 // Don't return a nil pointer because that still satisfies the interface. 116 return nil, errors.Errorf("no imported package for %s", pkgPath) 117 } 118 119 func (p *pkg) MissingDependencies() []string { 120 var md []string 121 for i := range p.m.missingDeps { 122 md = append(md, string(i)) 123 } 124 return md 125 } 126 127 func (p *pkg) Imports() []source.Package { 128 var result []source.Package 129 for _, imp := range p.imports { 130 result = append(result, imp) 131 } 132 return result 133 } 134 135 func (p *pkg) Module() *packages.Module { 136 return p.module 137 }