Skip to content

x/tools/gopls: add CodeLens Go to TestXxx #75030

@ShoshinNikita

Description

@ShoshinNikita

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

  1. I am not sure what algorithm should be used to match functions with their tests. A naive approach looks like this:

    1. Try to find test functions that match name exactly:
      • TestFoo for Foo
      • Test_foo for foo
      • TestBuffer_Bytes for (Buffer).Bytes
    2. Search with the capitalized first letter:
      • TestFoo for foo
      • TestBuffer_Bytes for (buffer).Bytes

    This algorithm should not have many false positives, but it fails to match TestReplaceGrow, TestReplacePanics, etc. with slices.Replace. We could use a prefix search like GoLand does, but I am not sure how to exclude TestDeleteFunc from matches for Delete:

  2. 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

    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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ToolProposalIssues 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.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions