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: Complete return as a snippet #64266

Closed
vikblom opened this issue Nov 19, 2023 · 7 comments
Closed

x/tools/gopls: Complete return as a snippet #64266

vikblom opened this issue Nov 19, 2023 · 7 comments
Assignees
Labels
FeatureRequest 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

@vikblom
Copy link

vikblom commented Nov 19, 2023

Feature suggestion

It would be convenient if the return keyword could be completed as a snippet that pre-populates the zero values of return parameters.

For example

func foo() (string, err) {
    ret<TAB>
}

would turn into

func foo() (string, err) {
    return "", nil
}

with the snippet cursor over "" and nil respectively.

Currently the keyword is only completed to a plain return. This could be an alternative completion ranked one step lower.

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

muirdm commented Nov 20, 2023

Note there is some logic to write a "return" snippet with zero values here. This proposed completion could work in a similar way to that linked completion.

@vikblom
Copy link
Author

vikblom commented Nov 20, 2023

Thanks @muirdm that is a great point!

This proposal would be a subset of that functionality. I'll see if I can make it fit together nicely.

@findleyr findleyr modified the milestones: Unreleased, gopls/v0.16.0 Nov 27, 2023
@findleyr
Copy link
Contributor

Thanks @vikblom and @muirdm!

I have tentatively put this in the gopls@v0.16.0 milestone, just for our own tracking purposes. You are course free to do as little or as much as you want, whenever you want :)

@gopherbot
Copy link

Change https://go.dev/cl/546775 mentions this issue: gopls/internal/lsp/source/completion: complete return snippet

@findleyr
Copy link
Contributor

findleyr commented Jan 6, 2024

This was fixed by https://go.dev/cl/546775

(the commit message said "Closes" instead of "Fixes").

@rogeryk
Copy link
Contributor

rogeryk commented Jan 11, 2024

Hi, I'm sorry for commenting so late. If "err" exists, should it be used first to instead "nil"? I think this is more common.

@vikblom
Copy link
Author

vikblom commented Jan 11, 2024

Hi @rogeryk

That is a valid point. I decided against it, keeping the zero value even for the error, to keep the implementation simple.

The end user might want err, fmt.Errorf, errors.New, or some local variant thereof (which is what I almost always use). My reasoning was that since the placeholder almost surely needs to be replaced anyway, it doesn't hurt that it is nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest 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

5 participants