带有 Intellisense 的自定义编辑器 [Visual Studio] [英] Custom Editor with Intellisense [Visual Studio]

查看:26
本文介绍了带有 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 层次结构,uint itemId, IntPtr docDataExisting, out IntPtr docView, out IntPtr docData,输出字符串 editorCaption,输出 Guid guidCommand,输出 int createWindowFlags){IComponentModel 模型 = _package.GetGlobalService() as IComponentModel;IVsEditorAdaptersFactoryService adapterService = model.GetService();ITextBufferFactoryServicebufferFactory bufferFactory = model.GetService();ITextEditorFactoryServiceeditorFactory editorFactory = model.GetService();IContentTypeRegistryServicetypeRegistry typeRegistry = model.GetService();IContentType contentType = typeRegistry.GetContentType("ContentType");ITextBuffer 缓冲区 = bufferFactory.CreateTextBuffer(contentType);IWpfTextView view = editorFactory.CreateTextView(buffer);IVsCodeWindow 窗口 = adapterService.CreateVsCodeWindowAdapter(_serviceProvider);//尝试打开编辑器时抛出异常//无法找到给定缓冲区的适配器"IVsTextLines lines = adapterService.GetBufferAdapter(buffer) as IVsTextLines;window.SetBuffer(lines);docView = Marshal.GetIUnknownForObject(window);docData = Marshal.GetIUnknownForObject(lines);guidCommand = VSConstants.GUID_TextEditorFactory;返回 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){类型 codeWindowType = typeof(IVsCodeWindow);Guid riid = codeWindowType.GUID;Guid clsid = typeof(VsCodeWindowClass).GUID;IVsCodeWindow 窗口 = (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;返回 Marshal.GetIUnknownForObject(window);}

I would like to have a custom editor that interacts with the newer MEF language services (e.g. IVsTextViewCreationListener, IIntellisenseController, etc).

I'm writing a Visual Studio extension for a custom language. I have Intellisense, syntax highlighting, and code completion working for a specific file extension, but there is no single extension for this language. So I would like these features to work with any file extension. I also have a custom editor derived from IVsEditorFactory so I can open any file with 'Open With...'.

In IVsEditorFactory.CreateEditorInstance I use the IVsEditorAdaptersFactoryService to create an IVsTextBuffer and IVsCodeWindow. This all works fine and I can open files in the editor, but I can't figure out how to have the MEF components apply Intellisense, sytnax highlighting, etc. to the custom editor.

One thing I tried...

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;
}

In the debugger I can see that my MEF components are now recognizing the new buffer and view. However, it appears that there is no way to convert an ITextBuffer into IVsTextLines.

解决方案

Based on your description, it seems that you want to create a custom language, which need to support Intellisense, syntax highlighting, and code completion working for a specific file extension. The following link provide a sample which support for Intellisense, syntax coloring, code completion, code snippets, custom project types and MSBuild integration.

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

I can see that my MEF components are now recognizing the new buffer and view, but I don't know how to put these into a IVsCodeWindow.

From the code, it uses the following method:

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);
        }

这篇关于带有 Intellisense 的自定义编辑器 [Visual Studio]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆