Skip to content

Commit

Permalink
Move DiagnosticIncrementalAnalzyer to EditorFeatures
Browse files Browse the repository at this point in the history
Refactor AnalyzerHelpers

Remove IIncementalAnalyzer2

Move DiagnosticService to LSP layer

Move DiagnosticIncrementalAnalyzer to LSP

Move *OptionsStorage types

Simplify CodeFixService

Move CodeFixService and CodeCleanupService to LSP layer

Include LSP layer in EditorFeatures test composition
  • Loading branch information
tmat committed Mar 8, 2022
1 parent 347cd02 commit 2a96692
Show file tree
Hide file tree
Showing 86 changed files with 444 additions and 356 deletions.
2 changes: 1 addition & 1 deletion src/Analyzers/Core/Analyzers/Analyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\AbstractFileHeaderDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\AbstractFileHeaderHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\FileHeader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AnalyzerHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AnalyzerOptionsExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DeserializationConstructorCheck.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DiagnosticHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)EnforceOnBuildValues.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;

#if CODE_STYLE
using TOption = Microsoft.CodeAnalysis.Options.IOption2;
Expand Down Expand Up @@ -45,7 +50,7 @@ public static IdeAnalyzerOptions From(OptionSet options, string language)
#endif
}

internal static partial class AnalyzerHelper
internal static partial class AnalyzerOptionsExtensions
{
public static IdeAnalyzerOptions GetIdeOptions(this AnalyzerOptions options)
#if CODE_STYLE
Expand All @@ -60,7 +65,7 @@ public static T GetOption<T>(this SemanticModelAnalysisContext context, Option2<
var syntaxTree = context.SemanticModel.SyntaxTree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxNodeAnalysisContext context, Option2<T> option)
Expand All @@ -69,7 +74,7 @@ public static T GetOption<T>(this SyntaxNodeAnalysisContext context, Option2<T>
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxTreeAnalysisContext context, Option2<T> option)
Expand All @@ -78,7 +83,7 @@ public static T GetOption<T>(this SyntaxTreeAnalysisContext context, Option2<T>
var syntaxTree = context.Tree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this OperationAnalysisContext context, Option2<T> option)
Expand All @@ -87,7 +92,7 @@ public static T GetOption<T>(this OperationAnalysisContext context, Option2<T> o
var syntaxTree = context.Operation.Syntax.SyntaxTree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SemanticModelAnalysisContext context, PerLanguageOption2<T> option, string? language)
Expand All @@ -96,7 +101,7 @@ public static T GetOption<T>(this SemanticModelAnalysisContext context, PerLangu
var syntaxTree = context.SemanticModel.SyntaxTree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, language, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxNodeAnalysisContext context, PerLanguageOption2<T> option, string? language)
Expand All @@ -105,7 +110,7 @@ public static T GetOption<T>(this SyntaxNodeAnalysisContext context, PerLanguage
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, language, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxTreeAnalysisContext context, PerLanguageOption2<T> option, string? language)
Expand All @@ -114,7 +119,7 @@ public static T GetOption<T>(this SyntaxTreeAnalysisContext context, PerLanguage
var syntaxTree = context.Tree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, language, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this OperationAnalysisContext context, PerLanguageOption2<T> option, string? language)
Expand All @@ -123,7 +128,7 @@ public static T GetOption<T>(this OperationAnalysisContext context, PerLanguageO
var syntaxTree = context.Operation.Syntax.SyntaxTree;
var cancellationToken = context.CancellationToken;

return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
return analyzerOptions.GetOption(option, language, syntaxTree, cancellationToken);
}

public static bool TryGetEditorConfigOption<T>(this AnalyzerOptions analyzerOptions, TOption option, SyntaxTree syntaxTree, [MaybeNullWhen(false)] out T value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override void Initialize(AnalysisContext context)

[PerformanceSensitive(
"https://github.com/dotnet/roslyn/issues/23583",
Constraint = nameof(AnalyzerHelper.GetOption) + " is expensive and should be avoided if a syntax-based fast path exists.")]
Constraint = nameof(AnalyzerOptionsExtensions.GetOption) + " is expensive and should be avoided if a syntax-based fast path exists.")]
private void AnalyzeNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol formatProviderType)
{
var syntaxFacts = GetSyntaxFacts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.CodeAnalysis.Diagnostics
{
internal static partial class AnalyzerHelper
internal static partial class AnalyzerOptionsExtensions
{
#pragma warning disable IDE0060 // Remove unused parameter - Needed to share this method signature between CodeStyle and Features layer.
public static T GetOption<T>(this AnalyzerOptions analyzerOptions, IOption2 option, string? language, SyntaxTree syntaxTree, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public async Task TestDiagnosticSpan()
var compilerEngineCompilation = (CSharpCompilation)(await compilerEngineWorkspace.CurrentSolution.Projects.Single().GetRequiredCompilationAsync(CancellationToken.None));

var diagnostics = compilerEngineCompilation.GetAnalyzerDiagnostics(new[] { analyzer });
AssertEx.Any(diagnostics, d => d.Id == AnalyzerHelper.AnalyzerExceptionDiagnosticId);
AssertEx.Any(diagnostics, d => d.Id == DocumentAnalysisExecutor.AnalyzerExceptionDiagnosticId);
}

private class InvalidSpanAnalyzer : DiagnosticAnalyzer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ private async Task AnalyzeAsync()
await incrementalAnalyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, _source.Token).ConfigureAwait(false);
await incrementalAnalyzer.AnalyzeDocumentAsync(document, bodyOpt: null, reasons: InvocationReasons.Empty, cancellationToken: _source.Token).ConfigureAwait(false);
}
else if (incrementalAnalyzer is IIncrementalAnalyzer2 incrementalAnalyzer2)
else
{
await incrementalAnalyzer2.AnalyzeNonSourceDocumentAsync(textDocument, InvocationReasons.Empty, _source.Token).ConfigureAwait(false);
await incrementalAnalyzer.AnalyzeNonSourceDocumentAsync(textDocument, InvocationReasons.Empty, _source.Token).ConfigureAwait(false);
}

// don't call project one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void CanCreateDiagnosticForAnalyzerLoadFailure(

// Ensure CreateAnalyzerLoadFailureDiagnostic doesn't fail when called. We don't assert much about the resulting
// diagnostic -- this is primarly to ensure we don't forget to update it if a new error code is added.
var diagnostic = AnalyzerHelper.CreateAnalyzerLoadFailureDiagnostic(eventArgs, "Analyzer.dll", null, languageName);
var diagnostic = DocumentAnalysisExecutor.CreateAnalyzerLoadFailureDiagnostic(eventArgs, "Analyzer.dll", null, languageName);
Assert.Equal(languageName, diagnostic.Language);

if (expectsTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics
[UseExportProvider]
public class DiagnosticAnalyzerServiceTests
{
private static readonly TestComposition s_featuresCompositionWithMockDiagnosticUpdateSourceRegistrationService = FeaturesTestCompositions.Features
private static readonly TestComposition s_featuresCompositionWithMockDiagnosticUpdateSourceRegistrationService = EditorTestCompositions.EditorFeatures
.AddExcludedPartTypes(typeof(IDiagnosticUpdateSourceRegistrationService))
.AddParts(typeof(MockDiagnosticUpdateSourceRegistrationService))
.AddParts(typeof(TestDocumentTrackingService));
Expand Down Expand Up @@ -1246,9 +1246,9 @@ private static async Task RunAllAnalysisAsync(IIncrementalAnalyzer analyzer, Tex
await analyzer.AnalyzeSyntaxAsync(document, InvocationReasons.Empty, CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeDocumentAsync(document, bodyOpt: null, reasons: InvocationReasons.Empty, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}
else if (analyzer is IIncrementalAnalyzer2 analyzer2)
else
{
await analyzer2.AnalyzeNonSourceDocumentAsync(textDocument, InvocationReasons.Empty, CancellationToken.None).ConfigureAwait(false);
await analyzer.AnalyzeNonSourceDocumentAsync(textDocument, InvocationReasons.Empty, CancellationToken.None).ConfigureAwait(false);
}

await analyzer.AnalyzeProjectAsync(textDocument.Project, semanticsChanged: true, reasons: InvocationReasons.Empty, cancellationToken: CancellationToken.None).ConfigureAwait(false);
Expand Down
6 changes: 3 additions & 3 deletions src/EditorFeatures/Test/Diagnostics/DiagnosticServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static DiagnosticService GetDiagnosticService(TestWorkspace workspace)
[Fact, Trait(Traits.Feature, Traits.Features.Diagnostics)]
public async Task TestGetDiagnostics1()
{
using var workspace = new TestWorkspace(composition: FeaturesTestCompositions.Features);
using var workspace = new TestWorkspace(composition: EditorTestCompositions.EditorFeatures);
var mutex = new ManualResetEvent(false);
var document = workspace.CurrentSolution.AddProject("TestProject", "TestProject", LanguageNames.CSharp).AddDocument("TestDocument", string.Empty);

Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task TestGetDiagnostics1()
[Fact, Trait(Traits.Feature, Traits.Features.Diagnostics)]
public async Task TestGetDiagnostics2()
{
using var workspace = new TestWorkspace(composition: FeaturesTestCompositions.Features);
using var workspace = new TestWorkspace(composition: EditorTestCompositions.EditorFeatures);
var mutex = new ManualResetEvent(false);
var document = workspace.CurrentSolution.AddProject("TestProject", "TestProject", LanguageNames.CSharp).AddDocument("TestDocument", string.Empty);
var document2 = document.Project.AddDocument("TestDocument2", string.Empty);
Expand Down Expand Up @@ -109,7 +109,7 @@ public async Task TestGetDiagnostics2()
[Fact, Trait(Traits.Feature, Traits.Features.Diagnostics)]
public async Task TestCleared()
{
using var workspace = new TestWorkspace(composition: FeaturesTestCompositions.Features);
using var workspace = new TestWorkspace(composition: EditorTestCompositions.EditorFeatures);
var mutex = new ManualResetEvent(false);
var document = workspace.CurrentSolution.AddProject("TestProject", "TestProject", LanguageNames.CSharp).AddDocument("TestDocument", string.Empty);
var document2 = document.Project.AddDocument("TestDocument2", string.Empty);
Expand Down
20 changes: 19 additions & 1 deletion src/EditorFeatures/Test/SolutionCrawler/WorkCoordinatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,7 @@ internal static class Metadata
public static readonly IncrementalAnalyzerProviderMetadata Crawler = new IncrementalAnalyzerProviderMetadata(new Dictionary<string, object> { { "WorkspaceKinds", new[] { SolutionCrawlerWorkspaceKind } }, { "HighPriorityForActiveFile", false }, { "Name", "TestAnalyzer" } });
}

private class Analyzer : IIncrementalAnalyzer2
private class Analyzer : IIncrementalAnalyzer
{
public static readonly Option2<bool> TestOption = new Option2<bool>("TestOptions", "TestOption", defaultValue: true);

Expand Down Expand Up @@ -1776,6 +1776,13 @@ public Task NonSourceDocumentCloseAsync(TextDocument textDocument, CancellationT

public Task NonSourceDocumentResetAsync(TextDocument textDocument, CancellationToken cancellationToken)
=> Task.CompletedTask;

public void LogAnalyzerCountSummary()
{
}

public int Priority => 1;

#endregion
}

Expand All @@ -1800,6 +1807,17 @@ public Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt, Invocati
public Task AnalyzeProjectAsync(Project project, bool semanticsChanged, InvocationReasons reasons, CancellationToken cancellationToken) => Task.CompletedTask;
public Task RemoveDocumentAsync(DocumentId documentId, CancellationToken cancellationToken) => Task.CompletedTask;
public Task RemoveProjectAsync(ProjectId projectId, CancellationToken cancellationToken) => Task.CompletedTask;
public Task NonSourceDocumentOpenAsync(TextDocument textDocument, CancellationToken cancellationToken) => Task.CompletedTask;
public Task NonSourceDocumentCloseAsync(TextDocument textDocument, CancellationToken cancellationToken) => Task.CompletedTask;
public Task NonSourceDocumentResetAsync(TextDocument textDocument, CancellationToken cancellationToken) => Task.CompletedTask;
public Task AnalyzeNonSourceDocumentAsync(TextDocument textDocument, InvocationReasons reasons, CancellationToken cancellationToken) => Task.CompletedTask;

public void LogAnalyzerCountSummary()
{
}

public int Priority => 1;

#endregion
}

Expand Down
10 changes: 3 additions & 7 deletions src/EditorFeatures/TestUtilities/EditorTestCompositions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public static class EditorTestCompositions
typeof(TextEditorResources).Assembly,
typeof(EditorFeaturesResources).Assembly,
typeof(CSharp.CSharpEditorResources).Assembly,
typeof(VisualBasic.VBEditorResources).Assembly);
typeof(VisualBasic.VBEditorResources).Assembly,
typeof(LanguageServerResources).Assembly);

public static readonly TestComposition EditorFeaturesWpf = EditorFeatures
.AddAssemblies(
Expand All @@ -76,11 +77,6 @@ public static class EditorTestCompositions
.AddParts(
typeof(TestInteractiveWindowEditorFactoryService));

public static readonly TestComposition LanguageServerProtocol = EditorFeatures
.AddAssemblies(
typeof(LanguageServerResources).Assembly);

public static readonly TestComposition LanguageServerProtocolWpf = EditorFeaturesWpf
.AddAssemblies(LanguageServerProtocol.Assemblies);
public static readonly TestComposition LanguageServerProtocol = EditorFeatures;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ namespace Roslyn.Test.Utilities
[UseExportProvider]
public abstract partial class AbstractLanguageServerProtocolTests
{
// TODO: remove WPF dependency (IEditorInlineRenameService)
private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocolWpf
private static readonly TestComposition s_composition = EditorTestCompositions.LanguageServerProtocol
.AddParts(typeof(TestDocumentTrackingService))
.AddParts(typeof(TestWorkspaceRegistrationService))
.AddParts(typeof(TestSyntaxTreeConfigurationService))
Expand Down

This file was deleted.

Loading

0 comments on commit 2a96692

Please sign in to comment.