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: add "if err != nil" snippet completion for Test/Benchmark Functions #43310

Closed
marwan-at-work opened this issue Dec 21, 2020 · 2 comments
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@marwan-at-work
Copy link
Contributor

One of my favorite features of Gopls is that it can auto-complete my error returns by writing a contextual if-statement block like so:

if err != nil {
  return <zero types>, ${1:err}
}

It accomplishes the above by doing the following:

  1. Checks if the enclosing function's signature returns an error
  2. Checks if the cursor's previous statement assigns an error.
  3. Offers an auto-complete snippet that writes out the entire block above.

The main logic of this feature lives here: https://github.com/golang/tools/blob/1965356128881de7178c4c0a1cbeb15035659dea/internal/lsp/source/completion/statements.go#L175

I propose that we update this function to include the following use case (that I find myself running into a lot):

func TestSomeLogic(t *testing.T) {
  _, err := os.Open("foo")
  <>
}

If the client asked for completion at the previous cursor, we should offer the following completion:

if err != nil {
  t.Fatal(err)
}

Reasoning:

Writing if err != nil { t.Fatal(err) } is quite common.

Quickly searching for the statement in popular repos I found that in the x/tools repository, this exact block appears about 393 times:

Screen Shot 2020-12-21 at 4 35 59 PM

While, in the moby/moby repository, it appears 749 times:

Screen Shot 2020-12-21 at 4 36 16 PM

Therefore, having this auto-completion will make Go developer's write tests more quickly when they need it.

In the future, more clever UX can be sketched out that will handle other common scenarios such as Fatalf but I believe as an initial implementation, t.Fatal(err) is a really good start and perhaps good enough.

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Dec 21, 2020
@gopherbot gopherbot added this to the Unreleased milestone Dec 21, 2020
@gopherbot
Copy link

Change https://golang.org/cl/279488 mentions this issue: lsp/source/completion: Add snippet completion for t.Fatal errs

@eyJhb

This comment has been minimized.

@golang golang locked and limited conversation to collaborators Nov 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants