| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mod |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"maps" |
| 11 |
"slices" |
| 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/file" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, params *protocol.ReferenceParams) ([]protocol.Location, error) { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
gomod, err := snapshot.ParseMod(ctx, fh) |
| 30 |
if err != nil { |
| 31 |
return nil, err |
| 32 |
} |
| 33 |
if gomod.File == nil { |
| 34 |
return nil, fmt.Errorf("mysterious parse failure %s", fh.URI().Path()) |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
offset, err := gomod.Mapper.PositionOffset(params.Position) |
| 39 |
if err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
var modpath string |
| 43 |
for _, req := range gomod.File.Require { |
| 44 |
if req.Syntax.Start.Byte <= offset && offset <= req.Syntax.End.Byte { |
| 45 |
modpath = req.Mod.Path |
| 46 |
break |
| 47 |
} |
| 48 |
} |
| 49 |
if modpath == "" { |
| 50 |
|
| 51 |
return nil, nil |
| 52 |
} |
| 53 |
|
| 54 |
locs := make(map[protocol.Location]bool) |
| 55 |
|
| 56 |
includeDecl := params.Context.IncludeDeclaration |
| 57 |
|
| 58 |
ids := make(map[metadata.PackageID]bool) |
| 59 |
g := snapshot.MetadataGraph() |
| 60 |
for _, pkg := range g.Packages { |
| 61 |
if pkg.Module != nil && pkg.Module.Path == modpath { |
| 62 |
ids[pkg.ID] = true |
| 63 |
|
| 64 |
if includeDecl { |
| 65 |
fh, err := snapshot.ReadFile(ctx, protocol.URIFromPath(pkg.Module.GoMod)) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
parsed, err := snapshot.ParseMod(ctx, fh) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
if parsed.File != nil && parsed.File.Module != nil { |
| 74 |
start, end := parsed.File.Module.Syntax.Span() |
| 75 |
loc, err := parsed.Mapper.OffsetLocation(start.Byte, end.Byte) |
| 76 |
if err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
locs[loc] = true |
| 80 |
includeDecl = false |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
uris := make(map[protocol.DocumentURI]*metadata.Package) |
| 87 |
for id := range ids { |
| 88 |
for _, importer := range g.ImportedBy[id] { |
| 89 |
if importer.Module.GoMod != gomod.URI.Path() { |
| 90 |
continue |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
for _, uri := range importer.CompiledGoFiles { |
| 95 |
upkgs := g.ForFile[uri] |
| 96 |
for _, upkg := range upkgs { |
| 97 |
if upkg.ID == importer.ID { |
| 98 |
uris[uri] = importer |
| 99 |
break |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
for uri := range uris { |
| 107 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Header) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
xpkg := uris[uri] |
| 116 |
for _, spec := range pgf.File.Imports { |
| 117 |
importPath := metadata.UnquoteImportPath(spec) |
| 118 |
if ids[xpkg.DepsByImpPath[importPath]] { |
| 119 |
loc, err := pgf.NodeLocation(spec.Path) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
locs[loc] = true |
| 124 |
continue |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
ans := slices.SortedFunc(maps.Keys(locs), protocol.CompareLocation) |
| 130 |
return ans, nil |
| 131 |
} |