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: feature request: adding postfix snippets for errors #50550

Open
urandom opened this issue Jan 11, 2022 · 10 comments
Open

x/tools/gopls: feature request: adding postfix snippets for errors #50550

urandom opened this issue Jan 11, 2022 · 10 comments
Labels
FeatureRequest gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@urandom
Copy link

urandom commented Jan 11, 2022

gopls version

golang.org/x/tools/gopls v0.7.4

Would it be possible to add postfix snippets that are specific for the error type.

I was thinking something like:

err.if!

// Which would expand to:

if err != nil {
    |
}

I guess this one could be uses for any type, not just an error, though maybe the snippet name should be better (ifNot!?)

and a more complex one that returns and takes into account the other return types:

func MyFunc() (*Ptr, int, error) {
    ...

    err.ret!

    // Which would expand to:
    if err != nil {
         return nil, 0, fmt.Errorf("|: %w", err)  
    }
}
@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 Jan 11, 2022
@gopherbot gopherbot added this to the Unreleased milestone Jan 11, 2022
@suzmue suzmue modified the milestones: Unreleased, gopls/unplanned Jan 11, 2022
@muirdm
Copy link

muirdm commented Jan 12, 2022

You can get something similar via statement completions. Does that satisfy your use cases?

@urandom
Copy link
Author

urandom commented Jan 12, 2022

These statement completions seem to be quite old, but I've never seen them being offered as part of any completion request.

I just checked what is currently being returned, and this is not part of the result:

Screenshot from 2022-01-12 10-35-01

I also installed vscode, in case neovim somehow didn't support this, but the result is the same:
Screenshot from 2022-01-12 10-41-46

According to the diff, this requires placeholders, but adding usePlaceholders to both vscode and neovim didn't make a difference for the results, though I'm not sure that placeholders are worth it for me even for this feature (they require a different workflow from what I'm used to)

@muirdm
Copy link

muirdm commented Jan 12, 2022

It should work in any client that supports snippets (do you have snippets disabled?). What does your VSCode go config look like?

This is an example where I see it:
Screen Shot 2022-01-12 at 1 19 13 PM

@urandom
Copy link
Author

urandom commented Jan 12, 2022

Snippets are enabled and working in both neovim and vscode. Both non lsp snippets and ones coming from gopls. The latter was quite visible when I enabled placeholders and was able to jump between the various positions.

The vscode config is empty (fresh install to test this), except for the 3 lines needed to enable placeholders. I'm not in front of a computer right now, so I can't actually post it.

Edit: settings.json:

{
    "gopls": {
        "ui.completion.usePlaceholders": true,
    }
}

@muirdm
Copy link

muirdm commented Apr 17, 2022

I see the if err != nil completion in VS Code with just the usePlaceholders:true setting. Note that it only shows up if "err" was assigned in the previous statement, and if the enclosing function's last return value is an error.

Screen Shot 2022-04-17 at 12 16 55 PM

@thepudds
Copy link

thepudds commented Apr 18, 2022

FWIW, I do see these with the following added to settings.json:

    "gopls": {
        "ui.completion.usePlaceholders": true,
    }

It would be nice if the if err statement completion was enabled by default.

I would be interested in sending a CL enable this by default if that is considered an appropriate change. (That might be a small change for a seasoned gopls contributor, but it would be an opportunity for me to learn more about contributing to gopls).

edit: corrected initial comment (incorrect setting).

@thepudds
Copy link

thepudds commented Apr 18, 2022

And I guess there are at least two ways to enable this by default:

  1. Change the default of "ui.completion.usePlaceholders" to true rather than defaulting off, but that might be a bigger change?
  2. Change the logic of if err statement completion to issue a completion without relying on any placeholders if c.opts.placeholders is false? Currently, it just returns:
func (c *completer) addErrCheckAndReturn() {
	if len(c.path) < 2 || c.enclosingFunc == nil || !c.opts.placeholders {
		return
	}

@muirdm
Copy link

muirdm commented Apr 18, 2022

Either makes sense to me. 1) will be contentious, but that is how it defaults already in Emacs lsp-mode. 2) should be easy, but note that I required placeholders so that the "err" in "return err" would be selected on insertion, making it easier to wrap/contextualize the error.

There is option 3) which is to ignore the usePlaceholders setting in this case and always give a placeholder. This use of placeholders is pretty benign and may not annoy users like the func call placeholders do.

@findleyr findleyr changed the title x/tools/gopls: feature request: adding postfx snippets for errors x/tools/gopls: feature request: adding postfix snippets for errors May 18, 2022
@findleyr findleyr added the gopls/completion Issues related to auto-completion in gopls. label May 18, 2022
@timsofteng
Copy link

timsofteng commented May 27, 2022

How about just ir?

@thepudds
Copy link

thepudds commented May 27, 2022

Just a quick update: I have a basic change (in line with what @muirdm suggested), as well as looking at a slightly larger change, but I’ll probably break the second piece into a separate issue…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls/completion Issues related to auto-completion in gopls. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

7 participants