向 IElisonBuffer 添加语法突出显示
-
02-01-2020 - |
题
我正在开发一个项目,我们将 C# 代码拆分为函数并将这些函数存储在 IEElisonBuffers. 。我已经连接了 Intellisense,并且缓冲区与其他扩展交互良好,如下所示:
但是,我无法在这些编辑器中使用语法突出显示。
我通过以下步骤嵌入这些编辑器:
- 创建一个
IVsInvisibleEditor
对于该文件。 - 获取
IVsTextLines
为了这IVsInvisibleEditor
- 创建一个
IVsCodeWindow
并为此设置缓冲区IVsCodeWindow
成为IVsTextLines
来自IVsInvisibleEditor
- 得到一个
IWpfTextViewHost
从这个代码窗口。这让我回到了“WPF Land”,在这里我能够与传统的跨度进行交互。 - 创建 SnapshotSpan
IWpfTextViewHost
的文本视图。此 SnapshotSpan 包含一个函数。 - 创建一个
IElisionBuffer
包含 SnapshotSpan。 - 创建一个
IVsTextBuffer
通过IVsEditorAdaptersFactoryService.CreateVsTextBufferAdapterForSecondaryBuffer()
通过在IElisionBuffer
. - 现在我投了
IVsTextBuffer
到IVsTextLines
并打电话SetLanguageServiceID()
传入 C# GUID:694DD9B6-B865-4C5B-AD85-86356E9C88DC。 - 我仔细检查它是否设置正确
GetLanguageServiceID()
一切看起来都很好。 - 我创建一个
IVsTextView
并用新的初始化它IVsTextBuffer
. - 然后我得到
IWpfTextViewHost
为了这IVsTextView
.
为 IElisionBuffer 设置语言服务 ID 时是否需要注意任何特殊步骤?
为了完整起见,这是我正在使用的代码:
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);
}
解决方案
使省略缓冲区的内容类型成为内容类型“投影”或派生自内容类型“投影”。这就是标记者应该通过它来投射的暗示。
不隶属于 StackOverflow