|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.SpacingRules |
5 | 5 | { |
| 6 | + using System.Collections.Generic; |
6 | 7 | using System.Collections.Immutable; |
7 | 8 | using System.Composition; |
8 | 9 | using System.Linq; |
@@ -31,7 +32,7 @@ public class SA1027CodeFixProvider : CodeFixProvider |
31 | 32 | /// <inheritdoc/> |
32 | 33 | public override FixAllProvider GetFixAllProvider() |
33 | 34 | { |
34 | | - return CustomFixAllProviders.BatchFixer; |
| 35 | + return FixAll.Instance; |
35 | 36 | } |
36 | 37 |
|
37 | 38 | /// <inheritdoc/> |
@@ -95,5 +96,37 @@ private static TextChange FixDiagnostic(IndentationOptions indentationOptions, S |
95 | 96 |
|
96 | 97 | return new TextChange(span, replacement.ToString()); |
97 | 98 | } |
| 99 | + |
| 100 | + private class FixAll : DocumentBasedFixAllProvider |
| 101 | + { |
| 102 | + public static FixAllProvider Instance { get; } |
| 103 | + = new FixAll(); |
| 104 | + |
| 105 | + protected override string CodeActionTitle |
| 106 | + => SpacingResources.SA1027CodeFix; |
| 107 | + |
| 108 | + protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) |
| 109 | + { |
| 110 | + var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); |
| 111 | + if (diagnostics.IsEmpty) |
| 112 | + { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + var indentationOptions = IndentationOptions.FromDocument(document); |
| 117 | + SourceText sourceText = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false); |
| 118 | + |
| 119 | + List<TextChange> changes = new List<TextChange>(); |
| 120 | + foreach (var diagnostic in diagnostics) |
| 121 | + { |
| 122 | + changes.Add(FixDiagnostic(indentationOptions, sourceText, diagnostic)); |
| 123 | + } |
| 124 | + |
| 125 | + changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start)); |
| 126 | + |
| 127 | + SyntaxTree tree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false); |
| 128 | + return await tree.WithChangedText(sourceText.WithChanges(changes)).GetRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); |
| 129 | + } |
| 130 | + } |
98 | 131 | } |
99 | 132 | } |
0 commit comments