I'm working on a project where we've split up C# code into functions and stored these functions within IElisonBuffers. I've got Intellisense hooked up, and the buffers interact with other extensions fine as shown below:
However, I cannot get syntax highlighting to work within these editors.
I embed these editors via the following steps:
- Create an
IVsInvisibleEditor
for the file. - Get the
IVsTextLines
for thisIVsInvisibleEditor
- Create an
IVsCodeWindow
and set the buffer for thisIVsCodeWindow
to be theIVsTextLines
from theIVsInvisibleEditor
- Get an
IWpfTextViewHost
from this code window. This brings me back to "WPF Land" where I'm able to interact with traditional spans. - Create a SnapshotSpan of the
IWpfTextViewHost
's text view. This SnapshotSpan contains a single function. - Create an
IElisionBuffer
containing the SnapshotSpan. - Create an
IVsTextBuffer
viaIVsEditorAdaptersFactoryService.CreateVsTextBufferAdapterForSecondaryBuffer()
passing in theIElisionBuffer
. - Now I cast the
IVsTextBuffer
toIVsTextLines
and callSetLanguageServiceID()
passing in the C# GUID: 694DD9B6-B865-4C5B-AD85-86356E9C88DC. - I double check that it was set correctly via
GetLanguageServiceID()
and everything looks alright. - I create an
IVsTextView
and initialize it with the newIVsTextBuffer
. - I then get the
IWpfTextViewHost
for thisIVsTextView
.
Are there any special steps that need to be taken care of when setting up the language service ID for an IElisionBuffer?
For the sake of completeness this is the code I'm using:
public CustomEditorViewModel CreateEditor(string filePath, int start, int end) {
IVsInvisibleEditor invisibleEditor;
ErrorHandler.ThrowOnFailure(this._InvisibleEditorManager.RegisterInvisibleEditor(
filePath
, pProject: null
, dwFlags: (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING
, pFactory: null
, ppEditor: out invisibleEditor));
var docDataPointer = IntPtr.Zero;
Guid guidIVsTextLines = typeof(IVsTextLines).GUID;
ErrorHandler.ThrowOnFailure(
invisibleEditor.GetDocData(
fEnsureWritable: 1
, riid: ref guidIVsTextLines
, ppDocData: out docDataPointer));
IVsTextLines docData = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer);
//Createa a code window adapter
var codeWindow = _EditorAdapterFactory.CreateVsCodeWindowAdapter(VisualStudioServices.OLEServiceProvider);
//Associate our IVsTextLines with our new code window
ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(docData));
//Get our text view for our editor which we will use to get the WPF control that hosts that editor.
IVsTextView textView;
ErrorHandler.ThrowOnFailure(codeWindow.GetPrimaryView(out textView));
//This is our TextViewHost
//It transports us back into the land of WPF
IWpfTextViewHost textViewHost = _EditorAdapterFactory.GetWpfTextViewHost(textView);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Now we need to subset TextBuffer somehow...
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int length = end - start;
SnapshotSpan subsetSnapshot = new SnapshotSpan(textViewHost.TextView.TextSnapshot, start, length);
var CSharpType = _contentTypeRegistry.GetContentType("CSharp");
var projBuffer = _ProjectionBufferFactory.CreateElisionBuffer(
null
, new NormalizedSnapshotSpanCollection(subsetSnapshot)
, ElisionBufferOptions.None
,CSharpType);
IVsTextBuffer bufferAdapter = _EditorAdapterFactory.CreateVsTextBufferAdapterForSecondaryBuffer(VisualStudioServices.OLEServiceProvider, projBuffer);
//My attempt at getting syntax coloring to work:
Guid CSharpLanguageServiceId = new Guid("694DD9B6-B865-4C5B-AD85-86356E9C88DC");
IVsTextLines buffer = (IVsTextLines)bufferAdapter;
buffer.SetLanguageServiceID(ref CSharpLanguageServiceId);
IVsTextView projTextView = _EditorAdapterFactory.CreateVsTextViewAdapter(VisualStudioServices.OLEServiceProvider);
projTextView.Initialize(
(IVsTextLines)bufferAdapter
, IntPtr.Zero
, (uint)TextViewInitFlags.VIF_HSCROLL | (uint)TextViewInitFlags.VIF_VSCROLL | (uint)TextViewInitFlags3.VIF_NO_HWND_SUPPORT,
new[] { new INITVIEW { fSelectionMargin = 0, fWidgetMargin = 0, fVirtualSpace = 0, fDragDropMove = 0 } }
);
return _EditorAdapterFactory.GetWpfTextViewHost(projTextView);
}