Skip to content

Commit f6a148e

Browse files
committed
Implement a custom Fix All provider for SA1509
1 parent 92a0b27 commit f6a148e

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1509CodeFixProvider.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal class SA1509CodeFixProvider : CodeFixProvider
2929
/// <inheritdoc/>
3030
public override FixAllProvider GetFixAllProvider()
3131
{
32-
return CustomFixAllProviders.BatchFixer;
32+
return FixAll.Instance;
3333
}
3434

3535
/// <inheritdoc/>
@@ -40,33 +40,41 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4040
context.RegisterCodeFix(
4141
CodeAction.Create(
4242
LayoutResources.SA1509CodeFix,
43-
token => this.GetTransformedDocumentAsync(context.Document, diagnostic, token),
43+
token => GetTransformedDocumentAsync(context.Document, diagnostic, token),
4444
nameof(SA1509CodeFixProvider)),
4545
diagnostic);
4646
}
4747

4848
return SpecializedTasks.CompletedTask;
4949
}
5050

51-
private async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
51+
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5252
{
53-
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
54-
55-
var openBrace = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
56-
var leadingTrivia = openBrace.LeadingTrivia;
53+
var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
54+
return document.WithSyntaxRoot(newRoot);
55+
}
5756

58-
var newTriviaList = SyntaxFactory.TriviaList();
57+
private static async Task<SyntaxNode> GetTransformedDocumentAsync(Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
58+
{
59+
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
60+
return syntaxRoot.ReplaceTokens(
61+
diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
62+
(originalToken, rewrittenToken) =>
63+
{
64+
var openBrace = rewrittenToken;
65+
var leadingTrivia = openBrace.LeadingTrivia;
5966

60-
var previousEmptyLines = this.GetPreviousEmptyLines(openBrace).ToList();
61-
newTriviaList = newTriviaList.AddRange(leadingTrivia.Except(previousEmptyLines));
67+
var newTriviaList = SyntaxFactory.TriviaList();
6268

63-
var newOpenBrace = openBrace.WithLeadingTrivia(newTriviaList);
64-
var newSyntaxRoot = syntaxRoot.ReplaceToken(openBrace, newOpenBrace);
69+
var previousEmptyLines = GetPreviousEmptyLines(openBrace).ToList();
70+
newTriviaList = newTriviaList.AddRange(leadingTrivia.Except(previousEmptyLines));
6571

66-
return document.WithSyntaxRoot(newSyntaxRoot);
72+
var newOpenBrace = openBrace.WithLeadingTrivia(newTriviaList);
73+
return newOpenBrace;
74+
});
6775
}
6876

69-
private IEnumerable<SyntaxTrivia> GetPreviousEmptyLines(SyntaxToken openBrace)
77+
private static IEnumerable<SyntaxTrivia> GetPreviousEmptyLines(SyntaxToken openBrace)
7078
{
7179
var result = new List<SyntaxTrivia>();
7280

@@ -92,5 +100,16 @@ private IEnumerable<SyntaxTrivia> GetPreviousEmptyLines(SyntaxToken openBrace)
92100

93101
return result;
94102
}
103+
104+
private class FixAll : DocumentBasedFixAllProvider
105+
{
106+
public static FixAllProvider Instance { get; } =
107+
new FixAll();
108+
109+
protected override string CodeActionTitle => LayoutResources.SA1509CodeFix;
110+
111+
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
112+
=> await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
113+
}
95114
}
96115
}

0 commit comments

Comments
 (0)