Paul
Paul

Reputation: 6228

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:

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?

Upvotes: 0

Views: 559

Answers (2)

JoshVarty
JoshVarty

Reputation: 9426

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/

Upvotes: 2

SLaks
SLaks

Reputation: 887479

You need to use the existing stock Workspace, or you won't get references, nor, more importantly, preprocessor symbols (which affect the shape of the syntax tree).

Note that VS can have a couple of different Workspaces, including the main VisualStudioWorkspace for the current solution, MiscellaneousFilesWorkspace for other files, and some other debugger-related ones. (full list)

Upvotes: 1

Related Questions