Skip to content

Commit

Permalink
Merge pull request #73385 from CyrusNajmabadi/shareFixAllCode
Browse files Browse the repository at this point in the history
Share fix-all code
  • Loading branch information
CyrusNajmabadi authored May 8, 2024
2 parents ab8d819 + a20eb2e commit 9eeda46
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public sealed override IEnumerable<FixAllScope> GetSupportedFixAllScopes()
DetermineDiagnosticsAndGetFixedDocumentsAsync);

private async Task DetermineDiagnosticsAndGetFixedDocumentsAsync(
FixAllContext fixAllContext, Action<(DocumentId documentId, (SyntaxNode? node, SourceText? text))> callback)
FixAllContext fixAllContext, Func<Document, Document?, ValueTask> onDocumentFixed)
{
var cancellationToken = fixAllContext.CancellationToken;

Expand All @@ -93,15 +93,7 @@ await RoslynParallel.ForEachAsync(
return;

var newDocument = await this.FixAllAsync(fixAllContext, document, documentDiagnostics).ConfigureAwait(false);
if (newDocument == null || newDocument == document)
return;

// For documents that support syntax, grab the tree so that we can clean it up later. If it's a
// language that doesn't support that, then just grab the text.
var node = newDocument.SupportsSyntaxTree ? await newDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false) : null;
var text = newDocument.SupportsSyntaxTree ? null : await newDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

callback((document.Id, (node, text)));
await onDocumentFixed(document, newDocument).ConfigureAwait(false);
}).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal static class DocumentBasedFixAllProviderHelpers
ImmutableArray<TFixAllContext> fixAllContexts,
IProgress<CodeAnalysisProgress> progressTracker,
string progressTrackerDescription,
Func<TFixAllContext, Action<(DocumentId documentId, (SyntaxNode? node, SourceText? text))>, Task> getFixedDocumentsAsync)
Func<TFixAllContext, Func<Document, Document?, ValueTask>, Task> getFixedDocumentsAsync)
where TFixAllContext : IFixAllContext
{
var cancellationToken = originalFixAllContext.CancellationToken;
Expand Down Expand Up @@ -59,7 +59,14 @@ internal static class DocumentBasedFixAllProviderHelpers
Contract.ThrowIfFalse(
fixAllContext.Scope is FixAllScope.Document or FixAllScope.Project or FixAllScope.ContainingMember or FixAllScope.ContainingType);

await args.getFixedDocumentsAsync(fixAllContext, callback).ConfigureAwait(false);
await args.getFixedDocumentsAsync(
fixAllContext,
async (document, newDocument) =>
{
var tuple = await ProcessFixedDocumentAsync(document, newDocument, cancellationToken).ConfigureAwait(false);
if (tuple.HasValue)
callback(tuple.Value);
}).ConfigureAwait(false);
},
consumeItems: static async (stream, args, cancellationToken) =>
{
Expand Down Expand Up @@ -133,6 +140,21 @@ async Task<Solution> CleanSolutionAsync(Solution dirtySolution, ImmutableArray<D
args: (dirtySolution, progressTracker),
cancellationToken).ConfigureAwait(false);
}

async static ValueTask<(DocumentId documentId, (SyntaxNode? node, SourceText? text))?> ProcessFixedDocumentAsync(
Document document, Document? newDocument, CancellationToken cancellationToken)
{
// If we didn't get a distinct new document, then we don't have anything to do.
if (newDocument == null || newDocument == document)
return null;

// For documents that support syntax, grab the tree so that we can clean it up later. If it's a
// language that doesn't support that, then just grab the text.
var node = newDocument.SupportsSyntaxTree ? await newDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false) : null;
var text = newDocument.SupportsSyntaxTree ? null : await newDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

return (document.Id, (node, text));
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public sealed override IEnumerable<FixAllScope> GetSupportedFixAllScopes()
/// documents that don't support syntax.
/// </summary>
private async Task GetFixedDocumentsAsync(
FixAllContext fixAllContext, Action<(DocumentId documentId, (SyntaxNode? node, SourceText? text))> callback)
FixAllContext fixAllContext, Func<Document, Document?, ValueTask> onDocumentFixed)
{
Contract.ThrowIfFalse(fixAllContext.Scope is FixAllScope.Document or FixAllScope.Project
or FixAllScope.ContainingMember or FixAllScope.ContainingType);
Expand All @@ -104,15 +104,7 @@ await RoslynParallel.ForEachAsync(
{
var (document, spans) = tuple;
var newDocument = await this.FixAllAsync(fixAllContext, document, spans).ConfigureAwait(false);
if (newDocument == null || newDocument == document)
return;

// For documents that support syntax, grab the tree so that we can clean it up later. If it's a
// language that doesn't support that, then just grab the text.
var node = newDocument.SupportsSyntaxTree ? await newDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false) : null;
var text = newDocument.SupportsSyntaxTree ? null : await newDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

callback((document.Id, (node, text)));
await onDocumentFixed(document, newDocument).ConfigureAwait(false);
}).ConfigureAwait(false);
}
}

0 comments on commit 9eeda46

Please sign in to comment.