Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace NServiceBus.Core.Analyzer.Fixes;

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Handlers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
Expand All @@ -12,7 +14,6 @@ namespace NServiceBus.Core.Analyzer.Fixes;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;
using NServiceBus.Core.Analyzer.Handlers;

[Shared]
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AddConventionBasedHandleMethodFixer))]
Expand All @@ -25,14 +26,19 @@ public class AddConventionBasedHandleMethodFixer : CodeFixProvider
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root is null)
{
return;
}

foreach (var diagnostic in context.Diagnostics)
if (EditorConfigSettings.KeyMatches(context.Document.Project.AnalyzerOptions, root.SyntaxTree, EditorConfigSettings.HandlerStyleKey, EditorConfigSettings.HandlerStyleInterfaces))
{
if (root?.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is not { } node)
{
continue;
}
return;
}

foreach (var diagnostic in context.Diagnostics)
{
var node = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
var classDecl = node.FirstAncestorOrSelf<ClassDeclarationSyntax>();
if (classDecl is null || !HandlerFixerGuards.IsEmptyHandlerShell(classDecl))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace NServiceBus.Core.Analyzer.Fixes;

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
Expand All @@ -25,14 +26,19 @@ public class AddIHandleMessagesInterfaceFixer : CodeFixProvider
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root is null)
{
return;
}

foreach (var diagnostic in context.Diagnostics)
if (EditorConfigSettings.KeyMatches(context.Document.Project.AnalyzerOptions, root.SyntaxTree, EditorConfigSettings.HandlerStyleKey, EditorConfigSettings.HandlerStyleConventions))
{
if (root?.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is not { } node)
{
continue;
}
return;
}

foreach (var diagnostic in context.Diagnostics)
{
var node = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
var classDecl = node.FirstAncestorOrSelf<ClassDeclarationSyntax>();
if (classDecl is null || !HandlerFixerGuards.IsEmptyHandlerShell(classDecl))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<ItemGroup>
<Compile Include="..\NServiceBus.Core.Analyzer\DiagnosticIds.cs" />
<Compile Include="..\NServiceBus.Core.Analyzer\EditorConfigSettings.cs" />
<Compile Include="..\NServiceBus.Core.Analyzer\Features\FeatureKnownTypes.cs" LinkBase="Features" />
<Compile Include="..\NServiceBus.Core.Analyzer\TypeSymbolExtensions.cs" />
<Compile Include="..\NServiceBus.Core.Analyzer\NamespaceSymbolExtensions.cs" />
Expand Down
36 changes: 36 additions & 0 deletions src/NServiceBus.Core.Analyzer/EditorConfigSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#nullable enable

namespace NServiceBus.Core.Analyzer;

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

static class EditorConfigSettings
{
/// <summary>
/// Get an editorconfig setting in an Analyzer. The AnalyzerOptions should be accessible from an Analyzer through <c>context.Options</c>
/// or in a CodeFixProvider from <c>context.Document.Project.AnalyzerOptions</c>.
/// </summary>
/// <returns></returns>
public static bool TryGetValue(AnalyzerOptions analyzerOptions, SyntaxTree? syntaxTree, string settingName, [NotNullWhen(true)] out string? value)
{
var provider = analyzerOptions.AnalyzerConfigOptionsProvider;

if (syntaxTree is null)
{
return provider.GlobalOptions.TryGetValue(settingName, out value);
}

var options = analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree);
return options.TryGetValue(settingName, out value);
}

public static bool KeyMatches(AnalyzerOptions analyzerOptions, SyntaxTree? syntaxTree, string settingName, string matchValue, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
=> TryGetValue(analyzerOptions, syntaxTree, settingName, out var value) && string.Equals(value, matchValue, comparison);

public const string HandlerStyleKey = "nservicebus_handler_style";
public const string HandlerStyleInterfaces = "interfaces";
public const string HandlerStyleConventions = "conventions";
}