How to get IEditorOperations from IVsTextView?

倾然丶 夕夏残阳落幕 提交于 2019-12-25 09:11:21

问题


I'm developing my first Visual Studio (2015 Community) Command Menu and I'm trying to get access to IEditorOperations to delete text, send backspace etc. but I'm not sure how to. I can do:

var Service = Provider.GetService(typeof(IEditorOperationsFactoryService)) as IEditorOperationsFactoryService;
Service.GetEditorOperations(???);

I'm not sure what to pass in the ??? since I don't have access to an ITextView instead what I have is a IVsTExtView via:

IVsTextView View;
IVsTextManager Manager = (IVsTextManager)ServiceProvider.GetService(typeof(SVsTextManager));
int MustHaveFocus = 1;
Manager.GetActiveView(MustHaveFocus, null, out View);

When creating the Command Menu, VS generates a template for me with a private ctor creating the command service, binding it to the command set id etc. An overridden Initialize method, and a bunch of properties.

Any ideas?

EDIT: After help from Sergey, I managed to get a bit further. But now I get a null when I try to get the IEditorOperationsFactoryService, all the other values are valid.

static IEditorOperations GetEditorService(IServiceProvider Provider, IVsTextView VsView)
    {
        IEditorOperations Result;

        try
        {
            var Model = (IComponentModel)Provider.GetService(typeof(SComponentModel));
            var Editor = (IEditorOperationsFactoryService)Provider.GetService(typeof(IEditorOperationsFactoryService)); // returns null

            var Adaptor = Model.GetService<IVsEditorAdaptersFactoryService>();
            IWpfTextView TextView = Adaptor.GetWpfTextView(VsView);
            Result = Editor.GetEditorOperations(TextView);
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.ToString());
            Result = null;
        }

        return (Result);
    }

回答1:


You can get IEditorOperationsFactoryService instance from variable named Model, like this:

var Model = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));

var Editor = (IEditorOperationsFactoryService)Model.GetService<IEditorOperationsFactoryService>();



回答2:


You can get IWpfTextView (that implements ITextView) from IVsTextView using:

IVsTextView textView = ...;
IWpfTextView v = GetEditorAdaptersFactoryService().GetWpfTextView(textView);

private Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()
{
    Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel =
        (Microsoft.VisualStudio.ComponentModelHost.IComponentModel)serviceProvider.GetService(
            typeof(Microsoft.VisualStudio.ComponentModelHost.SComponentModel));
    return componentModel.GetService<Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService>();
}


来源:https://stackoverflow.com/questions/41206441/how-to-get-ieditoroperations-from-ivstextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!