Skip to content

Commit 0809ec2

Browse files
committed
gopls/internal/lsp/source: document {All,Workspace}Metadata
This change is the residue of a failed attempt to reduce the scope of the Implementations query (and others) based on a misunderstanding. It merely improves the documentation around these two concepts. What is the German word for a lengthy effort that ends in nothing more than a small amount of wistful documentation? Also, return errors correctly in InlayHint, which I touched along the way. Change-Id: I5023d0abffed8d0d9cfd8a18668ba859e8fabbc2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/493625 Run-TryBot: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 8f7fb01 commit 0809ec2

File tree

7 files changed

+46
-30
lines changed

7 files changed

+46
-30
lines changed

gopls/internal/lsp/mod/inlayhint.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,46 @@ func InlayHint(ctx context.Context, snapshot source.Snapshot, fh source.FileHand
1818
if err != nil {
1919
return nil, err
2020
}
21-
return unexpectedVersion(ctx, snapshot, pm), nil
22-
}
2321

24-
// Compare the version of the module used in the snapshot's metadata with the
25-
// version requested by the module, in both cases, taking replaces into account.
26-
// Produce an InlayHint when the version is the module is not the one usedd.
27-
func unexpectedVersion(ctx context.Context, snapshot source.Snapshot, pm *source.ParsedModule) []protocol.InlayHint {
28-
var ans []protocol.InlayHint
29-
if pm.File == nil {
30-
return nil
31-
}
22+
// Compare the version of the module used in the snapshot's metadata with the
23+
// version requested by the module, in both cases, taking replaces into account.
24+
// Produce an InlayHint when the version is the module is not the one used.
25+
3226
replaces := make(map[string]*modfile.Replace)
33-
requires := make(map[string]*modfile.Require)
3427
for _, x := range pm.File.Replace {
3528
replaces[x.Old.Path] = x
3629
}
30+
31+
requires := make(map[string]*modfile.Require)
3732
for _, x := range pm.File.Require {
3833
requires[x.Mod.Path] = x
3934
}
40-
am, _ := snapshot.AllMetadata(ctx)
35+
36+
am, err := snapshot.AllMetadata(ctx)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
var ans []protocol.InlayHint
4142
seen := make(map[string]bool)
4243
for _, meta := range am {
43-
if meta == nil || meta.Module == nil || seen[meta.Module.Path] {
44+
if meta.Module == nil || seen[meta.Module.Path] {
4445
continue
4546
}
4647
seen[meta.Module.Path] = true
47-
metaMod := meta.Module
48-
metaVersion := metaMod.Version
49-
if metaMod.Replace != nil {
50-
metaVersion = metaMod.Replace.Version
48+
metaVersion := meta.Module.Version
49+
if meta.Module.Replace != nil {
50+
metaVersion = meta.Module.Replace.Version
5151
}
5252
// These versions can be blank, as in gopls/go.mod's local replace
53-
if oldrepl, ok := replaces[metaMod.Path]; ok && oldrepl.New.Version != metaVersion {
53+
if oldrepl, ok := replaces[meta.Module.Path]; ok && oldrepl.New.Version != metaVersion {
5454
ih := genHint(oldrepl.Syntax, oldrepl.New.Version, metaVersion, pm.Mapper)
5555
if ih != nil {
5656
ans = append(ans, *ih)
5757
}
58-
} else if oldreq, ok := requires[metaMod.Path]; ok && oldreq.Mod.Version != metaVersion {
58+
} else if oldreq, ok := requires[meta.Module.Path]; ok && oldreq.Mod.Version != metaVersion {
5959
// maybe it was replaced:
60-
if _, ok := replaces[metaMod.Path]; ok {
60+
if _, ok := replaces[meta.Module.Path]; ok {
6161
continue
6262
}
6363
ih := genHint(oldreq.Syntax, oldreq.Mod.Version, metaVersion, pm.Mapper)
@@ -66,7 +66,7 @@ func unexpectedVersion(ctx context.Context, snapshot source.Snapshot, pm *source
6666
}
6767
}
6868
}
69-
return ans
69+
return ans, nil
7070
}
7171

7272
func genHint(mline *modfile.Line, oldVersion, newVersion string, m *protocol.Mapper) *protocol.InlayHint {

gopls/internal/lsp/source/completion/completion.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,8 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
11741174
// not assume global Pos/Object realms and then use export
11751175
// data instead of the quick parse approach taken here.
11761176

1177-
// First, we search among packages in the workspace.
1177+
// First, we search among packages in the forward transitive
1178+
// closure of the workspace.
11781179
// We'll use a fast parse to extract package members
11791180
// from those that match the name/path criterion.
11801181
all, err := c.snapshot.AllMetadata(ctx)
@@ -1610,7 +1611,7 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
16101611

16111612
count := 0
16121613

1613-
// Search packages across the entire workspace.
1614+
// Search the forward transitive closure of the workspace.
16141615
all, err := c.snapshot.AllMetadata(ctx)
16151616
if err != nil {
16161617
return err

gopls/internal/lsp/source/implementation.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ func implementations(ctx context.Context, snapshot Snapshot, fh FileHandle, pp p
152152
return nil, nil
153153
}
154154

155-
// The global search needs to look at every package in the workspace;
156-
// see package ./methodsets.
155+
// The global search needs to look at every package in the
156+
// forward transitive closure of the workspace; see package
157+
// ./methodsets.
157158
//
158159
// For now we do all the type checking before beginning the search.
159160
// TODO(adonovan): opt: search in parallel topological order

gopls/internal/lsp/source/known_packages.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func KnownPackagePaths(ctx context.Context, snapshot Snapshot, fh FileHandle) ([
5151
}
5252
}
5353

54-
// Now find candidates among known packages.
54+
// Now find candidates among all known packages.
5555
knownPkgs, err := snapshot.AllMetadata(ctx)
5656
if err != nil {
5757
return nil, err

gopls/internal/lsp/source/linkname.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func findLinknameAtOffset(pgf *ParsedGoFile, offset int) (string, int) {
117117
func findLinkname(ctx context.Context, snapshot Snapshot, pkgPath PackagePath, name string) (Package, *ParsedGoFile, token.Pos, error) {
118118
// Typically the linkname refers to a forward dependency
119119
// or a reverse dependency, but in general it may refer
120-
// to any package in the workspace.
120+
// to any package that is linked with this one.
121121
var pkgMeta *Metadata
122122
metas, err := snapshot.AllMetadata(ctx)
123123
if err != nil {

gopls/internal/lsp/source/rename.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ func renamePackageName(ctx context.Context, s Snapshot, f FileHandle, newName Pa
718718
// directory.
719719
//
720720
// It updates package clauses and import paths for the renamed package as well
721-
// as any other packages affected by the directory renaming among packages
722-
// described by allMetadata.
721+
// as any other packages affected by the directory renaming among all packages
722+
// known to the snapshot.
723723
func renamePackage(ctx context.Context, s Snapshot, f FileHandle, newName PackageName) (map[span.URI][]diff.Edit, error) {
724724
if strings.HasSuffix(string(newName), "_test") {
725725
return nil, fmt.Errorf("cannot rename to _test package")

gopls/internal/lsp/source/view.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,23 @@ type Snapshot interface {
175175
// WorkspaceMetadata returns a new, unordered slice containing
176176
// metadata for all ordinary and test packages (but not
177177
// intermediate test variants) in the workspace.
178+
//
179+
// The workspace is the set of modules typically defined by a
180+
// go.work file. It is not transitively closed: for example,
181+
// the standard library is not usually part of the workspace
182+
// even though every module in the workspace depends on it.
183+
//
184+
// Operations that must inspect all the dependencies of the
185+
// workspace packages should instead use AllMetadata.
178186
WorkspaceMetadata(ctx context.Context) ([]*Metadata, error)
179187

180-
// AllMetadata returns a new unordered array of metadata for all packages in the workspace.
188+
// AllMetadata returns a new unordered array of metadata for
189+
// all packages known to this snapshot, which includes the
190+
// packages of all workspace modules plus their transitive
191+
// import dependencies.
192+
//
193+
// It may also contain ad-hoc packages for standalone files.
194+
// It includes all test variants.
181195
AllMetadata(ctx context.Context) ([]*Metadata, error)
182196

183197
// Metadata returns the metadata for the specified package,

0 commit comments

Comments
 (0)