Skip to content

Commit 92a0b27

Browse files
committed
Implement a custom Fix All provider for SA1510
1 parent d84d836 commit 92a0b27

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1510CodeFixProvider.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.LayoutRules
55
{
66
using System.Collections.Immutable;
77
using System.Composition;
8+
using System.Linq;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.CodeAnalysis;
@@ -26,7 +27,7 @@ internal class SA1510CodeFixProvider : CodeFixProvider
2627
/// <inheritdoc/>
2728
public override FixAllProvider GetFixAllProvider()
2829
{
29-
return CustomFixAllProviders.BatchFixer;
30+
return FixAll.Instance;
3031
}
3132

3233
/// <inheritdoc/>
@@ -46,13 +47,31 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4647
}
4748

4849
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
50+
{
51+
var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
52+
return document.WithSyntaxRoot(newRoot);
53+
}
54+
55+
private static async Task<SyntaxNode> GetTransformedDocumentAsync(Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
4956
{
5057
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
58+
return syntaxRoot.ReplaceTokens(
59+
diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
60+
(originalToken, rewrittenToken) =>
61+
{
62+
return rewrittenToken.WithoutLeadingBlankLines();
63+
});
64+
}
65+
66+
private class FixAll : DocumentBasedFixAllProvider
67+
{
68+
public static FixAllProvider Instance { get; } =
69+
new FixAll();
5170

52-
var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
71+
protected override string CodeActionTitle => LayoutResources.SA1510CodeFix;
5372

54-
var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithoutLeadingBlankLines());
55-
return document.WithSyntaxRoot(newSyntaxRoot);
73+
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
74+
=> await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
5675
}
5776
}
5877
}

0 commit comments

Comments
 (0)