golang/go

x/tools/gopls: Hover: for func literal implicitly converted to named func type, show doc for that type

Opened this issue · 3 comments

Example:

filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
    ...
}

I'm always frustrated when I'm writing function body of a function literal passed as an argument of a call statement. When calling external libraries with callback arguments, I sometimes forget the meaning of the parameter, like err parameter of function literal whose type is the named type fs.WalkDirFunc, and I have to go to the documentation website, which is inconvenient. Besides, sometimes I want to see the documentation of fs.WalkDirFunc. I have to first jump to the definition of filepath.WalkDir, then jump to the type definition of fs.WalkDirFunc.

The type of the function literal can be decided if it's passed as an argument and the argument type is fs.WalkDirFunc or the declared type is a defined type, like var fn fs.WalkDirFunc

Describe the solution you'd like
A clear and concise description of what you want to happen.

Show documentation of function type definition when hovering over func token of the function literal if the function literal has no function name and the type of the function literal can be decided as a named type.

For example, documentation of fs.WalkDirFunc will be shown when hover over func token in the above example.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Concat documentation of named types of arguments after the documentation of named function.

Additional context
Add any other context or screenshots about the feature request here.

I believe all hover text like this comes from gopls

I have to first jump to the definition of filepath.WalkDir, then jump to the type definition of fs.WalkDirFunc.

I recently implemented golang/vscode-go#3827, you don't have to jump twice I guess. But this is still a special case because it's coincident that filepath.WalkDir's comment luckily have [fs.WalkDirFunc].

Maybe we can hyperlink all input and output types by default.

Show documentation of function type definition when hovering over func token of the function literal if the function literal has no function name and the type of the function literal can be decided as a named type.

I think this is a great feature request but the exact UX need more thinking. Is func token the right place to show such information?

Recently we added a feature to support hover over a range, right now it only give you the type of your selection. If we also include the comment for the type, maybe it would solve your problem. e.g. if you select the function(...) {...}, we will tell you the type of this expression is fs.WalkDirFunc and it's doc is ....

(You dont really have to select the whole thing, you just need to select a portion of it so gopls understand you are trying to select the whole thing)

cc @adonovan

Move to golang/go x/tools.

I like this idea. Care to send a CL?

(In the absence of #70638, probably the best approach is to use typeutil.TypesFromContext to find the type to which the FuncLit is assigned.)