How to get IEditorOperations from IVsTextView?
Asked Answered
R

2

1

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);
    }
Reagent answered 18/12, 2016 at 7:50 Comment(0)
N
2

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>();
Nb answered 23/12, 2016 at 5:36 Comment(1)
The generic version works for some reason but the non-generic one returns null. Thanks!Reagent
U
3

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>();
}
Unready answered 19/12, 2016 at 3:11 Comment(3)
Editor doesn't exist inside VisualStudio namepsace, nor ComponentModelHost. Do I need to reference a certain assembly?Reagent
IVsEditorAdaptersFactoryService is defined in Microsoft.VisualStudio.Editor.dll. IComponentModel is defined in Microsoft.VisualStudio.ComponentModelHost.dll.Unready
Now I get a null when getting IEditorOperationsFactoryService back from GetService. Please see edits.Reagent
N
2

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>();
Nb answered 23/12, 2016 at 5:36 Comment(1)
The generic version works for some reason but the non-generic one returns null. Thanks!Reagent

© 2022 - 2024 — McMap. All rights reserved.