添加语法高亮IElisonBuffer [英] Add Syntax Highlighting to IElisonBuffer
问题描述
我的工作在哪里我们的 IElisonBuffers 。我有智能感知挂钩,并按下图所示的缓冲与精细其他扩展交互:
不过,我不能让语法高亮这些编辑器内工作。
我通过以下步骤嵌入这些编辑:
- 创建一个
IVsInvisibleEditor
该文件。 - 获得
IVsTextLines
此IVsInvisibleEditor
- 创建一个
IVsCodeWindow
并设置了缓冲IVsCodeWindow
是IVsTextLines
从IVsInvisibleEditor
- 从这个代码窗口获得
IWpfTextViewHost
。这使我回WPF土地里我能够与传统的跨度进行互动。 - 创建的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时
为了完整起见,这是我使用的代码:
公共CustomEditorViewModel CreateEditor(字符串文件路径,诠释开始,诠释完){
IVsInvisibleEditor invisibleEditor;
ErrorHandler.ThrowOnFailure(this._InvisibleEditorManager.RegisterInvisibleEditor(
文件路径
,pProject:空
,dwFlags中:(UINT)_EDITORREGFLAGS.RIEF_ENABLECACHING
,pFactory:空
,ppEditor:出invisibleEditor));
VAR docDataPointer = IntPtr.Zero;
的Guid = guidIVsTextLines typeof运算(IVsTextLines).GUID;
ErrorHandler.ThrowOnFailure(
invisibleEditor.GetDocData(
fEnsureWritable:1
,RIID:REF guidIVsTextLines
,ppDocData:出docDataPointer));
IVsTextLines docData =(IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer);
// Createa代码窗口适配器
VAR codeWindow = _EditorAdapterFactory.CreateVsCodeWindowAdapter(VisualStudioServices.OLEServiceProvider);
//我们的新代码窗口
ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(docData))大专我们IVsTextLines;
//获取我们对我们的编辑,我们将用它来获取承载编辑器中的WPF控件文本视图。
IVsTextView TextView的;
ErrorHandler.ThrowOnFailure(codeWindow.GetPrimaryView(出的TextView));
//这是我们TextViewHost
//它带我们回到WPF $ B $的土地b IWpfTextViewHost textViewHost = _EditorAdapterFactory.GetWpfTextViewHost(TextView的);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//现在我们需要子集TextBuffer莫名其妙...
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
INT长度=结束 - 启动;
SnapshotSpan subsetSnapshot =新SnapshotSpan(textViewHost.TextView.TextSnapshot,开始,长度);
VAR CSharpType = _contentTypeRegistry.GetContentType(CSHARP);
VAR projBuffer = _ProjectionBufferFactory.CreateElisionBuffer(
空
,新NormalizedSnapshotSpanCollection(subsetSnapshot)
,ElisionBufferOptions.None
,CSharpType);
IVsTextBuffer bufferAdapter = _EditorAdapterFactory.CreateVsTextBufferAdapterForSecondaryBuffer(VisualStudioServices.OLEServiceProvider,projBuffer);
//我在得到语法着色的尝试工作:
的Guid = CSharpLanguageServiceId新的GUID(694DD9B6-B865-4C5B-AD85-86356E9C88DC);
IVsTextLines缓冲=(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,
新的[] {新INITVIEW {fSelectionMargin = 0,fWidgetMargin = 0,fVirtualSpace = 0,fDragDropMove = 0}
);
返回_EditorAdapterFactory.GetWpfTextViewHost(projTextView);
}
请的内容类型的省音缓冲器是,或从内容类型投影派生。就是这样标注器应通过该项目的暗示。
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);
}
Make the content type of your elision buffer be, or derive from, the content type "projection". That's the hint that taggers should project through that.
这篇关于添加语法高亮IElisonBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!