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: add codelens to show number of references to each symbol #40862

Open
sanket-bhalerao opened this issue Aug 16, 2020 · 26 comments
Open
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Thinking Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@sanket-bhalerao
Copy link

Issue Type: Feature Request

it would be nice to see if a function is referenced in the code or not.
click on the codelens and see all the places the variable, function being referenced.

variable:
image

functions:
image

Extension version: 2020.8.1301
VS Code version: Code - Insiders 1.49.0-insider (bd08768fd310d34cdd4423a53e5f3ca77340fea5, 2020-08-14T05:56:40.601Z)
OS version: Windows_NT x64 10.0.18363

@stamblerre
Copy link
Contributor

I believe that this feature is supported in GOPATH mode, but is not yet available via the language server. I have been hesitant to add it, since users should just be able to call "Find References" on a symbol to get this information. I would be curious to hear input from others before we move forward with this.

@sanket-bhalerao
Copy link
Author

@stamblerre, i tried following for the GOPATH mode.

  • set GOPATH to current working directory
  • install all the tools required for the extension
  • my settings for go are
    image

i still dont see the codelens.
am i missing something?

@hyangah
Copy link
Contributor

hyangah commented Aug 17, 2020

The codelens is off by default, so you will need to turn it on explicitly.

"go.enableCodeLens": {
        "references": true,
    }

But, if you are using the language server, why not use the Find References feature which is faster and doesn't add much visual noise to the code.

@sanket-bhalerao
Copy link
Author

@hyangah , thanks for the help
as to why not use find reference, i work mainly with angular and now just habituated with the codelens. i feel more comfortable with it i guess

@hyangah
Copy link
Contributor

hyangah commented Aug 17, 2020

@sanket-bhalerao Is your goal to simply check whether the variable or function is used or not (boolean decision), or you use it to really find all the locations they are used? For former, I think we can do better with color or other visualization than with codelens. For latter, maybe we should discuss more.

@sanket-bhalerao
Copy link
Author

@hyangah for variables its the boolean decision (as golang shows unused var as errors this part is already covered)
for functions and methods I use it for find if it is referenced at alss and all the places its used.

@hyangah hyangah changed the title codelens for functions, variables x/tools/gopls: consider codelens for functions, variables references Aug 18, 2020
@hyangah
Copy link
Contributor

hyangah commented Aug 18, 2020

@sanket-bhalerao Thanks! I will transfer this to gopls issue tracker.

@hyangah hyangah transferred this issue from golang/vscode-go Aug 18, 2020
@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 Aug 18, 2020
@gopherbot gopherbot added this to the Unreleased milestone Aug 18, 2020
@stamblerre stamblerre modified the milestones: Unreleased, gopls/v.0.5.0 Aug 25, 2020
@stamblerre stamblerre removed this from the gopls/v.0.5.0 milestone Aug 25, 2020
@stamblerre stamblerre changed the title x/tools/gopls: consider codelens for functions, variables references x/tools/gopls: add codelens for references to symbols Aug 25, 2020
@stamblerre
Copy link
Contributor

Thanks for the feature request, @sanket-bhalerao. I'd like to hear from others about the benefits of this feature. Currently, my preference would be to add the 'unused' checks from staticcheck (#36602), which might make this feature a little redundant.

@stamblerre stamblerre added this to the gopls/unplanned milestone Oct 21, 2020
@TerminalFi
Copy link

references is a quick insight into how many times a function is being called an generally would allow the end click to create a view into where the functions / etc are being used for quick looks. I think this is much more valuable than what is being shown here. And completely different from staticcheck. Just look at elm language server for instance

@stamblerre
Copy link
Contributor

stamblerre commented Jun 14, 2021

references is a quick insight into how many times a function is being called an generally would allow the end click to create a view into where the functions / etc are being used for quick looks. I think this is much more valuable than what is being shown here. And completely different from staticcheck. Just look at elm language server for instance

This feature is already available by right-clicking an identifier and selecting "Find all references". The code lens would make it easy to tell if a symbol is referenced, which is typically used to see if it is used anywhere in the workspace--much like the staticcheck 'unused' check.

@TerminalFi
Copy link

I guess a workaround to get this feature would just have the client implementation make the request automatically rather than having to click and request them. 🤷‍♂️ Seems like it may be more of a personal preference thing at that point ?

@stamblerre
Copy link
Contributor

stamblerre commented Jun 21, 2021

I think we would be open to this feature being added in gopls, but it would have to be off by default, because it would be expensive to constantly call "find references" on every symbol in a given file. Still, we will not prioritize work on this, so if a community member would like to pick this up and send a CL, please follow up here.

@tooltitude-support
Copy link

tooltitude-support commented Feb 17, 2023

We implemented this feature in our extension, tooltitude. We focus on augmenting gopls with convenience and productivity features. For example on screenshot you could see the usages of Join in the "stirngs" on kubernetes repo. It computed all reference counts in around 500ms on my M1 Max Macbook pro. On smaller files, and projects it's much faster. On machines with more cores the perf is even better.

Screenshot 2023-02-17 at 9 28 45 AM

P.S. You could read more about it here: https://www.tooltitude.com/ You could install from here: https://marketplace.visualstudio.com/items?itemName=tooltitudeteam.tooltitude

cc @TerminalFi @stamblerre @hyangah @sanket-bhalerao

P.S. Updated check time value to reflect a fix of a bug.

@TerminalFi
Copy link

Looks cool! I am a ST User, and maintain LSP-gopls. I see this is closed source. That is too bad

@tooltitude-support
Copy link

tooltitude-support commented Feb 17, 2023

@TerminalFi In theory nothing prevents it working from within ST. It's mostly LSP, with some additional requests and notifications to improve integration with VSCode. At some point we will consider it, taking into account that sublime is closed source too and people happily pay for performance (i.e. they are more likely than vscode users to be interested in commercial extension).

@adonovan adonovan changed the title x/tools/gopls: add codelens for references to symbols x/tools/gopls: add codelens to show number of reference to each symbol May 17, 2023
@adonovan adonovan changed the title x/tools/gopls: add codelens to show number of reference to each symbol x/tools/gopls: add codelens to show number of references to each symbol May 17, 2023
@tkgalk
Copy link

tkgalk commented Sep 13, 2023

@TerminalFi I have a VS Code extension that is open-source here: https://github.com/tkg-codes/go-interface-annotations. I have also mentioned this in #56695 unfortunately there doesn't seem to be any movement on this one. :(

I moved to neovim since making that extension for VSCode and it seems I can't get the same thing working on nvim, which is really, really sad.

@TerminalFi
Copy link

@TerminalFi I have a VS Code extension that is open-source here: https://github.com/tkg-codes/go-interface-annotations. I have also mentioned this in #56695 unfortunately there doesn't seem to be any movement on this one. :(

I moved to neovim since making that extension for VSCode and it seems I can't get the same thing working on nvim, which is really, really sad.

Time to move to Sublime ;)

@tkgalk
Copy link

tkgalk commented Sep 13, 2023

Does it work there? Goddamit. Do you know if gopls even supports this? I can make the neovim extension, but I need to know whether gopls can even return textdocument/references or textdocument/implementations correctly...

Unless I can get something like in GoLand or in VSCode, I'm going to keep baning my head against the wall and trying to recreate it in neovim. :D
Screenshot 2023-09-14 at 00 00 46

Screenshot 2023-09-14 at 00 01 32

@TerminalFi
Copy link

I am working on an update to LSP-gopls

CleanShot 2023-09-15 at 09 41 05@2x

@tkgalk
Copy link

tkgalk commented Sep 15, 2023

Hm, are you getting this data from gopls? 🤔 If so then it should be possible for me to code it for neovim too.

@TerminalFi
Copy link

TerminalFi commented Sep 15, 2023

Hm, are you getting this data from gopls? 🤔 If so then it should be possible for me to code it for neovim too.

Yes. Essentially this is the logic I am using.

  1. File is activitated (Opened or Focused)
  2. Send LSP Request textDocument/documentSymbol
  3. For each Symbol returned
for symbol in symbols:
    params = {
        'textDocument': {'uri': view_to_uri(self.view)},
        'position': symbol['selectionRange']['start']
    }
    request = Request("textDocument/references", params)

@tkgalk
Copy link

tkgalk commented Sep 15, 2023

Hm, this is what I tried. Hm. Maybe I just messed something up the last time. Oh well, got a new weekend project, I guess. Cheers!

@TerminalFi
Copy link

@tkgalk I actually have a working implementation in a branch of gopls. I need to figure out the proper way to open an MR for it though.

Seems to be relatively fast

CleanShot 2023-09-18 at 19 03 48@2x

@stamblerre I might need a little direction on handling sending the response back to the client. Looks like i need to do something in tsclient.go

@tkgalk
Copy link

tkgalk commented Sep 19, 2023

Yeah, I managed to code something for neovim, I'll release it later on GitHub for anyone wanting to use it, but I need to finish some stuff first (filter out declarations, add configuration options and squash a bug or two).

Thanks for pointing me in the right direction. :D

image

While making it, I also found that someone had already done something similar, though: https://github.com/VidocqH/lsp-lens.nvim.

@TerminalFi
Copy link

Yeah, just meant that this should be in core once I finish this. So it won't need to be handled by the client.

@FOWind
Copy link

FOWind commented Jan 10, 2024

We implemented this feature in our extension, tooltitude. We focus on augmenting gopls with convenience and productivity features. For example on screenshot you could see the usages of Join in the "stirngs" on kubernetes repo. It computed all reference counts in around 500ms on my M1 Max Macbook pro. On smaller files, and projects it's much faster. On machines with more cores the perf is even better.

Screenshot 2023-02-17 at 9 28 45 AM

P.S. You could read more about it here: https://www.tooltitude.com/ You could install from here: https://marketplace.visualstudio.com/items?itemName=tooltitudeteam.tooltitude

cc @TerminalFi @stamblerre @hyangah @sanket-bhalerao

P.S. Updated check time value to reflect a fix of a bug.

Nice extensions !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Thinking Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

9 participants