golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.79k stars 728 forks source link

Go To Implementations should default to handwritten implementations rather than generated implementations. #2580

Open RoarkeRandall opened 1 year ago

RoarkeRandall commented 1 year ago

Is your feature request related to a problem? Please describe. "Go To Implementations" is a great feature to navigate through your code that uses interfaces heavily. I'm suggesting a basic heuristic to make it a little better. For many go codebases, one of the implementations will be a generated mock of the interface for testing purposes. These are generally not the code you're trying to navigate to. Unfortunately, in my experience the mock implementations are the default selected implementation in the code peek view, as shown in this image:

image

Describe the solution you'd like A basic heuristic to try to select a better implementation to show as the default in the code peek view. I would avoid selecting generated implementations as the default to display since those are commonly mocks. I think a basic sorting where the file is generated is ranked less than a file that is not. I assume you can look for // Code generated by MockGen. DO NOT EDIT. or specifically the DO NOT EDIT

I'm happy to submit a PR if someone can point me in the right direction!

awakia commented 1 year ago

I'm also happy when this feature implemented. I also find same issue on vscode side but closed since caused-by-estension, but this should be general issues for go programmers. https://github.com/microsoft/vscode/issues/143352

The suggested solution must be work on my repository case. DO NOT EDIT must be common on other mock libraries. e.g. https://github.com/vektra/mockery/pull/188 Maybe, avoiding the word "mock" can also be good approach. image

hyangah commented 1 year ago

It looks like a duplicate of https://github.com/golang/vscode-go/issues/2074 which was auto-closed

@findleyr 's comment from https://github.com/golang/vscode-go/issues/2074#issuecomment-1105318727 also mentioned utilizing DO NOT EDIT as a signal.

Assuming that the mocks follow the convention of go.dev/issues/13560, i.e. have a DO NOT EDIT header, we could simply down-rank results that are in generated files. That seems like an unambigious rule that would work in most cases.

It's unclear to me what "down-rank" means though. Neither VS Code API https://code.visualstudio.com/api/references/vscode-api#ImplementationProvider or LSP https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_implementation is clear about ranking. Is the order sent by gopls preserved? And I don't see a way to control which one is expanded automatically when multiple entries are returned by implementationProvider either.

@RoarkeRandall the relevant code location is https://github.com/golang/tools/blob/3be06475e084b20fbd505539810c5233cbc285b8/gopls/internal/lsp/implementation.go#L14 and https://github.com/golang/tools/blob/master/gopls/internal/lsp/source/implementation.go#L22. Can you play around with the ordering and see that helps?

I don't think completely dropping mock or code from auto-generated files is a good idea - that will take away from users an option to check the full list of implementations or inspect the correctness of mock implementation.

RoarkeRandall commented 1 year ago

I don't think completely dropping mock or code from auto-generated files is a good idea - that will take away from users an option to check the full list of implementations or inspect the correctness of mock implementation.

Agreed.

And I don't see a way to control which one is expanded automatically when multiple entries are returned by implementationProvider either.

I have a hunch it's just the last result that is expanded.

Can you play around with the ordering and see that helps?

Sure, I can try just swapping the order and seeing if it has an effect. I'll need a bit to figure out how to test it.

keremgocen commented 10 months ago

I've been searching for a solution to https://github.com/golang/vscode-go/issues/2074 which led me here, and if no one is working on this atm I'd like to have a go...