| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 47 |
cur, ok := pgf.Cursor().FindByPos(start, end) |
| 48 |
if !ok { |
| 49 |
return nil, fmt.Errorf("no enclosing syntax") |
| 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 |
|
| 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, |
| 77 |
SelectionRange: declLoc.Range, |
| 78 |
}}, nil |
| 79 |
} |
| 80 |
|
| 81 |
|
| 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 |
|
| 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 |
|
| 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) |
| 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, |
| 120 |
SelectionRange: loc.Range, |
| 121 |
}) |
| 122 |
}) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
cmp := func(x, y protocol.TypeHierarchyItem) int { |
| 130 |
if d := strings.Compare(x.Detail, y.Detail); d != 0 { |
| 131 |
|
| 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 |
} |