// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes
{
internal partial class FixAllState
{
///
/// Diagnostic provider to fetch document/project diagnostics to fix in a .
///
internal sealed class FixMultipleDiagnosticProvider : FixAllContext.DiagnosticProvider
{
internal override bool IsFixMultiple => true;
private readonly ImmutableDictionary> _documentDiagnosticsMap;
private readonly ImmutableDictionary> _projectDiagnosticsMap;
public FixMultipleDiagnosticProvider(ImmutableDictionary> diagnosticsMap)
{
_documentDiagnosticsMap = diagnosticsMap;
_projectDiagnosticsMap = ImmutableDictionary>.Empty;
}
public FixMultipleDiagnosticProvider(ImmutableDictionary> diagnosticsMap)
{
_projectDiagnosticsMap = diagnosticsMap;
_documentDiagnosticsMap = ImmutableDictionary>.Empty;
}
internal override Task>> GetDocumentDiagnosticsToFixAsync(
FixAllContext context)
{
return Task.FromResult(_documentDiagnosticsMap);
}
internal override Task>> GetProjectDiagnosticsToFixAsync(
FixAllContext context)
{
return Task.FromResult(_projectDiagnosticsMap);
}
public override Task> GetAllDiagnosticsAsync(Project project, CancellationToken cancellationToken)
{
ImmutableArray.Builder allDiagnosticsBuilder = null;
ImmutableArray diagnostics;
if (!_documentDiagnosticsMap.IsEmpty)
{
foreach (var document in project.Documents)
{
if (_documentDiagnosticsMap.TryGetValue(document, out diagnostics))
{
allDiagnosticsBuilder = allDiagnosticsBuilder ?? ImmutableArray.CreateBuilder(diagnostics.Length);
allDiagnosticsBuilder.AddRange(diagnostics);
}
}
}
if (_projectDiagnosticsMap.TryGetValue(project, out diagnostics))
{
allDiagnosticsBuilder = allDiagnosticsBuilder ?? ImmutableArray.CreateBuilder(diagnostics.Length);
allDiagnosticsBuilder.AddRange(diagnostics);
}
IEnumerable allDiagnostics = allDiagnosticsBuilder != null ? allDiagnosticsBuilder.ToImmutable() : ImmutableArray.Empty;
return Task.FromResult(allDiagnostics);
}
public override Task> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken)
{
ImmutableArray diagnostics;
if (_documentDiagnosticsMap.TryGetValue(document, out diagnostics))
{
return Task.FromResult>(diagnostics);
}
return Task.FromResult(SpecializedCollections.EmptyEnumerable());
}
public override Task> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)
{
ImmutableArray diagnostics;
if (_projectDiagnosticsMap.TryGetValue(project, out diagnostics))
{
return Task.FromResult>(diagnostics);
}
return Task.FromResult(SpecializedCollections.EmptyEnumerable());
}
}
}
}