Debian Codegraph @

gopls/internal/golang/type_hierarchy.go

raw symbols SCIP
1 // Copyright 2025 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/ast"
11 "go/types"
12 "slices"
13 "strings"
14 "sync"
15
16 "golang.org/x/tools/gopls/internal/cache"
17 "golang.org/x/tools/gopls/internal/cache/metadata"
18 "golang.org/x/tools/gopls/internal/cache/methodsets"
19 "golang.org/x/tools/gopls/internal/file"
20 "golang.org/x/tools/gopls/internal/protocol"
21 )
22
23 // Type hierarchy support (using method sets)
24 //
25 // TODO(adonovan):
26 // - Support type hierarchy by signatures (using Kind=Function).
27 // As with Implementations by signature matching, needs more UX thought.
28 //
29 // - Allow methods too (using Kind=Method)? It's not exactly in the
30 // spirit of TypeHierarchy but it would be useful and it's easy
31 // enough to support.
32 //
33 // - fix pkg=command-line-arguments problem with query initiated at "error" in builtins.go
34
35 // PrepareTypeHierarchy returns the TypeHierarchyItems for the types at the selected position.
36 func PrepareTypeHierarchy(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range) ([]protocol.TypeHierarchyItem, error) {
37 pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI())
38 if err != nil {
39 return nil, err
40 }
41 start, end, err := pgf.RangePos(rng)
42 if err != nil {
43 return nil, err
44 }
45
46 // For now, we require that the selection be a type name.
47 cur, ok := pgf.Cursor().FindByPos(start, end)
48 if !ok {
49 return nil, fmt.Errorf("no enclosing syntax") // can't happen
50 }
51 id, ok := cur.Node().(*ast.Ident)
52 if !ok {
53 return nil, fmt.Errorf("not a type name")
54 }
55 tname, ok := pkg.TypesInfo().ObjectOf(id).(*types.TypeName)
56 if !ok {
57 return nil, fmt.Errorf("not a type name")
58 }
59
60 // Find declaration.
61 declLoc, err := ObjectLocation(ctx, pkg.FileSet(), snapshot, tname)
62 if err != nil {
63 return nil, err
64 }
65
66 pkgpath := "builtin"
67 if tname.Pkg() != nil {
68 pkgpath = tname.Pkg().Path()
69 }
70
71 return []protocol.TypeHierarchyItem{{
72 Name: tname.Name(),
73 Kind: cond(types.IsInterface(tname.Type()), protocol.Interface, protocol.Class),
74 Detail: pkgpath,
75 URI: declLoc.URI,
76 Range: declLoc.Range, // (in theory this should be the entire declaration)
77 SelectionRange: declLoc.Range,
78 }}, nil
79 }
80
81 // Subtypes reports information about subtypes of the selected type.
82 func Subtypes(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, item protocol.TypeHierarchyItem) ([]protocol.TypeHierarchyItem, error) {
83 return relatedTypes(ctx, snapshot, fh, item, methodsets.Subtype)
84 }
85
86 // Supertypes reports information about supertypes of the selected type.
87 func Supertypes(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, item protocol.TypeHierarchyItem) ([]protocol.TypeHierarchyItem, error) {
88 return relatedTypes(ctx, snapshot, fh, item, methodsets.Supertype)
89 }
90
91 // relatedTypes is the common implementation of {Super,Sub}types.
92 func relatedTypes(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, item protocol.TypeHierarchyItem, rel methodsets.TypeRelation) ([]protocol.TypeHierarchyItem, error) {
93 pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI())
94 if err != nil {
95 return nil, err
96 }
97 pos, err := pgf.PositionPos(item.Range.Start)
98 if err != nil {
99 return nil, err
100 }
101 cur, _ := pgf.Cursor().FindByPos(pos, pos) // can't fail
102
103 var (
104 itemsMu sync.Mutex
105 items []protocol.TypeHierarchyItem
106 )
107 err = implementationsMsets(ctx, snapshot, pkg, cur, rel, func(pkgpath metadata.PackagePath, name string, abstract bool, loc protocol.Location) {
108 if pkgpath == "" {
109 pkgpath = "builtin"
110 }
111
112 itemsMu.Lock()
113 defer itemsMu.Unlock()
114 items = append(items, protocol.TypeHierarchyItem{
115 Name: name,
116 Kind: cond(abstract, protocol.Interface, protocol.Class),
117 Detail: string(pkgpath),
118 URI: loc.URI,
119 Range: loc.Range, // (in theory this should be the entire declaration)
120 SelectionRange: loc.Range,
121 })
122 })
123 if err != nil {
124 return nil, err
125 }
126
127 // Sort by (package, name, URI, range) then
128 // de-duplicate based on the same 4-tuple
129 cmp := func(x, y protocol.TypeHierarchyItem) int {
130 if d := strings.Compare(x.Detail, y.Detail); d != 0 {
131 // Rank the original item's package first.
132 if d := boolCompare(x.Detail == item.Detail, y.Detail == item.Detail); d != 0 {
133 return -d
134 }
135 return d
136 }
137 if d := strings.Compare(x.Name, y.Name); d != 0 {
138 return d
139 }
140 if d := strings.Compare(string(x.URI), string(y.URI)); d != 0 {
141 return d
142 }
143 return protocol.CompareRange(x.SelectionRange, y.Range)
144 }
145 slices.SortFunc(items, cmp)
146 eq := func(x, y protocol.TypeHierarchyItem) bool { return cmp(x, y) == 0 }
147 items = slices.CompactFunc(items, eq)
148
149 return items, nil
150 }