Skip to content

Commit c1aa79d

Browse files
committed
gopls/internal/golang: fix gopls hover doc link
The hoverJSON.LinkPath field was incorrectly documented, and the gopls links formed from it were invalid when a module version was present. This CL fixes the URL logic. The existing test covered the behavior, but the assertion concealed a mistake. Fixes golang/go#70453 Change-Id: If9a7d3e65dabff50c9f528d82df9de67a71c41d1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/630077 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent e751756 commit c1aa79d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

gopls/internal/golang/hover.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ type hoverJSON struct {
7272
// SymbolName is the human-readable name to use for the symbol in links.
7373
SymbolName string `json:"symbolName"`
7474

75-
// LinkPath is the pkg.go.dev link for the given symbol.
76-
// For example, the "go/ast" part of "pkg.go.dev/go/ast#Node".
77-
// It may have a module version suffix "@v1.2.3".
75+
// LinkPath is the path of the package enclosing the given symbol,
76+
// with the module portion (if any) replaced by "module@version".
77+
//
78+
// For example: "github.com/google/go-github/[email protected]/github".
79+
//
80+
// Use LinkTarget + "/" + LinkPath + "#" + LinkAnchor to form a pkgsite URL.
7881
LinkPath string `json:"linkPath"`
7982

8083
// LinkAnchor is the pkg.go.dev link anchor for the given symbol.
@@ -1367,7 +1370,16 @@ func formatLink(h *hoverJSON, options *settings.Options, pkgURL func(path Packag
13671370
var url protocol.URI
13681371
var caption string
13691372
if pkgURL != nil { // LinksInHover == "gopls"
1370-
path, _, _ := strings.Cut(h.LinkPath, "@") // remove optional module version suffix
1373+
// Discard optional module version portion.
1374+
// (Ideally the hoverJSON would retain the structure...)
1375+
path := h.LinkPath
1376+
if module, versionDir, ok := strings.Cut(h.LinkPath, "@"); ok {
1377+
// "module@version/dir"
1378+
path = module
1379+
if _, dir, ok := strings.Cut(versionDir, "/"); ok {
1380+
path += "/" + dir
1381+
}
1382+
}
13711383
url = pkgURL(PackagePath(path), h.LinkAnchor)
13721384
caption = "in gopls doc viewer"
13731385
} else {

gopls/internal/test/integration/misc/hover_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ func main() {
592592
).Run(t, mod, func(t *testing.T, env *Env) {
593593
env.OpenFile("main.go")
594594
got, _ := env.Hover(env.RegexpSearch("main.go", "F"))
595-
const wantRE = "\\[`a.F` in gopls doc viewer\\]\\(http://127.0.0.1:[0-9]+/gopls/[^/]+/pkg/example.com\\?view=[0-9]+#F\\)" // no version
595+
const wantRE = "\\[`a.F` in gopls doc viewer\\]\\(http://127.0.0.1:[0-9]+/gopls/[^/]+/pkg/example.com/a\\?view=[0-9]+#F\\)" // no version
596596
if m, err := regexp.MatchString(wantRE, got.Value); err != nil {
597597
t.Fatalf("bad regexp in test: %v", err)
598598
} else if !m {

0 commit comments

Comments
 (0)