我想要一個自定義編輯器,它可以與較新的MEF語言服務交互(例如IVsTextViewCreationListener
,IIntellisenseController
等)。使用Intellisense的自定義編輯器[Visual Studio]
我正在爲自定義語言編寫Visual Studio擴展。我有Intellisense,語法突出顯示和代碼完成工作的特定文件擴展名,但沒有單一的擴展名爲這種語言。所以我希望這些功能可以與任何文件擴展名一起使用。我也有一個從IVsEditorFactory
衍生出來的自定義編輯器,所以我可以用'Open With ...'打開任何文件。我使用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
。
其實,問題是,我想支持任何擴展。我嘗試過類似於'CreateCodeView'方法的代碼,但它似乎不適用於使用'ITextBufferFactoryService'創建的緩衝區。我會更詳細地更新這個問題。 –