Skip to content

Commit 6d9ecf2

Browse files
committed
gopls/internal/cache: rename methods on Package
Rename methods on cache.Package to eliminate unnecessary "Get" prefixes. Also rename pkg.go to package.go. Change-Id: I42f2fa7a18a5973a53af93c63b48c7db19cba3b2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/572475 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 29d17a0 commit 6d9ecf2

27 files changed

+152
-137
lines changed

gopls/internal/cache/pkg.go renamed to gopls/internal/cache/package.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type loadScope interface {
8686
aScope()
8787
}
8888

89+
// TODO(rfindley): move to load.go
8990
type (
9091
fileLoadScope protocol.DocumentURI // load packages containing a file (including command-line-arguments)
9192
packageLoadScope string // load a specific package (the value is its PackageID)
@@ -124,23 +125,33 @@ func (pkg *syntaxPackage) File(uri protocol.DocumentURI) (*parsego.File, error)
124125
return nil, fmt.Errorf("no parsed file for %s in %v", uri, pkg.id)
125126
}
126127

127-
func (p *Package) GetSyntax() []*ast.File {
128+
// Syntax returns parsed compiled Go files contained in this package.
129+
func (p *Package) Syntax() []*ast.File {
128130
var syntax []*ast.File
129131
for _, pgf := range p.pkg.compiledGoFiles {
130132
syntax = append(syntax, pgf.File)
131133
}
132134
return syntax
133135
}
134136

137+
// FileSet returns the FileSet describing this package's positions.
138+
//
139+
// The returned FileSet is guaranteed to describe all Syntax, but may also
140+
// describe additional files.
135141
func (p *Package) FileSet() *token.FileSet {
136142
return p.pkg.fset
137143
}
138144

139-
func (p *Package) GetTypes() *types.Package {
145+
// Types returns the type checked go/types.Package.
146+
func (p *Package) Types() *types.Package {
140147
return p.pkg.types
141148
}
142149

143-
func (p *Package) GetTypesInfo() *types.Info {
150+
// TypesInfo returns the go/types.Info annotating the Syntax of this package
151+
// with type information.
152+
//
153+
// All fields in the resulting Info are populated.
154+
func (p *Package) TypesInfo() *types.Info {
144155
return p.pkg.typesInfo
145156
}
146157

@@ -152,10 +163,14 @@ func (p *Package) DependencyTypes(path PackagePath) *types.Package {
152163
return p.pkg.importMap[path]
153164
}
154165

155-
func (p *Package) GetParseErrors() []scanner.ErrorList {
166+
// ParseErrors returns a slice containing all non-empty parse errors produces
167+
// while parsing p.Syntax, or nil if the package contains no parse errors.
168+
func (p *Package) ParseErrors() []scanner.ErrorList {
156169
return p.pkg.parseErrors
157170
}
158171

159-
func (p *Package) GetTypeErrors() []types.Error {
172+
// TypeErrors returns the go/types.Errors produced during type checking this
173+
// package, if any.
174+
func (p *Package) TypeErrors() []types.Error {
160175
return p.pkg.typeErrors
161176
}

gopls/internal/golang/change_signature.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
4545
if err != nil {
4646
return nil, err
4747
}
48-
if perrors, terrors := pkg.GetParseErrors(), pkg.GetTypeErrors(); len(perrors) > 0 || len(terrors) > 0 {
48+
if perrors, terrors := pkg.ParseErrors(), pkg.TypeErrors(); len(perrors) > 0 || len(terrors) > 0 {
4949
var sample string
5050
if len(perrors) > 0 {
5151
sample = perrors[0].Error()
@@ -367,7 +367,7 @@ func rewriteCalls(ctx context.Context, rw signatureRewrite) (map[protocol.Docume
367367
{
368368
delegate := internalastutil.CloneNode(rw.newDecl) // clone before modifying
369369
delegate.Name.Name = tag + delegate.Name.Name
370-
if obj := rw.pkg.GetTypes().Scope().Lookup(delegate.Name.Name); obj != nil {
370+
if obj := rw.pkg.Types().Scope().Lookup(delegate.Name.Name); obj != nil {
371371
return nil, fmt.Errorf("synthetic name %q conflicts with an existing declaration", delegate.Name.Name)
372372
}
373373

@@ -467,9 +467,9 @@ func reTypeCheck(logf func(string, ...any), orig *cache.Package, fileMask map[pr
467467
var importer func(importPath string) (*types.Package, error)
468468
{
469469
var (
470-
importsByPath = make(map[string]*types.Package) // cached imports
471-
toSearch = []*types.Package{orig.GetTypes()} // packages to search
472-
searched = make(map[string]bool) // path -> (false, if present in toSearch; true, if already searched)
470+
importsByPath = make(map[string]*types.Package) // cached imports
471+
toSearch = []*types.Package{orig.Types()} // packages to search
472+
searched = make(map[string]bool) // path -> (false, if present in toSearch; true, if already searched)
473473
)
474474
importer = func(path string) (*types.Package, error) {
475475
if p, ok := importsByPath[path]; ok {

gopls/internal/golang/code_lens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func matchTestFunc(fn *ast.FuncDecl, pkg *cache.Package, nameRe *regexp.Regexp,
135135
if !nameRe.MatchString(fn.Name.Name) {
136136
return false
137137
}
138-
info := pkg.GetTypesInfo()
138+
info := pkg.TypesInfo()
139139
if info == nil {
140140
return false
141141
}

gopls/internal/golang/codeaction.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func getRewriteCodeActions(ctx context.Context, pkg *cache.Package, snapshot *ca
335335
//
336336
// TODO: Consider removing the inspection after convenienceAnalyzers are removed.
337337
inspect := inspector.New([]*ast.File{pgf.File})
338-
for _, diag := range fillstruct.Diagnose(inspect, start, end, pkg.GetTypes(), pkg.GetTypesInfo()) {
338+
for _, diag := range fillstruct.Diagnose(inspect, start, end, pkg.Types(), pkg.TypesInfo()) {
339339
rng, err := pgf.Mapper.PosRange(pgf.Tok, diag.Pos, diag.End)
340340
if err != nil {
341341
return nil, err
@@ -354,7 +354,7 @@ func getRewriteCodeActions(ctx context.Context, pkg *cache.Package, snapshot *ca
354354
}
355355
}
356356

357-
for _, diag := range fillswitch.Diagnose(inspect, start, end, pkg.GetTypes(), pkg.GetTypesInfo()) {
357+
for _, diag := range fillswitch.Diagnose(inspect, start, end, pkg.Types(), pkg.TypesInfo()) {
358358
edits, err := suggestedFixToEdits(ctx, snapshot, pkg.FileSet(), &diag.SuggestedFixes[0])
359359
if err != nil {
360360
return nil, err
@@ -413,14 +413,14 @@ func canRemoveParameter(pkg *cache.Package, pgf *parsego.File, rng protocol.Rang
413413
return true // trivially unused
414414
}
415415

416-
obj := pkg.GetTypesInfo().Defs[info.Name]
416+
obj := pkg.TypesInfo().Defs[info.Name]
417417
if obj == nil {
418418
return false // something went wrong
419419
}
420420

421421
used := false
422422
ast.Inspect(info.Decl.Body, func(node ast.Node) bool {
423-
if n, ok := node.(*ast.Ident); ok && pkg.GetTypesInfo().Uses[n] == obj {
423+
if n, ok := node.(*ast.Ident); ok && pkg.TypesInfo().Uses[n] == obj {
424424
used = true
425425
}
426426
return !used // keep going until we find a use

gopls/internal/golang/completion/builtin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentI
8282
// For non-initial append() args, infer slice type from the first
8383
// append() arg, or from parent context.
8484
if len(call.Args) > 0 {
85-
inf.objType = c.pkg.GetTypesInfo().TypeOf(call.Args[0])
85+
inf.objType = c.pkg.TypesInfo().TypeOf(call.Args[0])
8686
}
8787
if inf.objType == nil {
8888
inf.objType = parentInf.objType
@@ -98,13 +98,13 @@ func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentI
9898

9999
// Penalize the first append() argument as a candidate. You
100100
// don't normally append a slice to itself.
101-
if sliceChain := objChain(c.pkg.GetTypesInfo(), call.Args[0]); len(sliceChain) > 0 {
101+
if sliceChain := objChain(c.pkg.TypesInfo(), call.Args[0]); len(sliceChain) > 0 {
102102
inf.penalized = append(inf.penalized, penalizedObj{objChain: sliceChain, penalty: 0.9})
103103
}
104104
case "delete":
105105
if exprIdx > 0 && len(call.Args) > 0 {
106106
// Try to fill in expected type of map key.
107-
firstArgType := c.pkg.GetTypesInfo().TypeOf(call.Args[0])
107+
firstArgType := c.pkg.TypesInfo().TypeOf(call.Args[0])
108108
if firstArgType != nil {
109109
if mt, ok := firstArgType.Underlying().(*types.Map); ok {
110110
inf.objType = mt.Key()
@@ -114,9 +114,9 @@ func (c *completer) builtinArgType(obj types.Object, call *ast.CallExpr, parentI
114114
case "copy":
115115
var t1, t2 types.Type
116116
if len(call.Args) > 0 {
117-
t1 = c.pkg.GetTypesInfo().TypeOf(call.Args[0])
117+
t1 = c.pkg.TypesInfo().TypeOf(call.Args[0])
118118
if len(call.Args) > 1 {
119-
t2 = c.pkg.GetTypesInfo().TypeOf(call.Args[1])
119+
t2 = c.pkg.TypesInfo().TypeOf(call.Args[1])
120120
}
121121
}
122122

0 commit comments

Comments
 (0)