golang/go

x/tools/gopls: support global "go to implementation" queries on function types [local is done]

Open

#56,572 创建于 2022年11月4日

在 GitHub 查看
 (6 评论) (3 反应) (1 负责人)Go (19,008 fork)batch import
DocumentationFeatureRequestNeedsInvestigationToolsgoplshelp wanted

仓库指标

Star
 (133,883 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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

https://user-images.githubusercontent.com/1029947/200022424-960e463d-7581-47af-ac34-113a2b894fbf.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.

https://user-images.githubusercontent.com/1029947/200023200-f1bb4f6e-17d5-4806-a0f4-ebfe348f95e3.mov

And this same behaviour is true of anonymous functions:

https://user-images.githubusercontent.com/1029947/200023283-dfb77b4b-f5d4-45b1-9306-569008d5a0bb.mov

Expected behavior

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

https://user-images.githubusercontent.com/1029947/200023352-e230dae8-ce32-48bc-809e-8db7ca622c1b.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.

贡献者指南