Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
meziantou committed Nov 13, 2024
commit 5d218ab4eaa4c04d0516659ded916008a5b01a3d
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ROSLYN_3_8
#if !ROSLYN_3_8
#pragma warning disable IDE0130 // Namespace does not match folder structure
// File initially copied from
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/4d9b3e3bb785a55f73b3029a843f0c0b73cc9ea7/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/DocumentBasedFixAllProvider.cs
// Original copyright statement:
Expand All @@ -20,22 +21,22 @@ namespace Meziantou.Analyzer;
/// </summary>
internal abstract class DocumentBasedFixAllProvider : FixAllProvider
{
protected abstract string CodeActionTitle { get; }
protected abstract string GetFixAllTitle(FixAllContext fixAllContext);

public override Task<CodeAction?> GetFixAsync(FixAllContext fixAllContext)
{
return Task.FromResult(fixAllContext.Scope switch
{
FixAllScope.Document => CodeAction.Create(
CodeActionTitle,
GetFixAllTitle(fixAllContext),
cancellationToken => GetDocumentFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
nameof(DocumentBasedFixAllProvider)),
FixAllScope.Project => CodeAction.Create(
CodeActionTitle,
GetFixAllTitle(fixAllContext),
cancellationToken => GetProjectFixesAsync(fixAllContext.WithCancellationToken(cancellationToken), fixAllContext.Project),
nameof(DocumentBasedFixAllProvider)),
FixAllScope.Solution => CodeAction.Create(
CodeActionTitle,
GetFixAllTitle(fixAllContext),
cancellationToken => GetSolutionFixesAsync(fixAllContext.WithCancellationToken(cancellationToken)),
nameof(DocumentBasedFixAllProvider)),
_ => null,
Expand All @@ -49,11 +50,11 @@ internal abstract class DocumentBasedFixAllProvider : FixAllProvider
/// <param name="document">The document to fix.</param>
/// <param name="diagnostics">The diagnostics to fix in the document.</param>
/// <returns>
/// <para>The new <see cref="SyntaxNode"/> representing the root of the fixed document.</para>
/// <para>The new <see cref="Document"/> representing the root of the fixed document.</para>
/// <para>-or-</para>
/// <para><see langword="null"/>, if no changes were made to the document.</para>
/// </returns>
protected abstract Task<SyntaxNode?> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics);
protected abstract Task<Document?> FixAllAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics);

private async Task<Document> GetDocumentFixesAsync(FixAllContext fixAllContext)
{
Expand All @@ -64,30 +65,25 @@ private async Task<Document> GetDocumentFixesAsync(FixAllContext fixAllContext)
return document;
}

var newRoot = await FixAllInDocumentAsync(fixAllContext, document, diagnostics).ConfigureAwait(false);
if (newRoot is null)
{
return document;
}

return document.WithSyntaxRoot(newRoot);
var newDocument = await FixAllAsync(fixAllContext, document, diagnostics).ConfigureAwait(false);
return newDocument ?? document;
}

private async Task<Solution> GetSolutionFixesAsync(FixAllContext fixAllContext, ImmutableArray<Document> documents)
{
var documentDiagnosticsToFix = await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(fixAllContext).ConfigureAwait(false);

var solution = fixAllContext.Solution;
var newDocuments = new List<Task<SyntaxNode?>>(documents.Length);
var newDocuments = new List<Task<Document?>>(documents.Length);
foreach (var document in documents)
{
if (!documentDiagnosticsToFix.TryGetValue(document, out var diagnostics))
{
newDocuments.Add(document.GetSyntaxRootAsync(fixAllContext.CancellationToken));
newDocuments.Add(Task.FromResult<Document?>(document));
continue;
}

newDocuments.Add(FixAllInDocumentAsync(fixAllContext, document, diagnostics));
newDocuments.Add(FixAllAsync(fixAllContext, document, diagnostics));
}

for (var i = 0; i < documents.Length; i++)
Expand All @@ -96,7 +92,7 @@ private async Task<Solution> GetSolutionFixesAsync(FixAllContext fixAllContext,
if (newDocumentRoot is null)
continue;

solution = solution.WithDocumentSyntaxRoot(documents[i].Id, newDocumentRoot);
//solution = solution.WithDocumentSyntaxRoot(documents[i].Id, newDocumentRoot);
}

return solution;
Expand Down