-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: code action for implementing interface #60290
Comments
impl is available from VS Code through the gopls also has support for generating interface implementations as code actions (see #37537). Please let us know if there are ways that these features could be improved for your use case! |
Hi @suzmue, I knew about vscode & impl (not using vscode though), but I'd like gopls to do this out of the box. Also impl doesn't analyze existing code / existing methods, it just generates stubs from scratch. I struggle to find how to do use the code actions of gpls you mentioned through #37537. I'm using neovim but I'm able to trigger code action requests through code: where should my cursor be and can I just ask gopls for available actions or should I explicitly trigger a specific action? Please note the gopls documentation doesn't mention this special code action either. Thanks in advance! |
Change https://go.dev/cl/497756 mentions this issue: |
@suzmue I may overlook something, but checking the source of gopls, I don't see any implement interface command of sorts. |
It's admittedly tricky to find. First you must create a statement that will not compile because the concrete type does not satisfy the interface. For example: var r io.Reader = new(myReader). Gopls will report a diagnostic at the right-hand side of the assignment, and associated with that diagnostic is a code action to fix the type error by defining the missing methods. Each editor displays the code action differently, but generally it is some kind of button or menu item labelled "implement interface". Clicking it will apply an edit to your source file. |
@adonovan, yes that works (neovim in my case)! Hehe, it's actually a nice way to avoid the need for UI to pick which interface to implement :D It also nicely updates structs with partial implementations. Thanks! |
Four operations (rename, fix, imports, format) apply edits to the client's files. This change causes them to behave the same w.r.t. the -write, -diff, -preserve, and -list flags. Specifically, the default behavior prints the edited file contents (sans filename or newline). The -write, -diff, and -list flags suppress the default behavior but are otherwise orthogonal. The -preserve flag causes -write to save backups. (These semantics were based on the previous behavior of 'format', which I suspect is the most important of the subcommands by usage.) Also: - clarify the documentation of the fix operation, including examples of cases that now work, such as InvalidIfaceAssign (see the attached issue). - support CodeAction edit commands, which may cause the server to make an ApplyEdit downcall. ApplyEdit is handled using the same logic and flags as the client-initiated edits. - handle various discarded errors properly. - remove unnecessary Contexts. - add tests. Note: this CL changes the behavior of the rename and imports commands when multiple flags are provided. Before, flags had precedence -w > -d > default. Now, the -w -d -l flags are orthogonal and only if none are provided is the changed output printed. Also, the rename operation no longer prints the name of each changed file. Updates golang/go#60290 Change-Id: If986267ca4365e95f379d8243de6f302f7e756c0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/497756 Reviewed-by: Robert Findley <rfindley@google.com> Run-TryBot: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
It would be great if
gopls
could implement an interface for the struct under cursor, like https://github.com/josharian/impl does, but then updating existing implementation as well, erroring if it can't because of incompatibilities. Because of LSP,gopls
could also be accurate about using existing imports/aliases of packages.GoLand can already do this, would be great to bring it to other editors this way too!
The text was updated successfully, but these errors were encountered: