未验证 提交 5b94019e 编写于 作者: P Petr Houška 提交者: GitHub

Merge pull request #38521 from petrroll/docsInCodeFixesService

Added comments here and there.
......@@ -147,6 +147,9 @@ public async Task<ImmutableArray<CodeFixCollection>> GetFixesAsync(Document docu
//
// this design's weakness is that each side don't have enough information to narrow down works to do. it will most likely always do more works than needed.
// sometimes way more than it is needed. (compilation)
// group diagnostics by their diagnostics span
// invariant: later code gathers & runs CodeFixProviders for diagnostics with one identical diagnostics span (that gets set later as CodeFixCollection's TextSpan)
Dictionary<TextSpan, List<DiagnosticData>> aggregatedDiagnostics = null;
foreach (var diagnostic in await _diagnosticService.GetDiagnosticsForSpanAsync(document, range, diagnosticIdOpt: null, includeConfigurationFixes, cancellationToken).ConfigureAwait(false))
{
......@@ -166,6 +169,7 @@ public async Task<ImmutableArray<CodeFixCollection>> GetFixesAsync(Document docu
return ImmutableArray<CodeFixCollection>.Empty;
}
// append fixes for all diagnostics with the same diagnostics span
using var resultDisposer = ArrayBuilder<CodeFixCollection>.GetInstance(out var result);
foreach (var spanAndDiagnostic in aggregatedDiagnostics)
{
......@@ -257,6 +261,7 @@ public async Task<Document> ApplyCodeFixesForSpecificDiagnosticIdAsync(Document
// TODO (https://github.com/dotnet/roslyn/issues/4932): Don't restrict CodeFixes in Interactive
var isInteractive = document.Project.Solution.Workspace.Kind == WorkspaceKind.Interactive;
// gather CodeFixProviders for all distinct diagnostics found for current span
foreach (var diagnosticId in diagnostics.Select(d => d.Id).Distinct())
{
cancellationToken.ThrowIfCancellationRequested();
......@@ -282,6 +287,7 @@ public async Task<Document> ApplyCodeFixesForSpecificDiagnosticIdAsync(Document
var extensionManager = document.Project.Solution.Workspace.Services.GetService<IExtensionManager>();
// run each CodeFixProvider to gather individual CodeFixes for reported diagnostics
foreach (var fixer in allFixers.Distinct())
{
cancellationToken.ThrowIfCancellationRequested();
......@@ -333,7 +339,7 @@ public async Task<Document> ApplyCodeFixesForSpecificDiagnosticIdAsync(Document
}
private async Task AppendConfigurationsAsync(
Document document, TextSpan span, IEnumerable<DiagnosticData> diagnostics,
Document document, TextSpan diagnosticsSpan, IEnumerable<DiagnosticData> diagnostics,
ArrayBuilder<CodeFixCollection> result, CancellationToken cancellationToken)
{
if (!_configurationProvidersMap.TryGetValue(document.Project.Language, out var lazyConfigurationProviders) || lazyConfigurationProviders.Value == null)
......@@ -341,20 +347,21 @@ public async Task<Document> ApplyCodeFixesForSpecificDiagnosticIdAsync(Document
return;
}
// append CodeFixCollection for each CodeFixProvider
foreach (var provider in lazyConfigurationProviders.Value)
{
await AppendFixesOrConfigurationsAsync(
document, span, diagnostics, fixAllForInSpan: false, result, provider,
document, diagnosticsSpan, diagnostics, fixAllForInSpan: false, result, provider,
hasFix: d => provider.IsFixableDiagnostic(d),
getFixes: dxs => provider.GetFixesAsync(
document, span, dxs, cancellationToken),
document, diagnosticsSpan, dxs, cancellationToken),
cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
private async Task AppendFixesOrConfigurationsAsync<TCodeFixProvider>(
Document document,
TextSpan span,
TextSpan fixesSpan,
IEnumerable<DiagnosticData> diagnosticsWithSameSpan,
bool fixAllForInSpan,
ArrayBuilder<CodeFixCollection> result,
......@@ -413,7 +420,7 @@ await diagnosticsWithSameSpan.OrderByDescending(d => d.Severity)
}
var codeFix = new CodeFixCollection(
fixer, span, fixes, fixAllState,
fixer, fixesSpan, fixes, fixAllState,
supportedScopes, diagnostics.First());
result.Add(codeFix);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册