我正在开发一个项目,我们将 C# 代码拆分为函数并将这些函数存储在 IElisonBuffers 中。我已经连接了 Intellisense,并且缓冲区与其他扩展交互良好,如下所示:
但是,我无法在这些编辑器中使用语法突出显示。
我通过以下步骤嵌入这些编辑器:
- 创建
IVsInvisibleEditor
为文件。 - 获取
IVsTextLines
为此IVsInvisibleEditor
- 创建
IVsCodeWindow
并为此设置缓冲区IVsCodeWindow
成为IVsTextLines
来自IVsInvisibleEditor
- 获取
IWpfTextViewHost
从这个代码窗口。这让我回到了“WPF Land”,在这里我可以与传统的跨度进行交互。 - 创建
IWpfTextViewHost
的 SnapshotSpan的 TextView 。此 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);
}
最佳答案
使省略缓冲区的内容类型成为或派生自内容类型“投影”。这就是标记者应该通过它来转换的暗示。
关于c# - 向 IElisonBuffer 添加语法突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22085159/