-
Notifications
You must be signed in to change notification settings - Fork 19k
x/tools/gopls: add CodeLens Go to TestXxx #75030
Copy link
Copy link
Open
Labels
ToolProposalIssues describing a requested change to a Go tool or command-line program.Issues describing a requested change to a Go tool or command-line program.ToolsThis label describes issues relating to any tools in the x/tools repository.This label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.Issues related to the Go language server, gopls.
Milestone
Metadata
Metadata
Assignees
Labels
ToolProposalIssues describing a requested change to a Go tool or command-line program.Issues describing a requested change to a Go tool or command-line program.ToolsThis label describes issues relating to any tools in the x/tools repository.This label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.Issues related to the Go language server, gopls.
Env
gopls version:
master (5397e65c)Proposal
GoLand offers a
Go to testshortcut (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 commandGo to TestXxxto theSource Actionwindow (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 TestXxxCodeLens forslicespackage (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:
TestFooforFooTest_fooforfooTestBuffer_Bytesfor(Buffer).BytesTestFooforfooTestBuffer_Bytesfor(buffer).BytesThis 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 excludeTestDeleteFuncfrom matches forDelete:Maybe it's worth implementing this feature both as CodeLens and Source Action?
- requires only a single click
- noisy when a function has many tests [1]
- requires 3 actions:
Alt+., move selection,EnterWe 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.