@@ -15,12 +15,13 @@ import (
15
15
"regexp"
16
16
"strings"
17
17
18
+ "golang.org/x/tools/go/ast/astutil"
18
19
"golang.org/x/tools/gopls/internal/cache"
19
20
"golang.org/x/tools/gopls/internal/cache/metadata"
20
21
"golang.org/x/tools/gopls/internal/cache/parsego"
21
22
"golang.org/x/tools/gopls/internal/file"
22
23
"golang.org/x/tools/gopls/internal/protocol"
23
- "golang.org/x/tools/gopls/internal/util/astutil"
24
+ goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
24
25
"golang.org/x/tools/gopls/internal/util/bug"
25
26
"golang.org/x/tools/internal/event"
26
27
)
@@ -84,6 +85,32 @@ func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
84
85
return locations , err // may be success or failure
85
86
}
86
87
88
+ // Handle the case where the cursor is on a return statement by jumping to the result variables.
89
+ path , _ := astutil .PathEnclosingInterval (pgf .File , pos , pos )
90
+ if is [* ast.ReturnStmt ](path [0 ]) {
91
+ var funcType * ast.FuncType
92
+ for _ , n := range path {
93
+ switch n := n .(type ) {
94
+ case * ast.FuncLit :
95
+ funcType = n .Type
96
+ case * ast.FuncDecl :
97
+ funcType = n .Type
98
+ }
99
+ if funcType != nil {
100
+ break
101
+ }
102
+ }
103
+ // Inv: funcType != nil, as a return stmt cannot appear outside a function.
104
+ if funcType .Results == nil {
105
+ return nil , nil // no result variables
106
+ }
107
+ loc , err := pgf .NodeLocation (funcType .Results )
108
+ if err != nil {
109
+ return nil , err
110
+ }
111
+ return []protocol.Location {loc }, nil
112
+ }
113
+
87
114
// The general case: the cursor is on an identifier.
88
115
_ , obj , _ := referencedObject (pkg , pgf , pos )
89
116
if obj == nil {
@@ -102,7 +129,7 @@ func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
102
129
for _ , decl := range pgf .File .Decls {
103
130
if decl , ok := decl .(* ast.FuncDecl ); ok &&
104
131
decl .Body == nil &&
105
- astutil .NodeContains (decl .Name , pos ) {
132
+ goplsastutil .NodeContains (decl .Name , pos ) {
106
133
return nonGoDefinition (ctx , snapshot , pkg , decl .Name .Name )
107
134
}
108
135
}
0 commit comments