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 some error handle postfix completion (rr, reterr, varCheckError) #64178

Closed
rogeryk opened this issue Nov 15, 2023 · 9 comments
Labels
gopls Issues related to the Go language server, gopls. NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@rogeryk
Copy link
Contributor

rogeryk commented Nov 15, 2023

Describe the solution you'd like

Go requires that you must explicit error handling, so many times you must write some boring error handling code, and postfix completion can ease the pain.
rr
In most cases, we just need check the error and return it.
Kapture 2023-11-15 at 22 47 13
This also depends on the return parameters of the current function, for example, when the function has return values other than error, the return values other than error will use default values.
Kapture 2023-11-15 at 22 49 10

reterr
Sometimes when calling a function, you just need to check if there is an error.
Kapture 2023-11-15 at 22 55 42

varCheckError
Get the return value after calling the function and check for errors. This is a more general version compared to reterr.
Kapture 2023-11-15 at 22 58 52

@rogeryk rogeryk 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 Nov 15, 2023
@gopherbot gopherbot added this to the Unreleased milestone Nov 15, 2023
@muirdm
Copy link

muirdm commented Nov 16, 2023

There is already a fancy completion that writes the entire if err != nil { ... } for you. Do you see it? For example, completing at *HERE* below:

func foo() (int, error) {
  i, err := foo()
  if*HERE*
}

@suzmue suzmue added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Nov 16, 2023
@suzmue suzmue modified the milestones: Unreleased, gopls/backlog Nov 16, 2023
@suzmue suzmue added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 16, 2023
@rogeryk
Copy link
Contributor Author

rogeryk commented Nov 17, 2023

@muirdm Yes, It is same thing in the first case, but I think the latter two will still provide some convenience.
And i find that the rr and reterrr are the same postfix completion, so we can just focus on the latter two scenarios

@rogeryk
Copy link
Contributor Author

rogeryk commented Nov 24, 2023

@findleyr What do you think of this proposal? The pattern of error handle will not change anytime soon, so I think it's very valuable.

@gopherbot
Copy link

Change https://go.dev/cl/550455 mentions this issue: gopls/internal/lsp/source/completion: support postfix completion (rr, varCheckErr)

@findleyr
Copy link
Contributor

@rogeryk I think this makes sense. I'll review your CL -- thanks!

@findleyr
Copy link
Contributor

(although I'm not sure about varCheckErr -- let's discuss on the CL).

@findleyr
Copy link
Contributor

findleyr commented Jan 2, 2024

In discussion on https://go.dev/cl/550455, we have settled on the more consistent snippet names iferr and variferr. I believe iferr is consistent with the preexisting vscode-go snippet: microsoft/vscode-go#1549 (comment).

@rogeryk
Copy link
Contributor Author

rogeryk commented Jan 3, 2024

@findleyr
Gerrit can't seem to send images, so I'll use a gif here to explain why the specified placeholder tabstop is needed.
Look at the pictures below, when I modify "err", all "err" variables are modified at the same time. This requires us to specify the same tabstops for all "err"
iShot_2024-01-03_20 37 46

There is another confusing point, and I will explain it here.There is a nested tabstop in "variferr"

		{{- if eq ($a.TypeName $v.Type) "error" -}}
			{{$errName | $a.SpecifiedPlaceholder (len $a.Tuple) |
				$a.SpecifiedPlaceholder ($a.Inc (len $a.Tuple))}}
		{{- else -}}
		{{- if eq ($a.TypeName $v.Type) "error" -}}
			{{$errName | $a.SpecifiedPlaceholder 1 | $a.SpecifiedPlaceholder 2}}
		{{- else -}}

This is because sometimes we want to return err directly, and sometimes we want to wrap the err. So an additional tabstops is needed to modify the last returned err. Look the below picture.
iShot_2024-01-03_21 08 31

@findleyr
Copy link
Contributor

findleyr commented Jan 9, 2024

@rogeryk that's very cool, and I didn't know it worked that way. Thank you for the explanation.

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants