Example #1
0
        public override async Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext)
        {
            if (fixAllContext.Document != null)
            {
                var documentsAndDiagnosticsToFixMap = await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(false);

                return(await GetFixAsync(documentsAndDiagnosticsToFixMap, fixAllContext).ConfigureAwait(false));
            }
            else
            {
                var projectsAndDiagnosticsToFixMap = await fixAllContext.GetProjectDiagnosticsToFixAsync().ConfigureAwait(false);

                return(await GetFixAsync(projectsAndDiagnosticsToFixMap, fixAllContext).ConfigureAwait(false));
            }
        }
            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));
                }
            }
Example #3
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;
            }
            public async override Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
            {
                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);
                }
            }
                public override Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext)
                {
                    var solution = fixAllContext.Solution;

                    return(Task.FromResult <CodeAction?>(CodeAction.Create(
                                                             "Remove default case",
                                                             async cancellationToken =>
                    {
                        var toFix = await fixAllContext.GetDocumentDiagnosticsToFixAsync();
                        Project?project = null;
                        foreach (var kvp in toFix)
                        {
                            var document = kvp.Key;
                            project ??= document.Project;
                            var diagnostics = kvp.Value;
                            var root = await document.GetSyntaxRootAsync(cancellationToken);
                            Assumes.NotNull(root);
                            foreach (var diagnostic in diagnostics)
                            {
                                var sourceTree = diagnostic.Location.SourceTree;
                                Assumes.NotNull(sourceTree);
                                var node = (await sourceTree.GetRootAsync(cancellationToken)).FindNode(diagnostic.Location.SourceSpan);
                                Assumes.NotNull(node?.Parent);
                                var newRoot = root.RemoveNode(node.Parent, SyntaxRemoveOptions.KeepNoTrivia);
                                Assumes.NotNull(newRoot);
                                document = document.WithSyntaxRoot(newRoot);
                            }

                            solution = solution.WithDocumentText(document.Id, await document.GetTextAsync());
                        }

                        Assumes.NotNull(project);
                        return solution.AddDocument(DocumentId.CreateNewId(project.Id), "new.cs", SourceText.From(""));
                    },
                                                             nameof(TestThirdPartyCodeFix))));
                }
 public sealed override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
 {
     var documentsAndDiagnosticsToFixMap = await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(false);
     return await GetFixAsync(documentsAndDiagnosticsToFixMap, fixAllContext.State, fixAllContext.CancellationToken).ConfigureAwait(false);
 }
Example #7
0
        public sealed override async Task <CodeAction> GetFixAsync(FixAllContext fixAllContext)
        {
            var documentsAndDiagnosticsToFixMap = await fixAllContext.GetDocumentDiagnosticsToFixAsync().ConfigureAwait(false);

            return(await GetFixAsync(documentsAndDiagnosticsToFixMap, fixAllContext.State, fixAllContext.CancellationToken).ConfigureAwait(false));
        }