Roslyn - Navigate to SourceText in a Visual Studio extension

129 views Asked by At

When using Roslyn in the context of a Visual Studio extension, every SyntaxNode object has a location defined in it (type Microsoft.CodeAnalysis.Location).

Is it somehow possible to let Visual Studio navigate to a specific location in the UI?

The EditorFeatures package has some extension methods to retrieve the TextBuffer and Snapshot from the location, but it's still quite some manual work to open the document when needed, etcetera. I have the feeling there is somewhere a method that can do that for me.

https://github.com/dotnet/roslyn/blob/main/src/EditorFeatures/Text/Extensions.cs

1

There are 1 answers

2
Jason Malinowski On

So if you have a symbol in hand and you just want to do the equivalent of go to definition, you could call this method after MEF importing VisualStudioWorkspace. Otherwise, it doesn't look like we have a public helper in Roslyn anywhere. (We've absolutely got the implementation internally, of course...)