-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathdefinition.go
136 lines (121 loc) · 3.83 KB
/
definition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package goasm provides language-server features for files in Go
// assembly language (https://go.dev/doc/asm).
package goasm
import (
"context"
"fmt"
"go/token"
"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/cache/metadata"
"golang.org/x/tools/gopls/internal/file"
"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/gopls/internal/util/asm"
"golang.org/x/tools/gopls/internal/util/morestrings"
"golang.org/x/tools/internal/event"
)
// Definition handles the textDocument/definition request for Go assembly files.
func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position) ([]protocol.Location, error) {
ctx, done := event.Start(ctx, "goasm.Definition")
defer done()
mp, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI())
if err != nil {
return nil, err
}
// Read the file.
content, err := fh.Content()
if err != nil {
return nil, err
}
mapper := protocol.NewMapper(fh.URI(), content)
offset, err := mapper.PositionOffset(position)
if err != nil {
return nil, err
}
// Parse the assembly.
//
// TODO(adonovan): make this just another
// attribute of the type-checked cache.Package.
file := asm.Parse(fh.URI(), content)
// Figure out the selected symbol.
// For now, just find the identifier around the cursor.
var found *asm.Ident
for _, id := range file.Idents {
if id.Offset <= offset && offset <= id.End() {
found = &id
break
}
}
if found == nil {
return nil, fmt.Errorf("not an identifier")
}
// Resolve a symbol with a "." prefix to the current package.
sym := found.Name
if sym != "" && sym[0] == '.' {
sym = string(mp.PkgPath) + sym
}
// package-qualified symbol?
if pkgpath, name, ok := morestrings.CutLast(sym, "."); ok {
// Find declaring package among dependencies.
//
// TODO(adonovan): assembly may legally reference
// non-dependencies. For example, sync/atomic calls
// internal/runtime/atomic. Perhaps we should search
// the entire metadata graph, but that's path-dependent.
var declaring *metadata.Package
for pkg := range snapshot.MetadataGraph().ForwardReflexiveTransitiveClosure(mp.ID) {
if pkg.PkgPath == metadata.PackagePath(pkgpath) {
declaring = pkg
break
}
}
if declaring == nil {
return nil, fmt.Errorf("package %q is not a dependency", pkgpath)
}
pkgs, err := snapshot.TypeCheck(ctx, declaring.ID)
if err != nil {
return nil, err
}
pkg := pkgs[0]
def := pkg.Types().Scope().Lookup(name)
if def == nil {
return nil, fmt.Errorf("no symbol %q in package %q", name, pkgpath)
}
loc, err := mapPosition(ctx, pkg.FileSet(), snapshot, def.Pos(), def.Pos())
if err == nil {
return []protocol.Location{loc}, nil
}
} else {
// local symbols (funcs, vars, labels)
for _, id := range file.Idents {
if id.Name == found.Name &&
(id.Kind == asm.Text || id.Kind == asm.Global || id.Kind == asm.Label) {
loc, err := mapper.OffsetLocation(id.Offset, id.End())
if err != nil {
return nil, err
}
return []protocol.Location{loc}, nil
}
}
}
return nil, nil
}
// TODO(rfindley): avoid the duplicate column mapping here, by associating a
// column mapper with each file handle.
// TODO(adonovan): plundered from ../golang; factor.
func mapPosition(ctx context.Context, fset *token.FileSet, s file.Source, start, end token.Pos) (protocol.Location, error) {
file := fset.File(start)
uri := protocol.URIFromPath(file.Name())
fh, err := s.ReadFile(ctx, uri)
if err != nil {
return protocol.Location{}, err
}
content, err := fh.Content()
if err != nil {
return protocol.Location{}, err
}
m := protocol.NewMapper(fh.URI(), content)
return m.PosLocation(file, start, end)
}