Skip to content

Commit

Permalink
Make use of InvocationReason in tests and analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
drognanar committed May 16, 2016
1 parent 7357c7b commit 1b6a83f
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static async Task TestAsync(string codeWithMarker)
var document = workspace.Documents.First();
var documentId = document.Id;
var reasons = new InvocationReasons(PredefinedInvocationReasons.DocumentAdded);
await worker.AnalyzeSyntaxAsync(workspace.CurrentSolution.GetDocument(documentId), CancellationToken.None);
await worker.AnalyzeSyntaxAsync(workspace.CurrentSolution.GetDocument(documentId), InvocationReasons.Empty, CancellationToken.None);

var todoLists = worker.GetItems_TestingOnly(documentId);
var expectedLists = document.SelectedSpans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
Expand All @@ -31,9 +32,9 @@ public async Task TestHasSuccessfullyLoadedBeingFalse()
service.DiagnosticsUpdated += (s, a) => Assert.Empty(a.Diagnostics);

// now call each analyze method. none of them should run.
await analyzer.AnalyzeSyntaxAsync(document, CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeDocumentAsync(document, bodyOpt: null, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeProjectAsync(document.Project, semanticsChanged: true, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeDocumentAsync(document, bodyOpt: null, reasons: InvocationReasons.Empty, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeProjectAsync(document.Project, semanticsChanged: true, reasons: InvocationReasons.Empty, cancellationToken: CancellationToken.None).ConfigureAwait(false);

// wait for all events to raised
await listener.CreateWaitTask().ConfigureAwait(false);
Expand Down Expand Up @@ -74,9 +75,9 @@ public async Task TestHasSuccessfullyLoadedBeingFalseWhenFileOpened()
};

// now call each analyze method. none of them should run.
await analyzer.AnalyzeSyntaxAsync(document, CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeDocumentAsync(document, bodyOpt: null, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeProjectAsync(document.Project, semanticsChanged: true, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeDocumentAsync(document, bodyOpt: null, reasons: InvocationReasons.Empty, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeProjectAsync(document.Project, semanticsChanged: true, reasons: InvocationReasons.Empty, cancellationToken: CancellationToken.None).ConfigureAwait(false);

// wait for all events to raised
await listener.CreateWaitTask().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,13 +1039,13 @@ public Analyzer(bool waitForCancellation = false, bool blockedRun = false)
this.RunningEvent = new ManualResetEventSlim(initialState: false);
}

public Task AnalyzeProjectAsync(Project project, bool semanticsChanged, CancellationToken cancellationToken)
public Task AnalyzeProjectAsync(Project project, bool semanticsChanged, InvocationReasons reasons, CancellationToken cancellationToken)
{
this.ProjectIds.Add(project.Id);
return SpecializedTasks.EmptyTask;
}

public Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt, CancellationToken cancellationToken)
public Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt, InvocationReasons reasons, CancellationToken cancellationToken)
{
if (bodyOpt == null)
{
Expand All @@ -1055,7 +1055,7 @@ public Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt, Cancella
return SpecializedTasks.EmptyTask;
}

public Task AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
public Task AnalyzeSyntaxAsync(Document document, InvocationReasons reasons, CancellationToken cancellationToken)
{
this.SyntaxDocumentIds.Add(document.Id);
Process(document.Id, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Imports Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Shared.Options
Imports Microsoft.CodeAnalysis.SolutionCrawler
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.UnitTests.Diagnostics
Expand Down Expand Up @@ -1937,7 +1938,7 @@ class MyClass
Assert.Equal(HiddenDiagnosticsCompilationAnalyzer.Descriptor.Id, descriptors.Single().Id)

' Force project analysis
incrementalAnalyzer.AnalyzeProjectAsync(project, semanticsChanged:=True, cancellationToken:=CancellationToken.None).Wait()
incrementalAnalyzer.AnalyzeProjectAsync(project, semanticsChanged:=True, reasons:=InvocationReasons.Empty, cancellationToken:=CancellationToken.None).Wait()

' Get cached project diagnostics.
Dim diagnostics = diagnosticService.GetCachedDiagnosticsAsync(workspace, project.Id).WaitAndGetResult(CancellationToken.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.TodoComment

Dim document = workspace.Documents.First()
Dim documentId = document.Id
Await worker.AnalyzeSyntaxAsync(workspace.CurrentSolution.GetDocument(documentId), CancellationToken.None)
Await worker.AnalyzeSyntaxAsync(workspace.CurrentSolution.GetDocument(documentId), InvocationReasons.Empty, CancellationToken.None)

Dim todoLists = worker.GetItems_TestingOnly(documentId)

Expand Down
3 changes: 0 additions & 3 deletions src/Features/Core/Portable/Features.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,9 @@
<Compile Include="SolutionCrawler\IncrementalAnalyzerBase.cs" />
<Compile Include="SolutionCrawler\IncrementalAnalyzerProviderBase.cs" />
<Compile Include="SolutionCrawler\InternalSolutionCrawlerOptionsProvider.cs" />
<Compile Include="SolutionCrawler\InvocationReasons.cs" />
<Compile Include="SolutionCrawler\InvocationReasons_Constants.cs" />
<Compile Include="SolutionCrawler\ISolutionCrawlerProgressReporter.cs" />
<Compile Include="SolutionCrawler\ISolutionCrawlerService.cs" />
<Compile Include="SolutionCrawler\IWorkCoordinatorPriorityService.cs" />
<Compile Include="SolutionCrawler\PredefinedInvocationReasons.cs" />
<Compile Include="SolutionCrawler\SolutionCrawlerLogger.cs" />
<Compile Include="SolutionCrawler\InternalSolutionCrawlerOptions.cs" />
<Compile Include="SolutionCrawler\SolutionCrawlerProgressReporter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ private async Task ResetStatesAsync()
_currentSnapshotVersionTrackingSet.Clear();

_processingSolution = currentSolution;

await RunAnalyzersAsync(this.Analyzers, currentSolution, (a, s, c) => a.NewSolutionSnapshotAsync(s, c), this.CancellationToken).ConfigureAwait(false);

foreach (var id in this.Processor.GetOpenDocumentIds())
Expand Down
3 changes: 1 addition & 2 deletions src/VisualStudio/CSharp/Test/CSharpVisualStudioTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\..\..\Features\Core\Portable\Features.csproj">
<Project>{EDC68A0E-C68D-4A74-91B7-BF38EC909888}</Project>
<Project>{edc68a0e-c68d-4a74-91b7-bf38ec909888}</Project>
<Name>Features</Name>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServices.Implementation.SolutionSize;
using Xunit;
Expand Down Expand Up @@ -46,7 +47,7 @@ public async Task Test_SolutionSize_Update()
var text = SourceText.From(new string('2', 1000));
var newDocument = document.WithText(text);

await analyzer.AnalyzeSyntaxAsync(newDocument, CancellationToken.None);
await analyzer.AnalyzeSyntaxAsync(newDocument, InvocationReasons.DocumentChanged, CancellationToken.None);

var size = analyzer.GetSolutionSize(solution.Id);
Assert.Equal(expected - length + text.Length, size);
Expand Down Expand Up @@ -78,7 +79,7 @@ private static async Task AddSolutionAsync(SolutionSizeTracker.IncrementalAnalyz
{
foreach (var document in solution.Projects.SelectMany(p => p.Documents))
{
await analyzer.AnalyzeSyntaxAsync(document, CancellationToken.None);
await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Concurrent;
using System.Composition;
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Analyzer(IForegroundNotificationService notificationService, IAsynchronou
_workspace = workspace;
}

public Task AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
public Task AnalyzeSyntaxAsync(Document document, InvocationReasons reasons, CancellationToken cancellationToken)
{
FireEvents(document.Id, cancellationToken);

Expand Down Expand Up @@ -124,12 +124,12 @@ public bool NeedsReanalysisOnOptionChanged(object sender, OptionChangedEventArgs
return false;
}

public Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt, CancellationToken cancellationToken)
public Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt, InvocationReasons reasons, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyTask;
}

public Task AnalyzeProjectAsync(Project project, bool semanticsChanged, CancellationToken cancellationToken)
public Task AnalyzeProjectAsync(Project project, bool semanticsChanged, InvocationReasons reasons, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Imports Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Shared.TestHooks
Imports Microsoft.CodeAnalysis.SolutionCrawler
Imports Microsoft.CodeAnalysis.Text.Shared.Extensions
Imports Microsoft.VisualStudio.LanguageServices.Implementation.Diagnostics
Imports Microsoft.VisualStudio.Text.Tagging
Expand Down Expand Up @@ -47,7 +48,7 @@ class 123 { }
Dim tagger = provider.CreateTagger(Of IErrorTag)(buffer)
Using disposable = TryCast(tagger, IDisposable)
Dim analyzer = miscService.CreateIncrementalAnalyzer(workspace)
Await analyzer.AnalyzeSyntaxAsync(workspace.CurrentSolution.Projects.First().Documents.First(), CancellationToken.None)
Await analyzer.AnalyzeSyntaxAsync(workspace.CurrentSolution.Projects.First().Documents.First(), InvocationReasons.Empty, CancellationToken.None)

Await listener.CreateWaitTask()

Expand Down Expand Up @@ -87,8 +88,8 @@ class A

Dim document = workspace.CurrentSolution.Projects.First().Documents.First()
Dim analyzer = miscService.CreateIncrementalAnalyzer(workspace)
Await analyzer.AnalyzeSyntaxAsync(document, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, CancellationToken.None)
Await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, InvocationReasons.Empty, CancellationToken.None)

Await listener.CreateWaitTask()

Expand Down Expand Up @@ -124,8 +125,8 @@ class A

Dim document = workspace.CurrentSolution.Projects.First().Documents.First()
Dim analyzer = miscService.CreateIncrementalAnalyzer(workspace)
Await analyzer.AnalyzeSyntaxAsync(document, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, CancellationToken.None)
Await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, InvocationReasons.Empty, CancellationToken.None)

Await listener.CreateWaitTask()

Expand Down Expand Up @@ -161,8 +162,8 @@ class A

Dim document = workspace.CurrentSolution.Projects.First().Documents.First()
Dim analyzer = miscService.CreateIncrementalAnalyzer(workspace)
Await analyzer.AnalyzeSyntaxAsync(document, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, CancellationToken.None)
Await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, InvocationReasons.Empty, CancellationToken.None)

Await listener.CreateWaitTask()

Expand Down Expand Up @@ -198,8 +199,8 @@ class A

Dim document = workspace.CurrentSolution.Projects.First().Documents.First()
Dim analyzer = miscService.CreateIncrementalAnalyzer(workspace)
Await analyzer.AnalyzeSyntaxAsync(document, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, CancellationToken.None)
Await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None)
Await analyzer.AnalyzeDocumentAsync(document, Nothing, InvocationReasons.Empty, CancellationToken.None)

analyzer.RemoveDocument(document.Id)
Await listener.CreateWaitTask()
Expand Down Expand Up @@ -227,7 +228,7 @@ class 123 { }
End Sub

Dim analyzer = miscService.CreateIncrementalAnalyzer(workspace)
Await analyzer.AnalyzeSyntaxAsync(workspace.CurrentSolution.Projects.First().Documents.First(), CancellationToken.None)
Await analyzer.AnalyzeSyntaxAsync(workspace.CurrentSolution.Projects.First().Documents.First(), InvocationReasons.Empty, CancellationToken.None)

Assert.Equal(PredefinedBuildTools.Live, buildTool)
End Using
Expand All @@ -252,7 +253,7 @@ End Class
End Sub

Dim analyzer = miscService.CreateIncrementalAnalyzer(workspace)
Await analyzer.AnalyzeSyntaxAsync(workspace.CurrentSolution.Projects.First().Documents.First(), CancellationToken.None)
Await analyzer.AnalyzeSyntaxAsync(workspace.CurrentSolution.Projects.First().Documents.First(), InvocationReasons.Empty, CancellationToken.None)

Assert.Equal(PredefinedBuildTools.Live, buildTool)
End Using
Expand Down
3 changes: 3 additions & 0 deletions src/Workspaces/Core/Portable/Workspaces.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@
<Compile Include="SolutionCrawler\IIncrementalAnalyzer.cs" />
<Compile Include="SolutionCrawler\IIncrementalAnalyzerProvider.cs" />
<Compile Include="SolutionCrawler\IncrementalAnalyzerProviderMetadata.cs" />
<Compile Include="SolutionCrawler\InvocationReasons.cs" />
<Compile Include="SolutionCrawler\InvocationReasons_Constants.cs" />
<Compile Include="SolutionCrawler\ISolutionCrawlerRegistrationService.cs" />
<Compile Include="Diagnostics\IWorkspaceVenusSpanMappingService.cs" />
<Compile Include="Diagnostics\WellKnownDiagnosticPropertyNames.cs" />
Expand Down Expand Up @@ -415,6 +417,7 @@
<Compile Include="Options\DocumentOptionSet.cs" />
<Compile Include="Options\OptionSet.cs" />
<Compile Include="Packaging\IPackageInstallerService.cs" />
<Compile Include="SolutionCrawler\PredefinedInvocationReasons.cs" />
<Compile Include="SymbolSearch\ISymbolSearchService.cs" />
<Compile Include="FindSymbols\SymbolTree\ISymbolTreeInfoCacheService.cs" />
<Compile Include="FindSymbols\FindReferences\MetadataUnifyingEquivalenceComparer.cs" />
Expand Down

0 comments on commit 1b6a83f

Please sign in to comment.