hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
929 stars 180 forks source link

super+click to open the Go to references popup window #1857

Open LandazuriPaul opened 1 month ago

LandazuriPaul commented 1 month ago

Extension Version

v2.32.3

Problem Statement

This extension is great and allows to explore references both ways, but the cmd+click feature is only enabled in one direction:

Currently, we can still see the references popup by:

Expected User Experience

Since this is the way VS Code behaves with all other languages, it would be great to be able to open the Go to References popup when cmd+clicking on the symbol definition.

This would greatly improve the Developer Experience when navigating TF files.

Proposal

Since the Go to References feature is already implemented in this extension (according to this issue it was introduced in 2021), it would be a matter of "linking" the cmd+click to it.

References

Help Wanted

Community Note

dbanck commented 1 month ago

Interesting feature request! Thanks for raising it. I tend to use this in Go all the time.

I'll document my brief research into this below.


When you cmd+click on a symbol reference, the editor fires a textDocument/definition request to the language server. The LS will then respond with a list of locations of definitions (if found).

When you right click > Go to References on a symbol definition, the editor will fire a textDocument/references request to the language server. The LS will then respond with a list of locations (if found).

In the Terraform extension, when you cmd+click on a symbol definition, the editor will fire a textDocument/definition request as usual, but our LS will respond with an Error for "textDocument/definition" (ID 13): [-32098] no reference origin found, because the symbol is not technically a symbol reference.

However, in the Go extension, the Go LS will not respond with an error. Instead it responds with the range of the clicked symbol:

[Trace - 15:32:45.171 PM] Sending request 'textDocument/definition - (57)'.
Params: {"textDocument":{"uri":"file:///.../scopes.go"},"position":{"line":11,"character":7}}

[Trace - 15:32:45.172 PM] Received response 'textDocument/definition - (57)' in 0ms.
Result: [{"uri":"file:///.../scopes.go","range":{"start":{"line":11,"character":1},"end":{"line":11,"character":10}}}]

This will trigger a textDocument/references request on the client for the same position. The LS will then respond with a list of locations (if found).

I wasn't able to find any information about this behavior in the LSP spec.


Proposal

LandazuriPaul commented 1 month ago

Thank you very much for your response and thorough explanation @dbanck!

Your proposal definitely makes sense to me and I'm happy to help on a PR. I never collaborated on this repo or any VS Code extension, so for now I feel more like waiting for a potential draft, but once it's kicked off, I'm happy to test it, and participate on some parts of the implementation if it can be useful.