예제 #1
0
        private async Task <ImmutableArray <(Diagnostic diagnostic, CodeAction action)> > GetDiagnosticsAndCodeActions(
            ImmutableDictionary <Document, ImmutableArray <Diagnostic> > documentsAndDiagnosticsToFixMap,
            FixAllState fixAllState, CancellationToken cancellationToken)
        {
            var fixesBag = new ConcurrentBag <(Diagnostic diagnostic, CodeAction action)>();

            using (Logger.LogBlock(
                       FunctionId.CodeFixes_FixAllOccurrencesComputation_Document_Fixes,
                       FixAllLogger.CreateCorrelationLogMessage(fixAllState.CorrelationId),
                       cancellationToken))
            {
                cancellationToken.ThrowIfCancellationRequested();

                var tasks = new List <Task>();

                foreach (var kvp in documentsAndDiagnosticsToFixMap)
                {
                    var document         = kvp.Key;
                    var diagnosticsToFix = kvp.Value;
                    Debug.Assert(!diagnosticsToFix.IsDefaultOrEmpty);
                    if (!diagnosticsToFix.IsDefaultOrEmpty)
                    {
                        tasks.Add(AddDocumentFixesAsync(
                                      document, diagnosticsToFix, fixesBag, fixAllState, cancellationToken));
                    }
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }

            return(fixesBag.ToImmutableArray());
        }
예제 #2
0
        internal override async Task <CodeAction> GetFixAsync(
            ImmutableDictionary <Document, ImmutableArray <Diagnostic> > documentsAndDiagnosticsToFixMap,
            FixAllState fixAllState, CancellationToken cancellationToken)
        {
            if (documentsAndDiagnosticsToFixMap?.Any() == true)
            {
                FixAllLogger.LogDiagnosticsStats(fixAllState.CorrelationId, documentsAndDiagnosticsToFixMap);

                var diagnosticsAndCodeActions = await GetDiagnosticsAndCodeActions(
                    documentsAndDiagnosticsToFixMap, fixAllState, cancellationToken).ConfigureAwait(false);

                if (diagnosticsAndCodeActions.Length > 0)
                {
                    var functionId = FunctionId.CodeFixes_FixAllOccurrencesComputation_Document_Merge;
                    using (Logger.LogBlock(functionId, FixAllLogger.CreateCorrelationLogMessage(fixAllState.CorrelationId), cancellationToken))
                    {
                        FixAllLogger.LogFixesToMergeStats(functionId, fixAllState.CorrelationId, diagnosticsAndCodeActions.Length);
                        return(await TryGetMergedFixAsync(
                                   diagnosticsAndCodeActions, fixAllState, cancellationToken).ConfigureAwait(false));
                    }
                }
            }

            return(null);
        }
예제 #3
0
        internal async override Task <Solution> GetChangedSolutionAsync(
            IProgressTracker progressTracker, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            FixAllLogger.LogState(FixAllState, IsInternalCodeFixProvider(FixAllState.CodeFixProvider));

            var service = FixAllState.Project.Solution.Workspace.Services.GetService <IFixAllGetFixesService>();

            // Use the new cancellation token instead of the stale one present inside _fixAllContext.
            return(await service.GetFixAllChangedSolutionAsync(
                       FixAllState.CreateFixAllContext(progressTracker, cancellationToken)).ConfigureAwait(false));
        }
예제 #4
0
        internal override Task <ImmutableArray <CodeActionOperation> > ComputeOperationsAsync(
            IProgressTracker progressTracker, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            FixAllLogger.LogState(FixAllState, IsInternalCodeFixProvider(FixAllState.CodeFixProvider));

            var service = FixAllState.Project.Solution.Workspace.Services.GetService <IFixAllGetFixesService>();

            // Use the new cancellation token instead of the stale one present inside _fixAllContext.
            return(service.GetFixAllOperationsAsync(
                       FixAllState.CreateFixAllContext(progressTracker, cancellationToken),
                       _showPreviewChangesDialog));
        }
 internal virtual async Task <ImmutableDictionary <Document, ImmutableArray <Diagnostic> > > GetDocumentDiagnosticsToFixWorkerAsync(
     FixAllContext fixAllContext)
 {
     using (Logger.LogBlock(
                FunctionId.CodeFixes_FixAllOccurrencesComputation_Document_Diagnostics,
                FixAllLogger.CreateCorrelationLogMessage(fixAllContext.State.CorrelationId),
                fixAllContext.CancellationToken))
     {
         return(await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(
                    fixAllContext,
                    fixAllContext.ProgressTracker,
                    (document, cancellationToken) => document.IsGeneratedCode(cancellationToken)).ConfigureAwait(false));
     }
 }
            internal virtual async Task <ImmutableDictionary <Project, ImmutableArray <Diagnostic> > > GetProjectDiagnosticsToFixAsync(
                FixAllContext fixAllContext)
            {
                using (Logger.LogBlock(
                           FunctionId.CodeFixes_FixAllOccurrencesComputation_Project_Diagnostics,
                           FixAllLogger.CreateCorrelationLogMessage(fixAllContext.State.CorrelationId),
                           fixAllContext.CancellationToken))
                {
                    var project = fixAllContext.Project;
                    if (project != null)
                    {
                        switch (fixAllContext.Scope)
                        {
                        case FixAllScope.Project:
                            var diagnostics = await fixAllContext.GetProjectDiagnosticsAsync(project).ConfigureAwait(false);

                            var kvp = SpecializedCollections.SingletonEnumerable(KeyValuePairUtil.Create(project, diagnostics));
                            return(ImmutableDictionary.CreateRange(kvp));

                        case FixAllScope.Solution:
                            var projectsAndDiagnostics = ImmutableDictionary.CreateBuilder <Project, ImmutableArray <Diagnostic> >();

                            var tasks = project.Solution.Projects.Select(async p => new
                            {
                                Project     = p,
                                Diagnostics = await fixAllContext.GetProjectDiagnosticsAsync(p).ConfigureAwait(false)
                            }).ToArray();

                            await Task.WhenAll(tasks).ConfigureAwait(false);

                            foreach (var task in tasks)
                            {
                                var projectAndDiagnostics = await task.ConfigureAwait(false);

                                if (projectAndDiagnostics.Diagnostics.Any())
                                {
                                    projectsAndDiagnostics[projectAndDiagnostics.Project] = projectAndDiagnostics.Diagnostics;
                                }
                            }

                            return(projectsAndDiagnostics.ToImmutable());
                        }
                    }

                    return(ImmutableDictionary <Project, ImmutableArray <Diagnostic> > .Empty);
                }
            }