Skip to content

Commit e8ff82c

Browse files
findleyrgopherbot
authored andcommitted
gopls: support jump to definition for field links in comments
I don't know why we didn't support this, but it was trivial to enable. Fixes golang/go#75038 Change-Id: Ia41953aadb1fcf459db16049c1e3bef56cd7882c Reviewed-on: https://go-review.googlesource.com/c/tools/+/696435 Auto-Submit: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 6ebfec2 commit e8ff82c

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

gopls/internal/golang/comment.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ func lookupDocLinkSymbol(pkg *cache.Package, pgf *parsego.File, name string) typ
181181
// that define the symbol but are not directly imported;
182182
// see https://github.com/golang/go/issues/61677
183183

184-
// Type.Method?
185-
recv, method, ok := strings.Cut(name, ".")
184+
// Field or sel?
185+
recv, sel, ok := strings.Cut(name, ".")
186186
if ok {
187187
obj := scope.Lookup(recv) // package scope
188188
if obj == nil {
@@ -192,11 +192,8 @@ func lookupDocLinkSymbol(pkg *cache.Package, pgf *parsego.File, name string) typ
192192
if !ok {
193193
return nil
194194
}
195-
m, _, _ := types.LookupFieldOrMethod(obj.Type(), true, obj.Pkg(), method)
196-
if is[*types.Func](m) {
197-
return m
198-
}
199-
return nil
195+
m, _, _ := types.LookupFieldOrMethod(obj.Type(), true, obj.Pkg(), sel)
196+
return m
200197
}
201198

202199
if obj := scope.Lookup(name); obj != nil {

gopls/internal/test/marker/testdata/definition/comment.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@ func Conv(s string) int { //@loc(Conv, "Conv")
2727
return int(i)
2828
}
2929

30+
type T struct {
31+
Field int //@ loc(Field, "Field")
32+
}
33+
34+
func (T) Method() {} //@ loc(Method, "Method")
35+
3036
// The declared and imported names of the package both work:
3137
// [path.Join] //@ def("Join", Join)
3238
// [pathpkg.Join] //@ def("Join", Join)
39+
//
40+
// Also, both [T.Field] and //@ def("Field", Field)
41+
// [T.Method] are supported. //@ def("Method", Method)
3342
func _() {
3443
pathpkg.Join()
3544
}

0 commit comments

Comments
 (0)