Find an IVsTextView or IWpfTextView for a given ProjectItem, in VS 2010 RC extension
Asked Answered
L

3

14

I have the ProjectItem, and want to get the IWPFTextView that is associated with it, if any.

I have tried to get an IVsTextManager, and then iterate through the views, but iVsTextManager.EnumViews always returns nothing.

Here is what I've got so far:

var txtMgr = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));

if (txtMgr != null)
{
    IVsEnumTextViews iVsEnumTextViews;
    IVsTextView[] views = null;

    // Passing null will return all available views, at least according to the documentation
    // unfortunately, this returns a 0x80070057 error (invalid parameter)
    var errorValue = txtMgr.EnumViews(null, out iVsEnumTextViews);

    if (errorValue == VSConstants.S_OK)
    {
        // enumerate, find the IVsTextView with a matching filename.

Surely there is another/better way??

Thanks in advance

~ Cameron

Louis answered 9/3, 2010 at 23:56 Comment(0)
L
26

Here is how to do it. First get the full path for the project item, then call this helper method:

/// <summary>
/// Returns an IVsTextView for the given file path, if the given file is open in Visual Studio.
/// </summary>
/// <param name="filePath">Full Path of the file you are looking for.</param>
/// <returns>The IVsTextView for this file, if it is open, null otherwise.</returns>
internal static Microsoft.VisualStudio.TextManager.Interop.IVsTextView GetIVsTextView(string filePath)
{
    var dte2 = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
    Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte2;
    Microsoft.VisualStudio.Shell.ServiceProvider serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(sp);

    Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchy uiHierarchy;
    uint itemID;
    Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame windowFrame;
    Microsoft.VisualStudio.Text.Editor.IWpfTextView wpfTextView = null;
    if (Microsoft.VisualStudio.Shell.VsShellUtilities.IsDocumentOpen(serviceProvider, filePath, Guid.Empty,
                                    out uiHierarchy, out itemID, out windowFrame))
    {
        // Get the IVsTextView from the windowFrame.
        return Microsoft.VisualStudio.Shell.VsShellUtilities.GetTextView(windowFrame);
    }

    return null;
}
Louis answered 11/3, 2010 at 17:53 Comment(3)
And how do you get the wpfTextView?. Thanks!Bedell
Great. Thanks for this helper method. I really helped me a lot to solve similar problem.Hammer
Using your method, here is what I solved for myself: https://mcmap.net/q/827378/-how-to-get-current-activedocument-in-visual-studio-extension-using-mefHammer
A
15

I know it has been a while since this question has been answered, but hopefully the following code for retrieving the IWpfTextView from the IVsTextView will help someone else:

private IWpfTextView GetWpfTextView(IVsTextView vTextView)
{
    IWpfTextView view = null;
    IVsUserData userData = vTextView as IVsUserData;

    if (null != userData)
    {
        IWpfTextViewHost viewHost;
        object holder;
        Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
        userData.GetData(ref guidViewHost, out holder);
        viewHost = (IWpfTextViewHost)holder;
        view = viewHost.TextView;
    }

    return view;
}
Aegina answered 6/7, 2011 at 21:27 Comment(0)
K
0

You can get IWpfTextView like that:

var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
var textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
IVsTextView activeView = null;
ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out activeView));
var editorAdapter = componentModel.GetService<IVsEditorAdaptersFactoryService>();
IWpfTextView wpfTextView = editorAdapetr.GetWpfTextView(activeView);
Kahl answered 9/1, 2015 at 23:9 Comment(1)
I've actually seen this code not work 100% of the time. Try it on a .sql file. The selected answer has worked more consistently for me.Abaddon

© 2022 - 2024 — McMap. All rights reserved.