c# - 向 IElisonBuffer 添加语法突出显示

标签 c# visual-studio syntax-highlighting vspackage languageservice

我正在开发一个项目,我们将 C# 代码拆分为函数并将这些函数存储在 IElisonBuffers 中。我已经连接了 Intellisense,并且缓冲区与其他扩展交互良好,如下所示:

enter image description here

但是,我无法在这些编辑器中使用语法突出显示。

我通过以下步骤嵌入这些编辑器:

  1. 创建 IVsInvisibleEditor为文件。
  2. 获取IVsTextLines为此IVsInvisibleEditor
  3. 创建 IVsCodeWindow并为此设置缓冲区 IVsCodeWindow成为IVsTextLines来自IVsInvisibleEditor
  4. 获取IWpfTextViewHost从这个代码窗口。这让我回到了“WPF Land”,在这里我可以与传统的跨度进行交互。
  5. 创建 IWpfTextViewHost 的 SnapshotSpan的 TextView 。此 SnapshotSpan 包含一个函数。
  6. 创建 IElisionBuffer包含 SnapshotSpan。
  7. 创建 IVsTextBuffer通过IVsEditorAdaptersFactoryService.CreateVsTextBufferAdapterForSecondaryBuffer()传入IElisionBuffer .
  8. 现在我转换IVsTextBufferIVsTextLines并调用SetLanguageServiceID()传入 C# GUID:694DD9B6-B865-4C5B-AD85-86356E9C88DC。
  9. 我通过 GetLanguageServiceID() 仔细检查其设置是否正确一切看起来都很好。
  10. 我创建了一个IVsTextView并使用新的 IVsTextBuffer 对其进行初始化.
  11. 然后我得到 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/

相关文章:

c# - 如何修复 Visual Studio 2015 中的模糊字体

visual-studio - 如何将 Sublime Text 语法高亮文件转换为 visual studio 的 textmate 语法?

c# - 如何将任何对象类型转换为实际类型

C# - 如何在同一行中初始化 List<string[]>

c# - 使多个矩形或区域无效

visual-studio-2010 - VS.PHP自定义语法着色

c++ - Visual Studio (C++) 中自身类型的语法着色

c# - 自动完成 try catch(不是标准异常)

c# - SQL Server 列到组合框?

c++ - Visual Studio 2012 错误 MSB3073