【发布时间】:2016-11-02 14:37:04
【问题描述】:
我想要一个与较新的 MEF 语言服务交互的自定义编辑器(例如 IVsTextViewCreationListener、IIntellisenseController 等)。
我正在为自定义语言编写 Visual Studio 扩展。我有针对特定文件扩展名的智能感知、语法突出显示和代码完成功能,但这种语言没有单一的扩展名。所以我希望这些功能适用于任何文件扩展名。我还有一个源自IVsEditorFactory 的自定义编辑器,因此我可以使用“打开方式...”打开任何文件。
在IVsEditorFactory.CreateEditorInstance 中,我使用IVsEditorAdaptersFactoryService 创建IVsTextBuffer 和IVsCodeWindow。这一切都很好,我可以在编辑器中打开文件,但我不知道如何让 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