| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/ast" |
| 10 |
"go/scanner" |
| 11 |
"go/token" |
| 12 |
"go/types" |
| 13 |
"slices" |
| 14 |
"sync" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 17 |
"golang.org/x/tools/gopls/internal/cache/methodsets" |
| 18 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 19 |
"golang.org/x/tools/gopls/internal/cache/testfuncs" |
| 20 |
"golang.org/x/tools/gopls/internal/cache/xrefs" |
| 21 |
"golang.org/x/tools/gopls/internal/protocol" |
| 22 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
type ( |
| 27 |
PackageID = metadata.PackageID |
| 28 |
PackagePath = metadata.PackagePath |
| 29 |
PackageName = metadata.PackageName |
| 30 |
ImportPath = metadata.ImportPath |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type Package struct { |
| 39 |
metadata *metadata.Package |
| 40 |
loadDiagnostics []*Diagnostic |
| 41 |
pkg *syntaxPackage |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
type syntaxPackage struct { |
| 46 |
|
| 47 |
id PackageID |
| 48 |
|
| 49 |
|
| 50 |
fset *token.FileSet |
| 51 |
goFiles []*parsego.File |
| 52 |
compiledGoFiles []*parsego.File |
| 53 |
diagnostics []*Diagnostic |
| 54 |
parseErrors []scanner.ErrorList |
| 55 |
typeErrors []types.Error |
| 56 |
types *types.Package |
| 57 |
typesInfo *types.Info |
| 58 |
typesSizes types.Sizes |
| 59 |
importMap map[PackagePath]*types.Package |
| 60 |
|
| 61 |
xrefsOnce sync.Once |
| 62 |
_xrefs *xrefs.Index |
| 63 |
|
| 64 |
methodsetsOnce sync.Once |
| 65 |
_methodsets *methodsets.Index |
| 66 |
|
| 67 |
testsOnce sync.Once |
| 68 |
_tests *testfuncs.Index |
| 69 |
} |
| 70 |
|
| 71 |
func (p *syntaxPackage) xrefs() *xrefs.Index { |
| 72 |
p.xrefsOnce.Do(func() { |
| 73 |
p._xrefs = xrefs.NewIndex(p.compiledGoFiles, p.types, p.typesInfo) |
| 74 |
}) |
| 75 |
return p._xrefs |
| 76 |
} |
| 77 |
|
| 78 |
func (p *syntaxPackage) methodsets() *methodsets.Index { |
| 79 |
p.methodsetsOnce.Do(func() { |
| 80 |
p._methodsets = methodsets.NewIndex(p.fset, p.types) |
| 81 |
}) |
| 82 |
return p._methodsets |
| 83 |
} |
| 84 |
|
| 85 |
func (p *syntaxPackage) tests() *testfuncs.Index { |
| 86 |
p.testsOnce.Do(func() { |
| 87 |
p._tests = testfuncs.NewIndex(p.compiledGoFiles, p.typesInfo) |
| 88 |
}) |
| 89 |
return p._tests |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
func (p *syntaxPackage) hasFixedFiles() bool { |
| 97 |
return slices.ContainsFunc(p.compiledGoFiles, (*parsego.File).Fixed) |
| 98 |
} |
| 99 |
|
| 100 |
func (p *Package) String() string { return string(p.metadata.ID) } |
| 101 |
|
| 102 |
func (p *Package) Metadata() *metadata.Package { return p.metadata } |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
type loadScope interface { |
| 108 |
aScope() |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
type ( |
| 113 |
fileLoadScope protocol.DocumentURI |
| 114 |
packageLoadScope string |
| 115 |
moduleLoadScope struct { |
| 116 |
dir string |
| 117 |
modulePath string |
| 118 |
} |
| 119 |
viewLoadScope struct{} |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
func (fileLoadScope) aScope() {} |
| 124 |
func (packageLoadScope) aScope() {} |
| 125 |
func (moduleLoadScope) aScope() {} |
| 126 |
func (viewLoadScope) aScope() {} |
| 127 |
|
| 128 |
func (p *Package) CompiledGoFiles() []*parsego.File { |
| 129 |
return p.pkg.compiledGoFiles |
| 130 |
} |
| 131 |
|
| 132 |
func (p *Package) File(uri protocol.DocumentURI) (*parsego.File, error) { |
| 133 |
return p.pkg.File(uri) |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
func (p *Package) FileEnclosing(pos token.Pos) (*parsego.File, error) { |
| 139 |
for _, files := range [...][]*parsego.File{p.pkg.compiledGoFiles, p.pkg.goFiles} { |
| 140 |
for _, pgf := range files { |
| 141 |
if pgf.File.FileStart <= pos && pos <= pgf.File.FileEnd { |
| 142 |
return pgf, nil |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
return nil, fmt.Errorf("no parsed file for position %d (%s) in %v", |
| 147 |
pos, safetoken.StartPosition(p.FileSet(), pos), p.pkg.id) |
| 148 |
} |
| 149 |
|
| 150 |
func (pkg *syntaxPackage) File(uri protocol.DocumentURI) (*parsego.File, error) { |
| 151 |
for _, files := range [...][]*parsego.File{pkg.compiledGoFiles, pkg.goFiles} { |
| 152 |
for _, pgf := range files { |
| 153 |
if pgf.URI == uri { |
| 154 |
return pgf, nil |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
return nil, fmt.Errorf("no parsed file for %s in %v", uri, pkg.id) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func (p *Package) Syntax() []*ast.File { |
| 163 |
var syntax []*ast.File |
| 164 |
for _, pgf := range p.pkg.compiledGoFiles { |
| 165 |
syntax = append(syntax, pgf.File) |
| 166 |
} |
| 167 |
return syntax |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
func (p *Package) FileSet() *token.FileSet { |
| 175 |
return p.pkg.fset |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
func (p *Package) Types() *types.Package { |
| 180 |
return p.pkg.types |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
func (p *Package) TypesInfo() *types.Info { |
| 188 |
return p.pkg.typesInfo |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
func (p *Package) TypesSizes() types.Sizes { |
| 193 |
return p.pkg.typesSizes |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
func (p *Package) DependencyTypes(path PackagePath) *types.Package { |
| 201 |
return p.pkg.importMap[path] |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
func (p *Package) ParseErrors() []scanner.ErrorList { |
| 207 |
return p.pkg.parseErrors |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
func (p *Package) TypeErrors() []types.Error { |
| 213 |
return p.pkg.typeErrors |
| 214 |
} |