Today I was working on a sample Visual Studio extension and I had the following question:
Given a Visual Studio ITextSnapshot or ITextBuffer how can I get the corresponding Roslyn Document?
It turns out there’s a bunch of extension methods that make this easy. They’re not shipped with the Microsoft.CodeAnalysis NuGet package so you’ll need to pull them down manually:
- Install the EditorFeatures NuGet package:
Install-Package Microsoft.CodeAnalysis.EditorFeatures.Text - Include the appropriate a using statement:
using Microsoft.CodeAnalysis.Text;
Now you’ll be able to map from ITextBuffer and ITextSnapshot back to Roslyn’s Document, SourceText and Workspace objects.