Search code examples
visual-studio-2015roslynvisual-studio-extensions

Using Roslyn for VS2015 Custom Editor


I'm building a Visual Studio 2015 custom editor (not a code editor extension, a totally custom WPF control) to edit .cs files. I'm really uncertain of what the right approach is to use the .NET Compilation SDK here.

So far I've tried:

  • Getting the workspace->soltuion->projects and finding the open file using the file path that is passed to my editor factory. Of course if the document isn't part of the solution you get nothing.
  • Parsing the raw source by pulling the data from the text buffer and spinning up my own isolated syntax tree.

None of what I'm doing now feels clean at all. Is there a more direct way to access compiler APIs for editor's document?


Solution

  • I might be interpreting your question wrong but I'll take a shot. It sounds like you're trying to find the Roslyn document for a given text buffer.

    There's actually a whole set of extension methods to make it easier to bridge the gap between Roslyn and Visual Studio objects.

    See: Microsoft.CodeAnalysis.Text.Extensions

    They're not shipped with the Microsoft.CodeAnalysis NuGet Package though, you'll have to install them via:

    Install-Package Microsoft.CodeAnalysis.EditorFeatures.Text -Version 1.0.0

    Given an ITextSnapshot or ITextBuffer these methods will allow you to map them back to the original Roslyn Documents (if any exist).

    For more info: https://joshvarty.wordpress.com/2015/07/06/lrn-quick-tip-bridging-visual-studio-and-roslyn/