Skip to content

Commit ed1b03a

Browse files
authored
Merge pull request #2731 from pdelvo/morefixall
Add two more fix all providers
2 parents c4bf6c4 + d6424c2 commit ed1b03a

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class FileHeaderCodeFixProvider : CodeFixProvider
4747
/// <inheritdoc/>
4848
public override FixAllProvider GetFixAllProvider()
4949
{
50-
return CustomFixAllProviders.BatchFixer;
50+
return FixAll.Instance;
5151
}
5252

5353
/// <inheritdoc/>
@@ -67,6 +67,11 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
6767
}
6868

6969
private static async Task<Document> GetTransformedDocumentAsync(Document document, CancellationToken cancellationToken)
70+
{
71+
return document.WithSyntaxRoot(await GetTransformedSyntaxRootAsync(document, cancellationToken).ConfigureAwait(false));
72+
}
73+
74+
private static async Task<SyntaxNode> GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken)
7075
{
7176
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
7277
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
@@ -96,7 +101,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
96101
}
97102
}
98103

99-
return document.WithSyntaxRoot(newSyntaxRoot);
104+
return newSyntaxRoot;
100105
}
101106

102107
private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document document, SyntaxNode root, StyleCopSettings settings, int commentIndex, XmlFileHeader header)
@@ -460,5 +465,22 @@ private static SyntaxTriviaList RemoveHeaderDecorationLines(SyntaxTriviaList tri
460465

461466
return trivia;
462467
}
468+
469+
private class FixAll : DocumentBasedFixAllProvider
470+
{
471+
public static FixAllProvider Instance { get; } = new FixAll();
472+
473+
protected override string CodeActionTitle => DocumentationResources.SA1633CodeFix;
474+
475+
protected override Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
476+
{
477+
if (diagnostics.IsEmpty)
478+
{
479+
return null;
480+
}
481+
482+
return GetTransformedSyntaxRootAsync(document, fixAllContext.CancellationToken);
483+
}
484+
}
463485
}
464486
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1517CodeFixProvider.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class SA1517CodeFixProvider : CodeFixProvider
2727
/// <inheritdoc/>
2828
public override FixAllProvider GetFixAllProvider()
2929
{
30-
return CustomFixAllProviders.BatchFixer;
30+
return FixAll.Instance;
3131
}
3232

3333
/// <inheritdoc/>
@@ -47,6 +47,13 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4747
}
4848

4949
private static async Task<Document> GetTransformedDocumentAsync(Document document, CancellationToken token)
50+
{
51+
var newSyntaxRoot = await GetTransformedSyntaxRootAsync(document, token).ConfigureAwait(false);
52+
53+
return document.WithSyntaxRoot(newSyntaxRoot);
54+
}
55+
56+
private static async Task<SyntaxNode> GetTransformedSyntaxRootAsync(Document document, CancellationToken token)
5057
{
5158
var syntaxRoot = await document.GetSyntaxRootAsync(token).ConfigureAwait(false);
5259

@@ -66,9 +73,26 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6673

6774
var newFirstToken = firstToken.WithLeadingTrivia(newTriviaList);
6875
var newSyntaxRoot = syntaxRoot.ReplaceToken(firstToken, newFirstToken);
69-
var newDocument = document.WithSyntaxRoot(newSyntaxRoot);
76+
return newSyntaxRoot;
77+
}
78+
79+
private class FixAll : DocumentBasedFixAllProvider
80+
{
81+
public static FixAllProvider Instance { get; } =
82+
new FixAll();
7083

71-
return newDocument;
84+
protected override string CodeActionTitle =>
85+
LayoutResources.SA1517CodeFix;
86+
87+
protected override Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
88+
{
89+
if (diagnostics.IsEmpty)
90+
{
91+
return null;
92+
}
93+
94+
return GetTransformedSyntaxRootAsync(document, fixAllContext.CancellationToken);
95+
}
7296
}
7397
}
7498
}

0 commit comments

Comments
 (0)