【问题标题】:Custom Editor with Intellisense [Visual Studio]使用 Intellisense 的自定义编辑器 [Visual Studio]
【发布时间】:2016-11-02 14:37:04
【问题描述】:

我想要一个与较新的 MEF 语言服务交互的自定义​​编辑器(例如 IVsTextViewCreationListenerIIntellisenseController 等)。

我正在为自定义语言编写 Visual Studio 扩展。我有针对特定文件扩展名的智能感知、语法突出显示和代码完成功能,但这种语言没有单一的扩展名。所以我希望这些功能适用于任何文件扩展名。我还有一个源自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

【问题讨论】:

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


    【解决方案1】:

    根据您的描述,您似乎想要创建一种自定义语言,该语言需要支持智能感知、语法突出显示和代码完成,适用于特定文件扩展名。以下链接提供了一个支持 Intellisense、语法着色、代码完成、代码 sn-ps、自定义项目类型和 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);
            }
    

    【讨论】:

    • 其实问题是我想支持任何扩展。我已经尝试过类似CreateCodeView 方法的代码,但它似乎不适用于使用ITextBufferFactoryService 创建的缓冲区。我会用更多细节更新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    相关资源
    最近更新 更多