Embed the Visual Studio editor & theme architecture in your own programs
VSEmbed lets you embed the Visual Studio editor & theme architecture in your own programs.
#Usage Install from NuGet.
To initialize Visual Studio, you need the following code:
VsLoader.LoadLatest(); // Or .Load(new Version(...))
VsServiceProvider.Initialize();
VsMefContainerBuilder.CreateDefault().Build(); // Only needed for editor embedding
The last line can only be JITted after initializing VsLoader (because Build()
returns an IComponentModel
, which is defined in a VS assembly), so you should put it in a separate method and call that method after setting up VsLoader.
You must create the MEF container using VsMefContainerBuilder
; it will use Visual Studio 2015's new version of MEF (where available) to support Roslyn's MEF2 exports.
If you're already using MEF, you can call WithFilteredCatalogs(assemblies)
or WithCatalog(types)
to add your own assemblies to the MEF container. Note that VsMefContainerBuilder
is immutable; these methods return new instances with the new catalogs added.
#Using Roslyn
After loading Dev14, you can set the ContentType of an ITextBuffer to C#
or VisualBasic
to activate the Roslyn editors. However, you will also need to link the ITextBuffer to a Roslyn Workspace to activate the language services. In addition, the workspace must have an IWorkCoordinatorRegistrationService
registered to run diagnostics in the background.
To do all this, use my EditorWorkspace
class, and call CreateDocument()
to create a new document linked to a text buffer, or OpenDocument()
to link an existing document (which is already in the Workspace) to a text buffer. You can also inherit this class to provide additional behavior. You should also set ActiveDocumentId
to the document ID for the document being edited in the current text view, for quicker live error checking.
To add references to framework assemblies, call CreateFrameworkReference()
, which will locate the XML doc comment files for full IntelliSense.
If you create a Roslyn-powered buffer and do not link it to a workspace, I have a buffer listener which will create a simple workspace with a few references for you.
#Caveats
MefHostService
if MefV1HostServices
does not exist, and re-add the older XmlDocumentationProvider
code that was replaced in this commit.IPeekResultPresenter
& IPeekResultPresentation
, and create a WpfTextViewHost in Create()
. Note that peek only operates on file paths.IVsPreviewChangesService
and add it to the ServiceProvider.