@@ -20,29 +20,36 @@ import (
2020 "golang.org/x/tools/internal/event"
2121)
2222
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 ) {
2426 ctx , done := event .Start (ctx , "goasm.References" )
2527 defer done ()
2628
27- pkg , asmFile , err := GetPackageID (ctx , snapshot , fh .URI ())
29+ mps , err := snapshot . MetadataForFile (ctx , fh .URI ())
2830 if err != nil {
2931 return nil , err
3032 }
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 )
3439 if err != nil {
3540 return nil , err
3641 }
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 )
3949 if err != nil {
4050 return nil , err
4151 }
4252
43- // // Parse the assembly.
44- // file := asm.Parse(fh.URI(), content)
45-
4653 // Figure out the selected symbol.
4754 // For now, just find the identifier around the cursor.
4855 var found * asm.Ident
@@ -62,7 +69,10 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
6269 if ! ok {
6370 return nil , fmt .Errorf ("not found" )
6471 }
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.
6676 for _ , pgf := range pkg .CompiledGoFiles () {
6777 for curId := range pgf .Cursor .Preorder ((* ast .Ident )(nil )) {
6878 id := curId .Node ().(* ast.Ident )
@@ -76,6 +86,11 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
7686 }
7787 }
7888
89+ // If includeDeclaration is false, return only reference locations (exclude declarations).
90+ if ! includeDeclaration {
91+ return locations , nil
92+ }
93+
7994 for _ , asmFile := range pkg .AsmFiles () {
8095 for _ , id := range asmFile .Idents {
8196 if id .Name != sym {
@@ -93,25 +108,3 @@ func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
93108
94109 return locations , nil
95110}
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