I’m developing a VS Code extension, and after some interaction, I get a Rust struct string like my::module::StructName. What I want to do is programmatically direct the user to the file/line/column location of the definition—essentially, a “Go to Definition” command.
I couldn’t find a command like “go to definition” or “find file/line/column of a type” in the rust-analyzer registered commands.
So far, my approach has been to create a virtual VS Code document with the input struct and maybe wrap with some Rust code around it.
Then use the generic "Go to Definition" VS Code command, it doesn't work since it doesn't have the right context.
I tried to connect to the running rust-analyzer server via the Language Server Protocol (LSP) to make a textDocument/definition request, which should return the location of the type definition.
The problem is that the VS Code rust-analyzer extension manages (starts and stops) the rust-analyzer server instance. I want to connect to the already running server, not start a new one nor ask the user to maintain an external one.
Is this possible? Which transport would the connection use (IPC, socket, etc.)?
There is "Search in symbols" (standard feature of VSCode and LSP, usually available in VSCode as Ctrl+T) that allows you to search for a symbol globally, but it doesn't currently support fully qualified paths - that is, you can only write "StructName" (and it'll find all occurrences of it) and not the precise "my::module::StructName".
rust-analyzer does not provide another command to do what you want. It is possible to build it (quite easily) on it, but I'd prefer a solution that integrates with Search in symbols (for example, as a very simplistic approach, if the name contains :: resolve it as a path instead of search for it). We welcome contributions, and feel free to ask for guidance if you want to accept the challenge! (Opening an issue might also be a good idea).
I get an array of possible symbols that I sort by Levenshtein distance and keep the first one.
I reveal the editor to the symbol location.
And it works ... sort of. It only finds workspace symbols. I'd like to also get dependency symbols too ... If I search for Vec I would find the official alloc::vec::Vec Rust file ...
So I'm still digging.
Anyway I might look to contribute to rust-analyzer. I'm just not sure symbol searching would work as I would like to ...
I'll probably open an issue some time in the future to focus the discussion.