Search code examples
roslynvisual-studio-extensionsvsixvisual-studio-sdkvssdk

How to navigate to a definition of a Symbol located in the external assembly


I have a VS extension with a custom "Code Map" tool window. It displays specific code elements in a tree view control. The tree view nodes support navigation to symbol definition on a double click on the tree node. The navigation uses Roslyn symbols corresponding to tree nodes, and navigates to them.

This all relies on the integration between Visual Studio and Roslyn. Basically, it tries to get location file path and span from Roslyn symbol or node, then use file path to open the file and get Microsoft.VisualStudio.Text.Editor.IWpfTextViewvia VS SDK utilities. Then I use IWpfTextView and location span to move caret to the required location. You can see details on GitHub: https://github.com/Acumatica/Acuminator/blob/dev/src/Acuminator/Acuminator.Vsix/Utils/Navigation/VSDocumentNavigation.cs

This works just fine with symbols declared in the solution but does not work for symbols declared in external assemblies. At the same time, Visual Studio itself navigates to external symbols with F12.

I would like to know if it is possible to repeat the same navigation programmatically with Roslyn and VS SDK.

So far, I have found only the following hints:


Solution

  • As discussed in the comments, yes, you can just call VisualStudioWorkspace.TryGoToDefinitionAsync(); the Project parameter is simply used for us to help figure out where the symbol came from -- the symbol can still be from metadata or somewhere else.