Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: feature request - render example tests as part of hover documentation #71837

Open
SKalt opened this issue Feb 19, 2025 · 2 comments
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@SKalt
Copy link

SKalt commented Feb 19, 2025

gopls version

v0.17.1
Build info
----------
golang.org/x/tools/gopls v0.17.1
    golang.org/x/tools/[email protected] h1:Mt/DSfnnSe3dyf6MH/dZZ0iww+viHNhAFc4rEYDiOAw=
    github.com/BurntSushi/[email protected] h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=
    github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/[email protected] h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=
    golang.org/x/[email protected] h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
    golang.org/x/[email protected] h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
    golang.org/x/[email protected] h1:TCDqnvbBsFapViksHcHySl/sW4+rTGNIAoJJesHRuMM=
    golang.org/x/[email protected] h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
    golang.org/x/[email protected] h1:kgwdasJRsdDWYgWcEgMF424DiXwwXHSb3V8xVTi//i8=
    golang.org/x/[email protected] h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
    honnef.co/go/[email protected] h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I=
    mvdan.cc/[email protected] h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
    mvdan.cc/xurls/[email protected] h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.24.0

go env

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/codespace/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/codespace/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build852198375=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/workspaces/temp.go/go.mod'
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/codespace/sdk/go1.24.0'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/codespace/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/codespace/sdk/go1.24.0/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I wanted to provide document usage for a public function, and I wanted its documentation to stay up-to-date. Luckily for me, Go's testing package supports testing examples associated with public functions: see https://pkg.go.dev/testing#hdr-Examples.

I tried it out:
package mypkg

// MyFunc docs
//
//	demoCode() { /* never tested, not necessarily go */ }
func MyFunc() int {
	return 1
}
package mypkg_test

import (
	"fmt"

	"github.com/skalt/temp.go/mypkg"
)

func ExampleMyFunc() { // ExampleX should be associated with function X or type X 
	fmt.Println(mypkg.MyFunc())
	// Output:
	// 1
}

(the full code can be found at https://github.com/SKalt/temp.go/tree/25e36e5)

Finally, I hovered over my function expecting to see some reference to my example.

What did you see happen?

In VSCode, I couldn't see any reference to the example I associated with my function:
Image

However, GoLand displayed footers in its on-hover doucumentation tooltip. The footer included a link to my example, the body of my example function, and the expected output.

What did you expect to see?

I'd like gopls to provide tested examples in on-hover docs like GoLand does. I think easier access to tested examples would be well worth the extra screen space!

I'd be happy to implement this and submit a CL if I get buy-in. My plan is to write some sort of findExamples() call inside hover() in gopls/internal/golang/hover.go.

Editor and settings

VScode:

{
    "gopls": {
        "ui.documentation.hoverKind": "FullDocumentation" // default value
    }
}

Logs

No response

@SKalt SKalt added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Feb 19, 2025
@gopherbot gopherbot added this to the Unreleased milestone Feb 19, 2025
@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label Feb 19, 2025
@gabyhelp gabyhelp added the FeatureRequest Issues asking for a new feature that does not need a proposal. label Feb 19, 2025
@adonovan adonovan removed the Documentation Issues describing a change to documentation. label Feb 19, 2025
@SKalt
Copy link
Author

SKalt commented Feb 20, 2025

Unlike any of the above issues, I'm interested in displaying examples library authors explicitly provided as documentation. GoLand renders examples in full, but I'd also be open to providing links to the examples to minimize the amount of text included in hover boxes.

@findleyr findleyr modified the milestones: Unreleased, gopls/unplanned Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants