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: code action for implementing interface #60290

Closed
marcelbeumer opened this issue May 18, 2023 · 6 comments
Closed

x/tools/gopls: code action for implementing interface #60290

marcelbeumer opened this issue May 18, 2023 · 6 comments
Labels
FeatureRequest FrozenDueToAge 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

@marcelbeumer
Copy link

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!

@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 May 18, 2023
@gopherbot gopherbot added this to the Unreleased milestone May 18, 2023
@suzmue
Copy link
Contributor

suzmue commented May 22, 2023

impl is available from VS Code through the>Go: Generate Interface Stubs command: https://github.com/golang/vscode-go/wiki/features#generate-interface-implementation.

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!

@suzmue suzmue modified the milestones: Unreleased, gopls/later May 22, 2023
@marcelbeumer
Copy link
Author

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!

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/497756 mentions this issue: gopls/internal/lsp/cmd: consolidate editing flags

@marcelbeumer
Copy link
Author

@suzmue I may overlook something, but checking the source of gopls, I don't see any implement interface command of sorts.

@adonovan
Copy link
Member

adonovan commented May 26, 2023

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.

@marcelbeumer
Copy link
Author

marcelbeumer commented May 31, 2023

@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!

gopherbot pushed a commit to golang/tools that referenced this issue Sep 1, 2023
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>
@golang golang locked and limited conversation to collaborators May 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge 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

4 participants