0

我想要一個自定義編輯器,它可以與較新的MEF語言服務交互(例如IVsTextViewCreationListenerIIntellisenseController等)。使用Intellisense的自定義編輯器[Visual Studio]

我正在爲自定義語言編寫Visual Studio擴展。我有Intellisense,語法突出顯示和代碼完成工作的特定文件擴展名,但沒有單一的擴展名爲這種語言。所以我希望這些功能可以與任何文件擴展名一起使用。我也有一個從IVsEditorFactory衍生出來的自定義編輯器,所以我可以用'Open With ...'打開任何文件。我使用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

回答

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

其實,問題是,我想支持任何擴展。我嘗試過類似於'CreateCodeView'方法的代碼,但它似乎不適用於使用'ITextBufferFactoryService'創建的緩衝區。我會更詳細地更新這個問題。 –

相關問題