public static async Task <ImmutableDictionary <Project, ImmutableArray <Diagnostic> > > GetProjectDiagnosticsToFixAsync(FixAllContext fixAllContext) { var project = fixAllContext.Project; if (project != null) { switch (fixAllContext.Scope) { case FixAllScope.Project: var diagnostics = await fixAllContext.GetProjectDiagnosticsAsync(project).ConfigureAwait(false); return(ImmutableDictionary <Project, ImmutableArray <Diagnostic> > .Empty.SetItem(project, diagnostics)); case FixAllScope.Solution: var projectsAndDiagnostics = new ConcurrentDictionary <Project, ImmutableArray <Diagnostic> >(); var tasks = new List <Task>(project.Solution.ProjectIds.Count); Func <Project, Task> projectAction = async proj => { if (fixAllContext.CancellationToken.IsCancellationRequested) { return; } var projectDiagnostics = await fixAllContext.GetProjectDiagnosticsAsync(proj).ConfigureAwait(false); if (projectDiagnostics.Any()) { projectsAndDiagnostics.TryAdd(proj, projectDiagnostics); } }; foreach (var proj in project.Solution.Projects) { if (fixAllContext.CancellationToken.IsCancellationRequested) { break; } tasks.Add(projectAction(proj)); } await Task.WhenAll(tasks).ConfigureAwait(false); return(projectsAndDiagnostics.ToImmutableDictionary()); } } return(ImmutableDictionary <Project, ImmutableArray <Diagnostic> > .Empty); }
public static async Task <ImmutableDictionary <Project, ImmutableArray <Diagnostic> > > GetProjectDiagnosticsToFixAsync(FixAllContext fixAllContext) { var project = fixAllContext.Project; if (project != null) { switch (fixAllContext.Scope) { case FixAllScope.Project: var diagnostics = await fixAllContext.GetProjectDiagnosticsAsync(project).ConfigureAwait(false); return(ImmutableDictionary <Project, ImmutableArray <Diagnostic> > .Empty.SetItem(project, diagnostics)); case FixAllScope.Solution: var projectsAndDiagnostics = new ConcurrentDictionary <Project, ImmutableArray <Diagnostic> >(); var options = new ParallelOptions() { CancellationToken = fixAllContext.CancellationToken }; Parallel.ForEach(project.Solution.Projects, options, proj => { fixAllContext.CancellationToken.ThrowIfCancellationRequested(); var projectDiagnosticsTask = fixAllContext.GetProjectDiagnosticsAsync(proj); projectDiagnosticsTask.Wait(fixAllContext.CancellationToken); var projectDiagnostics = projectDiagnosticsTask.Result; if (projectDiagnostics.Any()) { projectsAndDiagnostics.TryAdd(proj, projectDiagnostics); } }); return(projectsAndDiagnostics.ToImmutableDictionary()); } } return(ImmutableDictionary <Project, ImmutableArray <Diagnostic> > .Empty); }