Skip to content

Add VSSDK003 Support async tool windows#25

Merged
AArnott merged 3 commits into
microsoft:masterfrom
AArnott:VSSDK003
Feb 16, 2018
Merged

Add VSSDK003 Support async tool windows#25
AArnott merged 3 commits into
microsoft:masterfrom
AArnott:VSSDK003

Conversation

@AArnott

@AArnott AArnott commented Feb 16, 2018

Copy link
Copy Markdown
Member

VSSDK003 Support async tool windows

Offer an async tool window factory for your tool windows so Visual Studio startup
is fast if the tool window is initially visible, and remains responsive when your
tool window is activated later in the session.

This analyzer only flags synchronous tool windows when proffered from an AsyncPackage-derived
VS package and when targeting Visual Studio 2017 Update 6 or later.

Examples of patterns that are flagged by this analyzer

[Guid("bd7b3e0c-f79e-46c1-8a04-12cbb8161ce5")]
public class ToolWindow1 : ToolWindowPane
{
    public ToolWindow1() : base(null)
    {
        this.Caption = "ToolWindow1";
        this.Content = new ToolWindow1Control();
    }
}

public class ToolWindow1Package : AsyncPackage
{
    // A lack of async tool window factory overrides
}

Solution

Override the async tool window factory methods on your AsyncPackage-derived class
and offer a constructor overload in your ToolWindowPane-derived class that accepts one parameter.

[Guid("bd7b3e0c-f79e-46c1-8a04-12cbb8161ce5")]
public class ToolWindow1 : ToolWindowPane
{
    public ToolWindow1() : base(null)
    {
        this.Caption = "ToolWindow1";
        this.Content = new ToolWindow1Control();
    }

    public ToolWindow1(string message)
        : this()
    {
    }
}

public class ToolWindow1Package : AsyncPackage
{
    public override IVsAsyncToolWindowFactory GetAsyncToolWindowFactory(Guid toolWindowType)
    {
        if (toolWindowType == typeof(ToolWindow1).GUID)
        {
            return this;
        }

        return base.GetAsyncToolWindowFactory(toolWindowType);
    }

    protected override string GetToolWindowTitle(Type toolWindowType, int id)
    {
        if (toolWindowType == typeof(ToolWindow1))
        {
            return "ToolWindow1 loading";
        }

        return base.GetToolWindowTitle(toolWindowType, id);
    }

    protected override async Task<object> InitializeToolWindowAsync(Type toolWindowType, int id, CancellationToken cancellationToken)
    {
        // potentially expensive work, preferably done on a background thread where possible.
        await Task.Delay(5000, cancellationToken);

        return "foo"; // this is passed to the tool window constructor
    }
}

Closes #21

@AArnott AArnott self-assigned this Feb 16, 2018
@codecov-io

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@756b32b). Click here to learn what that means.
The diff coverage is 97.95%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #25   +/-   ##
=========================================
  Coverage          ?   91.54%           
=========================================
  Files             ?        6           
  Lines             ?      213           
  Branches          ?        0           
=========================================
  Hits              ?      195           
  Misses            ?       18           
  Partials          ?        0
Impacted Files Coverage Δ
src/Microsoft.VisualStudio.SDK.Analyzers/Types.cs 91.66% <100%> (ø)
...nalyzers/VSSDK003SupportAsyncToolWindowAnalyzer.cs 97.82% <97.82%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 756b32b...cd1c258. Read the comment docs.

}

/// <summary>
/// Called to test a C# DiagnosticAnalyzer when applied on multiple inputted string as a source

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "...multiple inputted strings..."

@AArnott AArnott merged commit de9c7ab into microsoft:master Feb 16, 2018
@AArnott AArnott deleted the VSSDK003 branch February 16, 2018 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants