Skip to content

Commit

Permalink
gopls/internal/golang: fix gopls hover doc link
Browse files Browse the repository at this point in the history
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan committed Nov 20, 2024
1 parent e751756 commit c1aa79d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions gopls/internal/golang/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ type hoverJSON struct {
// SymbolName is the human-readable name to use for the symbol in links.
SymbolName string `json:"symbolName"`

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

// LinkAnchor is the pkg.go.dev link anchor for the given symbol.
Expand Down Expand Up @@ -1367,7 +1370,16 @@ func formatLink(h *hoverJSON, options *settings.Options, pkgURL func(path Packag
var url protocol.URI
var caption string
if pkgURL != nil { // LinksInHover == "gopls"
path, _, _ := strings.Cut(h.LinkPath, "@") // remove optional module version suffix
// Discard optional module version portion.
// (Ideally the hoverJSON would retain the structure...)
path := h.LinkPath
if module, versionDir, ok := strings.Cut(h.LinkPath, "@"); ok {
// "module@version/dir"
path = module
if _, dir, ok := strings.Cut(versionDir, "/"); ok {
path += "/" + dir
}
}
url = pkgURL(PackagePath(path), h.LinkAnchor)
caption = "in gopls doc viewer"
} else {
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/test/integration/misc/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func main() {
).Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
got, _ := env.Hover(env.RegexpSearch("main.go", "F"))
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
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
if m, err := regexp.MatchString(wantRE, got.Value); err != nil {
t.Fatalf("bad regexp in test: %v", err)
} else if !m {
Expand Down

0 comments on commit c1aa79d

Please sign in to comment.