繁体   English   中英

具有Intellisense的自定义编辑器[Visual Studio]

[英]Custom Editor with Intellisense [Visual Studio]

我希望有一个与更新的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

根据您的描述,您似乎想要创建一种自定义语言,该语言需要支持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);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM