@@ -20,29 +20,36 @@ import (
20
20
"golang.org/x/tools/internal/event"
21
21
)
22
22
23
- func References (ctx context.Context , snapshot * cache.Snapshot , fh file.Handle , position protocol.Position ) ([]protocol.Location , error ) {
23
+ // References returns a list of locations (file and position) where the symbol under the cursor in an assembly file is referenced,
24
+ // including both Go source files and assembly files within the same package.
25
+ func References (ctx context.Context , snapshot * cache.Snapshot , fh file.Handle , position protocol.Position , includeDeclaration bool ) ([]protocol.Location , error ) {
24
26
ctx , done := event .Start (ctx , "goasm.References" )
25
27
defer done ()
26
28
27
- pkg , asmFile , err := GetPackageID (ctx , snapshot , fh .URI ())
29
+ mps , err := snapshot . MetadataForFile (ctx , fh .URI ())
28
30
if err != nil {
29
31
return nil , err
30
32
}
31
-
32
- // Read the file.
33
- content , err := fh .Content ()
33
+ metadata .RemoveIntermediateTestVariants (& mps )
34
+ if len (mps ) == 0 {
35
+ return nil , fmt .Errorf ("no package metadata for file %s" , fh .URI ())
36
+ }
37
+ mp := mps [0 ]
38
+ pkgs , err := snapshot .TypeCheck (ctx , mp .ID )
34
39
if err != nil {
35
40
return nil , err
36
41
}
37
- mapper := protocol .NewMapper (fh .URI (), content )
38
- offset , err := mapper .PositionOffset (position )
42
+ pkg := pkgs [0 ]
43
+ asmFile , err := pkg .AsmFile (fh .URI ())
44
+ if err != nil {
45
+ return nil , err // "can't happen"
46
+ }
47
+
48
+ offset , err := asmFile .Mapper .PositionOffset (position )
39
49
if err != nil {
40
50
return nil , err
41
51
}
42
52
43
- // // Parse the assembly.
44
- // file := asm.Parse(fh.URI(), content)
45
-
46
53
// Figure out the selected symbol.
47
54
// For now, just find the identifier around the cursor.
48
55
var found * asm.Ident
@@ -62,7 +69,10 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
62
69
if ! ok {
63
70
return nil , fmt .Errorf ("not found" )
64
71
}
65
- // return localReferences(pkg, targets, true, report)
72
+
73
+ // TODO(grootguo): Currently, only references to the symbol within the package are found (i.e., only Idents in this package's Go files are searched).
74
+ // It is still necessary to implement cross-package reference lookup: that is, to find all references to this symbol in other packages that import the current package.
75
+ // Refer to the global search logic in golang.References, and add corresponding test cases for verification.
66
76
for _ , pgf := range pkg .CompiledGoFiles () {
67
77
for curId := range pgf .Cursor .Preorder ((* ast .Ident )(nil )) {
68
78
id := curId .Node ().(* ast.Ident )
@@ -76,6 +86,11 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
76
86
}
77
87
}
78
88
89
+ // If includeDeclaration is false, return only reference locations (exclude declarations).
90
+ if ! includeDeclaration {
91
+ return locations , nil
92
+ }
93
+
79
94
for _ , asmFile := range pkg .AsmFiles () {
80
95
for _ , id := range asmFile .Idents {
81
96
if id .Name != sym {
@@ -93,25 +108,3 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
93
108
94
109
return locations , nil
95
110
}
96
-
97
- func GetPackageID (ctx context.Context , snapshot * cache.Snapshot , uri protocol.DocumentURI ) (* cache.Package , * asm.File , error ) {
98
- mps , err := snapshot .MetadataForFile (ctx , uri )
99
- if err != nil {
100
- return nil , nil , err
101
- }
102
- metadata .RemoveIntermediateTestVariants (& mps )
103
- if len (mps ) == 0 {
104
- return nil , nil , fmt .Errorf ("no package metadata for file %s" , uri )
105
- }
106
- mp := mps [0 ]
107
- pkgs , err := snapshot .TypeCheck (ctx , mp .ID )
108
- if err != nil {
109
- return nil , nil , err
110
- }
111
- pkg := pkgs [0 ]
112
- asmFile , err := pkg .AsmFile (uri )
113
- if err != nil {
114
- return nil , nil , err // "can't happen"
115
- }
116
- return pkg , asmFile , nil
117
- }
0 commit comments