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: support "go to implementation" (textDocument/implementation) for function types #56572

Open
a-h opened this issue Nov 4, 2022 · 5 comments
Assignees
Labels
Documentation Issues describing a change to documentation. FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@a-h
Copy link
Contributor

a-h commented Nov 4, 2022

As of gopls v0.10.1, the go to implementation feature of the LSP supports finding all of the types which implement an interface via the "Go to Implementation" feature in VS Code and Neovim etc.

This is a feature request to add the same support for named function types, and anonymous functions.

Current behaviour

go_to_implementation_interface.mov

Go also allows you to define function types and also accept functions as a parameter. This is seen in the standard library, e.g. https://pkg.go.dev/net/http#HandlerFunc and https://pkg.go.dev/strings#IndexFunc but here's a simple example.

type FunctionType func(s string, i int)

Any function that has the same signature implements that function type.

func ImplementationOfFunctionType1(s string, i int) {
}

func ImplementationOfFunctionType2(s string, i int) {
}

So, you can use it like this:

func TestFunctionType(f FunctionType) {
	f("s", 0)
}

The current behaviour of gopls does not allow you to go to the implementation of f from within the function body of the TestFunctionType function.

go_to_implementation_function_current.mov

And this same behaviour is true of anonymous functions:

go_to_implementation_function_anon_current.mov

Expected behavior

It should be possible to go the functions which implement the signature, as per this example.

go_to_function_type_implementation.mov

I've also tested the same behaviour with my modified version of the gopls Language Server running with Neovim.

Implementation

I've got an implementation of the functionality to contribute.

@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 Nov 4, 2022
@gopherbot gopherbot added this to the Unreleased milestone Nov 4, 2022
@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label Nov 4, 2022
@findleyr findleyr added FeatureRequest Issues asking for a new feature that does not need a proposal. help wanted labels Nov 4, 2022
@findleyr findleyr modified the milestones: Unreleased, gopls/unplanned Nov 4, 2022
@findleyr
Copy link
Member

findleyr commented Nov 4, 2022

Hi, if you already have an implementation for this, we can take a look. As long as it is not too invasive, this is a feature we'd happily accept. Thanks!

@a-h
Copy link
Contributor Author

a-h commented Nov 4, 2022

Thanks @findleyr - I've added a PR over here: golang/tools#412

I've tried to keep the new features out of the way as much as possible.

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 13, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/619195 mentions this issue: gopls/internal/golang: allow Implementation on any expression

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/654556 mentions this issue: gopls/internal/golang: Implementations for func types

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/655175 mentions this issue: gopls/internal/util/fingerprint: split from cache/methodsets

gopherbot pushed a commit to golang/tools that referenced this issue Mar 5, 2025
This CL adds support to the Implementations query for function
types. The query relates two sets of locations:

  1. the "func" token of each function declaration (FuncDecl or
     FuncLit). These are analogous to declarations of concrete
     methods.

  2. uses of abstract functions:

     (a) the "func" token of each FuncType that is not part of
     Func{Decl,Lit}. These are analogous to interface{...} types.

     (b) the "(" paren of each dynamic call on a value of an
     abstract function type. These are analogous to references to
     interface method names, but no names are involved, which has
     historically made them hard to search for.

An Implementations query on a location in set 1 returns set 2,
and vice versa.

Only the local algorithm is implemented for now; the global
one (using an index analogous to methodsets) will follow.

This CL supersedes CL 448035 and CL 619515, both of which attempt
to unify the treatment of functions and interfaces in the methodsets
algorithm and in the index; but the two problems are not precisely
analogous, and I think we'll end up with more but simpler code
if we implement themn separately.

+ tests, docs, relnotes

Updates golang/go#56572

Change-Id: I18e1a7cc2f6c320112b9f3589323d04f9a52ef3c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/654556
Commit-Queue: Alan Donovan <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Alan Donovan <[email protected]>
gopherbot pushed a commit to golang/tools that referenced this issue Mar 6, 2025
This CL splits the fingerprint data type into its own package,
as the index for Implementations by signatures will need it,
but is otherwise unrelated to the logic to build the index
by method sets.

Updates golang/go#56572

Change-Id: I87905d3c5f3d555f100f318b97080e6802b616e4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/655175
Auto-Submit: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
Commit-Queue: Alan Donovan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation. FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. 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