| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package goasm |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"fmt" |
| 12 |
"go/token" |
| 13 |
|
| 14 |
"golang.org/x/tools/gopls/internal/cache" |
| 15 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 16 |
"golang.org/x/tools/gopls/internal/file" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
"golang.org/x/tools/gopls/internal/util/asm" |
| 19 |
"golang.org/x/tools/gopls/internal/util/morestrings" |
| 20 |
"golang.org/x/tools/internal/event" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range) ([]protocol.Location, error) { |
| 25 |
ctx, done := event.Start(ctx, "goasm.Definition") |
| 26 |
defer done() |
| 27 |
|
| 28 |
mp, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI()) |
| 29 |
if err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
content, err := fh.Content() |
| 35 |
if err != nil { |
| 36 |
return nil, err |
| 37 |
} |
| 38 |
mapper := protocol.NewMapper(fh.URI(), content) |
| 39 |
start, end, err := mapper.RangeOffsets(rng) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
file := asm.Parse(content) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
var found *asm.Ident |
| 53 |
for _, id := range file.Idents { |
| 54 |
if id.Offset <= start && end <= id.End() { |
| 55 |
found = &id |
| 56 |
break |
| 57 |
} |
| 58 |
} |
| 59 |
if found == nil { |
| 60 |
return nil, fmt.Errorf("not an identifier") |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
sym := found.Name |
| 65 |
if sym != "" && sym[0] == '.' { |
| 66 |
sym = string(mp.PkgPath) + sym |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
if pkgpath, name, ok := morestrings.CutLast(sym, "."); ok { |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
var declaring *metadata.Package |
| 78 |
for pkg := range snapshot.MetadataGraph().ForwardReflexiveTransitiveClosure(mp.ID) { |
| 79 |
if pkg.PkgPath == metadata.PackagePath(pkgpath) { |
| 80 |
declaring = pkg |
| 81 |
break |
| 82 |
} |
| 83 |
} |
| 84 |
if declaring == nil { |
| 85 |
return nil, fmt.Errorf("package %q is not a dependency", pkgpath) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
pkgs, err := snapshot.TypeCheck(ctx, declaring.ID) |
| 90 |
if err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
pkg := pkgs[0] |
| 94 |
def := pkg.Types().Scope().Lookup(name) |
| 95 |
if def == nil { |
| 96 |
return nil, fmt.Errorf("no symbol %q in package %q", name, pkgpath) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
pos := def.Pos() |
| 101 |
pgf, err := pkg.FileEnclosing(pos) |
| 102 |
if err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
loc, err := pgf.PosLocation(pos, pos+token.Pos(len(name))) |
| 106 |
if err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
return []protocol.Location{loc}, nil |
| 110 |
|
| 111 |
} else { |
| 112 |
|
| 113 |
for _, id := range file.Idents { |
| 114 |
if id.Name == found.Name && |
| 115 |
(id.Kind == asm.Text || id.Kind == asm.Global || id.Kind == asm.Label) { |
| 116 |
|
| 117 |
loc, err := mapper.OffsetLocation(id.Offset, id.End()) |
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
return []protocol.Location{loc}, nil |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return nil, nil |
| 127 |
} |