示例#1
0
            private static async Task <Solution> CreateChangedSolutionAsync(AbstractSuppressionCodeFixProvider fixer, Project triggerProject, ImmutableDictionary <Project, ImmutableArray <Diagnostic> > diagnosticsByProject, CancellationToken cancellationToken)
            {
                var currentSolution = triggerProject.Solution;

                foreach (var(oldProject, diagnostics) in diagnosticsByProject)
                {
                    var currentProject = currentSolution.GetProject(oldProject.Id);
                    var compilation    = await currentProject.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

                    var suppressMessageAttribute = compilation.SuppressMessageAttributeType();

                    if (suppressMessageAttribute != null)
                    {
                        var diagnosticsBySymbol = await CreateDiagnosticsBySymbolAsync(oldProject, diagnostics, cancellationToken).ConfigureAwait(false);

                        if (diagnosticsBySymbol.Any())
                        {
                            var projectCodeAction = new GlobalSuppressMessageFixAllCodeAction(
                                fixer, suppressMessageAttribute, diagnosticsBySymbol, currentProject);
                            var newDocument = await projectCodeAction.GetChangedSuppressionDocumentAsync(cancellationToken).ConfigureAwait(false);

                            currentSolution = newDocument.Project.Solution;
                        }
                    }
                }

                return(currentSolution);
            }
            public async override Task <CodeAction> GetFixAsync(FixAllContext fixAllContext)
            {
                // currently there's no FixAll support for local suppression, just bail out
                if (NestedSuppressionCodeAction.IsEquivalenceKeyForLocalSuppression(fixAllContext.CodeActionEquivalenceKey))
                {
                    return(null);
                }

                var batchFixer          = WellKnownFixAllProviders.BatchFixer;
                var suppressionFixer    = (AbstractSuppressionCodeFixProvider)((WrapperCodeFixProvider)fixAllContext.CodeFixProvider).SuppressionFixProvider;
                var isGlobalSuppression = NestedSuppressionCodeAction.IsEquivalenceKeyForGlobalSuppression(fixAllContext.CodeActionEquivalenceKey);

                if (!isGlobalSuppression)
                {
                    var isPragmaWarningSuppression = NestedSuppressionCodeAction.IsEquivalenceKeyForPragmaWarning(fixAllContext.CodeActionEquivalenceKey);
                    Contract.ThrowIfFalse(isPragmaWarningSuppression || NestedSuppressionCodeAction.IsEquivalenceKeyForRemoveSuppression(fixAllContext.CodeActionEquivalenceKey));

                    batchFixer = isPragmaWarningSuppression ?
                                 new PragmaWarningBatchFixAllProvider(suppressionFixer) :
                                 RemoveSuppressionCodeAction.GetBatchFixer(suppressionFixer);
                }

                var title = fixAllContext.CodeActionEquivalenceKey;

                if (fixAllContext.Document != null)
                {
                    var documentsAndDiagnosticsToFixMap =
                        await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(false);

                    return(!isGlobalSuppression
                        ? await batchFixer.GetFixAsync(
                               documentsAndDiagnosticsToFixMap, fixAllContext.State, fixAllContext.CancellationToken).ConfigureAwait(false)
                        : GlobalSuppressMessageFixAllCodeAction.Create(title, suppressionFixer, fixAllContext.Document, documentsAndDiagnosticsToFixMap));
                }
                else
                {
                    var projectsAndDiagnosticsToFixMap =
                        await fixAllContext.GetProjectDiagnosticsToFixAsync().ConfigureAwait(false);

                    return(!isGlobalSuppression
                        ? await batchFixer.GetFixAsync(
                               projectsAndDiagnosticsToFixMap, fixAllContext.State, fixAllContext.CancellationToken).ConfigureAwait(false)
                        : GlobalSuppressMessageFixAllCodeAction.Create(title, suppressionFixer, fixAllContext.Project, projectsAndDiagnosticsToFixMap));
                }
            }
            private static async Task<Solution> CreateChangedSolutionAsync(AbstractSuppressionCodeFixProvider fixer, Project triggerProject, ImmutableDictionary<Project, ImmutableArray<Diagnostic>> diagnosticsByProject, CancellationToken cancellationToken)
            {
                var currentSolution = triggerProject.Solution;
                foreach (var kvp in diagnosticsByProject)
                {
                    var oldProject = kvp.Key;
                    var currentProject = currentSolution.GetProject(oldProject.Id);
                    var diagnosticsBySymbol = await CreateDiagnosticsBySymbolAsync(fixer, oldProject, kvp.Value, cancellationToken).ConfigureAwait(false);
                    if (diagnosticsBySymbol.Any())
                    {
                        var projectCodeAction = new GlobalSuppressMessageFixAllCodeAction(fixer, diagnosticsBySymbol, currentProject);
                        var newDocument = await projectCodeAction.GetChangedSuppressionDocumentAsync(cancellationToken).ConfigureAwait(false);
                        currentSolution = newDocument.Project.Solution;
                    }
                }

                return currentSolution;
            }
示例#4
0
            public override async Task <CodeAction> GetFixAsync(FixAllContext fixAllContext)
            {
                // currently there's no FixAll support for local suppression, just bail out
                if (NestedSuppressionCodeAction.IsEquivalenceKeyForLocalSuppression(fixAllContext.CodeActionEquivalenceKey))
                {
                    return(null);
                }

                var suppressionFixer = (AbstractSuppressionCodeFixProvider)((WrapperCodeFixProvider)fixAllContext.CodeFixProvider).SuppressionFixProvider;

                if (NestedSuppressionCodeAction.IsEquivalenceKeyForGlobalSuppression(fixAllContext.CodeActionEquivalenceKey))
                {
                    var fallbackOptions = fixAllContext.GetOptionsProvider();

                    // For global suppressions, we defer to the global suppression system to handle directly.
                    var title = fixAllContext.CodeActionEquivalenceKey;
                    return(fixAllContext.Document != null
                        ? GlobalSuppressMessageFixAllCodeAction.Create(
                               title, suppressionFixer, fixAllContext.Document,
                               await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(false),
                               fallbackOptions)
                        : GlobalSuppressMessageFixAllCodeAction.Create(
                               title, suppressionFixer, fixAllContext.Project,
                               await fixAllContext.GetProjectDiagnosticsToFixAsync().ConfigureAwait(false),
                               fallbackOptions));
                }

                if (NestedSuppressionCodeAction.IsEquivalenceKeyForPragmaWarning(fixAllContext.CodeActionEquivalenceKey))
                {
                    var batchFixer = new PragmaWarningBatchFixAllProvider(suppressionFixer);
                    return(await batchFixer.GetFixAsync(fixAllContext).ConfigureAwait(false));
                }

                if (NestedSuppressionCodeAction.IsEquivalenceKeyForRemoveSuppression(fixAllContext.CodeActionEquivalenceKey))
                {
                    var batchFixer = RemoveSuppressionCodeAction.GetBatchFixer(suppressionFixer);
                    return(await batchFixer.GetFixAsync(fixAllContext).ConfigureAwait(false));
                }

                throw ExceptionUtilities.Unreachable;
            }
            private static async Task <Solution> CreateChangedSolutionAsync(AbstractSuppressionCodeFixProvider fixer, Document triggerDocument, ImmutableDictionary <Document, ImmutableArray <Diagnostic> > diagnosticsByDocument, CancellationToken cancellationToken)
            {
                var currentSolution = triggerDocument.Project.Solution;

                foreach (var grouping in diagnosticsByDocument.GroupBy(d => d.Key.Project))
                {
                    var oldProject          = grouping.Key;
                    var currentProject      = currentSolution.GetProject(oldProject.Id);
                    var diagnosticsBySymbol = await CreateDiagnosticsBySymbolAsync(fixer, grouping, cancellationToken).ConfigureAwait(false);

                    if (diagnosticsBySymbol.Any())
                    {
                        var projectCodeAction = new GlobalSuppressMessageFixAllCodeAction(fixer, diagnosticsBySymbol, currentProject);
                        var newDocument       = await projectCodeAction.GetChangedSuppressionDocumentAsync(cancellationToken).ConfigureAwait(false);

                        currentSolution = newDocument.Project.Solution;
                    }
                }

                return(currentSolution);
            }
示例#6
0
            public async override Task <CodeAction> GetFixAsync(FixAllContext fixAllContext)
            {
                var batchFixer          = (BatchFixAllProvider)WellKnownFixAllProviders.BatchFixer;
                var fixMultipleContext  = fixAllContext as FixMultipleContext;
                var suppressionFixer    = (AbstractSuppressionCodeFixProvider)((WrapperCodeFixProvider)fixAllContext.CodeFixProvider).SuppressionFixProvider;
                var isGlobalSuppression = NestedSuppressionCodeAction.IsEquivalenceKeyForGlobalSuppression(fixAllContext.CodeActionEquivalenceKey);

                if (!isGlobalSuppression)
                {
                    var isPragmaWarningSuppression = NestedSuppressionCodeAction.IsEquivalenceKeyForPragmaWarning(fixAllContext.CodeActionEquivalenceKey);
                    Contract.ThrowIfFalse(isPragmaWarningSuppression || NestedSuppressionCodeAction.IsEquivalenceKeyForRemoveSuppression(fixAllContext.CodeActionEquivalenceKey));

                    batchFixer = isPragmaWarningSuppression ?
                                 new PragmaWarningBatchFixAllProvider(suppressionFixer) :
                                 RemoveSuppressionCodeAction.GetBatchFixer(suppressionFixer);
                }

                var title = fixAllContext.CodeActionEquivalenceKey;

                if (fixAllContext.Document != null)
                {
                    var documentsAndDiagnosticsToFixMap = fixMultipleContext != null ?
                                                          fixMultipleContext.DocumentDiagnosticsToFix :
                                                          await batchFixer.GetDocumentDiagnosticsToFixAsync(fixAllContext).ConfigureAwait(false);

                    return(!isGlobalSuppression ?
                           await batchFixer.GetFixAsync(documentsAndDiagnosticsToFixMap, fixAllContext).ConfigureAwait(false) :
                           GlobalSuppressMessageFixAllCodeAction.Create(title, suppressionFixer, fixAllContext.Document, documentsAndDiagnosticsToFixMap));
                }
                else
                {
                    var projectsAndDiagnosticsToFixMap = fixMultipleContext != null ?
                                                         fixMultipleContext.ProjectDiagnosticsToFix :
                                                         await batchFixer.GetProjectDiagnosticsToFixAsync(fixAllContext).ConfigureAwait(false);

                    return(!isGlobalSuppression ?
                           await batchFixer.GetFixAsync(projectsAndDiagnosticsToFixMap, fixAllContext).ConfigureAwait(false) :
                           GlobalSuppressMessageFixAllCodeAction.Create(title, suppressionFixer, fixAllContext.Project, projectsAndDiagnosticsToFixMap));
                }
            }
示例#7
0
 private static CodeAction CreateGlobalSuppressionFixAllAction(string title, AbstractSuppressionCodeFixProvider fixer, Project triggerProject, ImmutableDictionary <Project, ImmutableArray <Diagnostic> > diagnosticsByProject)
 {
     return(new CodeAction.SolutionChangeAction(title,
                                                ct => GlobalSuppressMessageFixAllCodeAction.CreateChangedSolutionAsync(fixer, triggerProject, diagnosticsByProject, ct),
                                                equivalenceKey: title));
 }