-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Description
Env
gopls version: master (5397e65c)
Proposal
GoLand offers a Go to test
shortcut (Ctrl+Shift+T
) that allows users to jump to test definitions (if any) or create a new test:

I propose to add a similar feature to gopls
. Initially, I was going to suggest to add a new command Go to TestXxx
to the Source Action
window (Alt+.
in VSCode), mimicking GoLand. However, I think implementing it as a CodeLens would be more convenient: users can immediately see what functions have tests.
An MVP implementation can be found at https://go.dev/cl/696395. Examples of Go to TestXxx
CodeLens for slices
package (actually, Go to {Test|Example|Benchmark|Fuzz}Xxx
):

Questions
-
I am not sure what algorithm should be used to match functions with their tests. A naive approach looks like this:
- Try to find test functions that match name exactly:
TestFoo
forFoo
Test_foo
forfoo
TestBuffer_Bytes
for(Buffer).Bytes
- Search with the capitalized first letter:
TestFoo
forfoo
TestBuffer_Bytes
for(buffer).Bytes
This algorithm should not have many false positives, but it fails to match
TestReplaceGrow
,TestReplacePanics
, etc. withslices.Replace
. We could use a prefix search like GoLand does, but I am not sure how to excludeTestDeleteFunc
from matches forDelete
: - Try to find test functions that match name exactly:
-
Maybe it's worth implementing this feature both as CodeLens and Source Action?
Pros Cons CodeLens - obvious which functions have tests
- requires only a single click- requires a mouse click [0]
- noisy when a function has many tests [1]Source Action - easy to display all tests - not obvious whether a function has tests
- requires 3 actions:Alt+.
, move selection,Enter
- [0] Actually, it's possible to trigger CodeLens via keyboard, but there are some limitations - see Easy pick codelenses with keyboard shortcut microsoft/vscode#91232
- [1] Naive algorithm doesn't not produce that many CodeLenses
We could display only one test function (per type) as a CodeLens (with title like
Go to TestXxx (has X more tests)
) and list all the test functions in the Source Actions menu. This approach should address all the mentioned issues.