Debian Codegraph @

gopls/internal/golang/snapshot.go

raw symbols SCIP
1 // Copyright 2018 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 golang
6
7 import (
8 "context"
9 "fmt"
10 "go/token"
11 "go/types"
12
13 "golang.org/x/tools/gopls/internal/cache"
14 "golang.org/x/tools/gopls/internal/cache/metadata"
15 "golang.org/x/tools/gopls/internal/cache/parsego"
16 "golang.org/x/tools/gopls/internal/protocol"
17 "golang.org/x/tools/gopls/internal/util/safetoken"
18 )
19
20 // NarrowestPackageForFile is a convenience function that selects the narrowest
21 // non-ITV package to which this file belongs, type-checks it in the requested
22 // mode (full or workspace), and returns it, along with the parse tree of that
23 // file.
24 //
25 // The "narrowest" package is the one with the fewest number of files that
26 // includes the given file. This solves the problem of test variants, as the
27 // test will have more files than the non-test package.
28 //
29 // An intermediate test variant (ITV) package has identical source to a regular
30 // package but resolves imports differently. gopls should never need to
31 // type-check them.
32 //
33 // Type-checking is expensive. Call snapshot.ParseGo if all you need is a parse
34 // tree, or snapshot.MetadataForFile if you only need metadata.
35 func NarrowestPackageForFile(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (*cache.Package, *parsego.File, error) {
36 return selectPackageForFile(ctx, snapshot, uri, func(metas []*metadata.Package) *metadata.Package { return metas[0] })
37 }
38
39 // WidestPackageForFile is a convenience function that selects the widest
40 // non-ITV package to which this file belongs, type-checks it in the requested
41 // mode (full or workspace), and returns it, along with the parse tree of that
42 // file.
43 //
44 // The "widest" package is the one with the most number of files that includes
45 // the given file. Which is the test variant if one exists.
46 //
47 // An intermediate test variant (ITV) package has identical source to a regular
48 // package but resolves imports differently. gopls should never need to
49 // type-check them.
50 //
51 // Type-checking is expensive. Call snapshot.ParseGo if all you need is a parse
52 // tree, or snapshot.MetadataForFile if you only need metadata.
53 func WidestPackageForFile(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (*cache.Package, *parsego.File, error) {
54 return selectPackageForFile(ctx, snapshot, uri, func(metas []*metadata.Package) *metadata.Package { return metas[len(metas)-1] })
55 }
56
57 // NarrowestDeclaringPackage facilitates cross-package analysis by "jumping"
58 // from the current package context to the package that declares the given object.
59 //
60 // This is essential when the object of interest is defined in a different
61 // package than the one currently being type-checked.
62 //
63 // This function performs the context switch: it locates the file declaring
64 // the object, loads its narrowest package (see [NarrowestPackageForFile]),
65 // and returns the type-checked result.
66 //
67 // It returns the new package, the file, and the object's position translated
68 // to be valid within the new package's file set.
69 func NarrowestDeclaringPackage(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, obj types.Object) (*cache.Package, *parsego.File, token.Pos, error) {
70 posn := safetoken.StartPosition(pkg.FileSet(), obj.Pos())
71 pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, protocol.URIFromPath(posn.Filename))
72 if err != nil {
73 return nil, nil, token.NoPos, err
74 }
75 return pkg, pgf, pgf.Tok.Pos(posn.Offset), nil
76 }
77
78 func selectPackageForFile(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI, selector func([]*metadata.Package) *metadata.Package) (*cache.Package, *parsego.File, error) {
79 mps, err := snapshot.MetadataForFile(ctx, uri, true)
80 if err != nil {
81 return nil, nil, err
82 }
83 if len(mps) == 0 {
84 return nil, nil, fmt.Errorf("no package metadata for file %s", uri)
85 }
86 mp := selector(mps)
87 pkgs, err := snapshot.TypeCheck(ctx, mp.ID)
88 if err != nil {
89 return nil, nil, err
90 }
91 pkg := pkgs[0]
92 pgf, err := pkg.File(uri)
93 if err != nil {
94 return nil, nil, err // "can't happen"
95 }
96 return pkg, pgf, err
97 }
98
99 type (
100 PackageID = metadata.PackageID
101 PackagePath = metadata.PackagePath
102 PackageName = metadata.PackageName
103 ImportPath = metadata.ImportPath
104 )
105
106 type unit = struct{}