go: x/tools/gopls: add "implement missing function definition" quick fix
Is your feature request related to a problem? Please describe. It would be a QoL change, and save time(Javascript, C# and other vscode language extensions also have this feature).
Describe the solution you’d like
When you want to call a function but it’s not yet defined and implemented, it would be nice that if you could choose from the context menu, to define the method or function.
for example:
go handleConnection(conn)
then you click on the quick fixes content menu, and it automatically defines the function for you, like this:
func handleConnection(conn net.Con) {}
Describe alternatives you’ve considered
Additional context

About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 9
- Comments: 19 (15 by maintainers)
@enkeyz that’s something I personally would have done after this piece, to keep this relatively small and reasonable. Another thing I’d like to look at as well is returned values expected for the function, i.e.
should generate
But again, as iterations on top of this.
So I’ve put together something that works (or seems to work). I’m probably gathering the data I need to generate those stubs in a poor manner, so please let me know how it can be improved.
I’ve added a bunch of test cases I used to validate what I was doing (still staying well away from any possible values an undeclared func might be intended to return).
For arguments names, if an identifier was used the name of that identifier is reused, otherwise names are inferred by the type of the argument, e.g.:
int->ierror->errnet.Conn->conn[]int->iAlso, if two arguments share the same identifier and/or type, argument name uniqueness is ensured, for example:
For the sake of this conversation, I’m reporting below what code fixed by generating function stubs for each case looks like right now, so whoever is interested in checking the outputs doesn’t have to dive into code:
Test Cases
uis always the function that was undeclared to begin with.Primitives with an identifier
Imported types
Non-primitive types, values and pointers
Non-unique identifiers
Literals / No identifiers
Returned values from another function
Passing an error
Using a function with multiple returned values as an argument
Using operations as arguments
Using a slice as argument
If what’s there is actually usable/useful, what’s the plan now? 😃 also thanks for having me doing this, I’m having a blast!
@rentziass sounds good, please let us know how we can help.
@enkeyz just a year late, sorry 😅 not sure this is something you can solve, test code is still part of a package and depending on whether you’re using a
_testpackage or not it might even be the same package as your non-test code.@findleyr is it ok if I start looking into adding expected returned values too? After sometime spent as a user of this analyzer it really is a bit of a missing feature.
Thanks for adding this feature, but there’s one issue. When doing TDD, you write the tests first, so sometimes the function, or method doesn’t exists.
So when you use this auto implement feature, it creates the function or method in the test file, instead of the normal file, where you put the package code.
Is this something you can solve?
@rentziass Also, yeah, it should consider return values imo. Awesome job btw.
Change https://golang.org/cl/348829 mentions this issue:
internal/lsp/analysis/implementmissing: add analyzerThanks for the suggestion. I agree that this would be helpful, and should theoretically not be that difficult to add.
Transferred to the Go issue tracker, as this would be implemented by Gopls.