Skip to content

Commit

Permalink
Move to SyntaxEditorBasedCodeFixProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Apr 4, 2022
1 parent 3c0d140 commit 5e4b835
Showing 1 changed file with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;

using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper;

namespace Microsoft.CodeAnalysis.CodeStyle
{
internal abstract class AbstractFormattingCodeFixProvider : CodeFixProvider
internal abstract class AbstractFormattingCodeFixProvider : SyntaxEditorBasedCodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(IDEDiagnosticIds.FormattingDiagnosticId);
Expand Down Expand Up @@ -54,14 +55,11 @@ private async Task<SyntaxFormattingOptions> GetOptionsAsync(Document document, C
return this.SyntaxFormatting.GetFormattingOptions(analyzerConfigOptions);
}

public sealed override FixAllProvider GetFixAllProvider()
=> FixAllProvider.Create(async (context, document, diagnostics) =>
{
var cancellationToken = context.CancellationToken;
var options = await GetOptionsAsync(document, cancellationToken).ConfigureAwait(false);
var syntaxRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var updatedSyntaxRoot = Formatter.Format(syntaxRoot, this.SyntaxFormatting, options, cancellationToken);
return document.WithSyntaxRoot(updatedSyntaxRoot);
});
protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
var options = await GetOptionsAsync(document, cancellationToken).ConfigureAwait(false);
var updatedRoot = Formatter.Format(editor.OriginalRoot, SyntaxFormatting, options, cancellationToken);
editor.ReplaceNode(editor.OriginalRoot, updatedRoot);
}
}
}

0 comments on commit 5e4b835

Please sign in to comment.