Community.VisualStudio.Toolkit icon indicating copy to clipboard operation
Community.VisualStudio.Toolkit copied to clipboard

Is returning a text view in an output pane an expected result for await VS.Documents.GetActiveDocumentViewAsync()?

Open martin-honnen opened this issue 2 years ago • 2 comments

I am rather new to extension development and the toolkit and I am trying to understand whether I use await VS.Documents.GetActiveDocumentViewAsync() correctly, for me, it seems, it sometimes, after e.g. ex.LogAsync(...) has written some details to the Extensions output pane and the Visual Studio user has looked at the pane, i.e. selected it, the await VS.Documents.GetActiveDocumentViewAsync() does not return null but rather a view to the output pane.

Is that supposed to happen?

I am thinking about safeguarding my extension code's attempt to use e.g. the FilePath of the returned document view with e.g.

var documentView = await VS.Documents.GetActiveDocumentViewAsync();

var fileName = documentView.FilePath;

PhysicalFile item = await PhysicalFile.FromFileAsync(fileName);

if (item == null)
{
    await VS.StatusBar.ShowMessageAsync("No document is selected/active. Make sure focus is not on e.g. the output pane.");
    return;
}

as it seems I don't get a physical file in the case of the output pane.

I am not sure, that is the right and necessary approach. Any advice appreciated.

martin-honnen avatar Apr 09 '22 20:04 martin-honnen

The output window is just another type of "document view" or "text view", (although it has some other unique characteristics, like being read-only), so if it has the focus, and you call GetActiveDocumentViewAsync(), then the output window's "document view" will be returned.

It sounds like what you would like to do is get the active editor. I'm not sure if there's a way to find the "active" editor, even if it doesn't have the focus, but a better way to check if the active document view is an editor would be to use the WindowFrame property. If the document view is an editor, then the WindowView.Editor will contain a non-empty GUID.

DocumentView? view = await VS.Documents.GetActiveDocumentViewAsync();
if (view is not null) {
    if (view.WindowFrame.Editor != Guid.Empty) {
        // This is an editor.
    } else {
        // This is some other type of window (WindowFrame.Guid should identify the type of the window).
    }
}

reduckted avatar Apr 10 '22 10:04 reduckted

I'm guessing this is related - using Community.VisualStudio.15 with Visual Studio 2019 ( I also target SSMS ) VS.Documents.GetActiveDocumentViewAsync() always return no FilePath (or Document) when the current Document ( ? editor ) is an ASPX file. I've tried various workarounds, but DTE2 dte = await VS.GetServiceAsync<EnvDTE.DTE, DTE2>(); dte.ActiveDocument is all I see that actually works. GetActiveDocumentViewAsync() seems to want a lot of caveats.

jefflomax avatar Sep 04 '22 23:09 jefflomax