使用Intellisense的自定义编辑器[Visual Studio]

时间:2016-11-02 14:37:04

标签: c# visual-studio-2015 intellisense visual-studio-extensions

我希望有一个与较新的MEF语言服务互动的自定义编辑器(例如IVsTextViewCreationListenerIIntellisenseController等)。

我正在为自定义语言编写Visual Studio扩展。我有Intellisense,语法突出显示和代码完成工作的特定文件扩展名,但没有这种语言的单一扩展名。所以我希望这些功能适用于任何文件扩展名。我还有一个源自IVsEditorFactory的自定义编辑器,因此我可以使用“打开方式...”打开任何文件。

IVsEditorFactory.CreateEditorInstance中,我使用IVsEditorAdaptersFactoryService创建IVsTextBufferIVsCodeWindow。这一切都很好,我可以在编辑器中打开文件,但我无法弄清楚如何让MEF组件将Intellisense,sytnax高亮等应用于自定义编辑器。

我尝试了一件事......

public int CreateEditorInstance(uint createFlags, string documentMoniker, string physicalView,
  IVsHierarchy hierarchy,
  uint itemId, IntPtr docDataExisting, out IntPtr docView, out IntPtr docData,
  out string editorCaption, out Guid guidCommand, out int createWindowFlags)
{
  IComponentModel model = _package.GetGlobalService<SComponentModel>() as IComponentModel;
  IVsEditorAdaptersFactoryService adapterService = model.GetService<IVsEditorAdaptersFactoryService>();
  ITextBufferFactoryServicebufferFactory bufferFactory = model.GetService<ITextBufferFactoryService>();
  ITextEditorFactoryServiceeditorFactory editorFactory = model.GetService<ITextEditorFactoryService>();
  IContentTypeRegistryServicetypeRegistry typeRegistry = model.GetService<IContentTypeRegistryService>();
  IContentType contentType = typeRegistry.GetContentType("ContentType");
  ITextBuffer buffer = bufferFactory.CreateTextBuffer(contentType);
  IWpfTextView view = editorFactory.CreateTextView(buffer);

  IVsCodeWindow window = adapterService.CreateVsCodeWindowAdapter(_serviceProvider);

  // Throws exception when trying to open the editor
  // "Could not find adapter for the given buffer"
  IVsTextLines lines = adapterService.GetBufferAdapter(buffer) as IVsTextLines;
  window.SetBuffer(lines);
  docView = Marshal.GetIUnknownForObject(window);
  docData = Marshal.GetIUnknownForObject(lines);
  guidCommand = VSConstants.GUID_TextEditorFactory;
  return VSConstants.S_OK;
}

在调试器中我可以看到我的MEF组件现在正在识别新的缓冲区和视图。但是,似乎无法将ITextBuffer转换为IVsTextLines

1 个答案:

答案 0 :(得分:0)

根据您的描述,您似乎想要创建一种自定义语言,该语言需要支持Intellisense,语法突出显示以及为特定文件扩展名工作的代码完成。以下链接提供了一个支持Intellisense,语法着色,代码完成,代码片段,自定义项目类型和MSBuild集成的示例。

https://code.msdn.microsoft.com/IPyIntegration

  
    

我可以看到我的MEF组件现在正在识别新的缓冲区和视图,但我不知道如何将它们放入IVsCodeWindow。

  

从代码中,它使用以下方法:

private IntPtr CreateCodeView(IVsTextLines textLines, ref string editorCaption, ref Guid cmdUI)
        {
            Type codeWindowType = typeof(IVsCodeWindow);
            Guid riid = codeWindowType.GUID;
            Guid clsid = typeof(VsCodeWindowClass).GUID;
            IVsCodeWindow window = (IVsCodeWindow)package.CreateInstance(ref clsid, ref riid, codeWindowType);
            ErrorHandler.ThrowOnFailure(window.SetBuffer(textLines));
            ErrorHandler.ThrowOnFailure(window.SetBaseEditorCaption(null));
            ErrorHandler.ThrowOnFailure(window.GetEditorCaption(READONLYSTATUS.ROSTATUS_Unknown, out editorCaption));
            cmdUI = VSConstants.GUID_TextEditorFactory;
            return Marshal.GetIUnknownForObject(window);
        }