private async Task <ImmutableArray <CodeFix> > GetSuppressionsAsync( Document documentOpt, Project project, IEnumerable <Diagnostic> diagnostics, SuppressionTargetInfo suppressionTargetInfo, bool skipSuppressMessage, bool skipUnsuppress, CancellationToken cancellationToken) { // We only care about diagnostics that can be suppressed/unsuppressed. diagnostics = diagnostics.Where(CanBeSuppressedOrUnsuppressed); if (diagnostics.IsEmpty()) { return(ImmutableArray <CodeFix> .Empty); } if (!skipSuppressMessage) { var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); var suppressMessageAttribute = compilation.SuppressMessageAttributeType(); skipSuppressMessage = suppressMessageAttribute == null || !suppressMessageAttribute.IsAttribute(); } var result = ArrayBuilder <CodeFix> .GetInstance(); foreach (var diagnostic in diagnostics) { if (!diagnostic.IsSuppressed) { var nestedActions = ArrayBuilder <NestedSuppressionCodeAction> .GetInstance(); if (diagnostic.Location.IsInSource && documentOpt != null) { // pragma warning disable. nestedActions.Add(PragmaWarningCodeAction.Create(suppressionTargetInfo, documentOpt, diagnostic, this)); } // SuppressMessageAttribute suppression is not supported for compiler diagnostics. if (!skipSuppressMessage && !SuppressionHelpers.IsCompilerDiagnostic(diagnostic)) { // global assembly-level suppress message attribute. nestedActions.Add(new GlobalSuppressMessageCodeAction(suppressionTargetInfo.TargetSymbol, project, diagnostic, this)); } if (nestedActions.Count > 0) { var codeAction = new TopLevelSuppressionCodeAction( diagnostic, nestedActions.ToImmutableAndFree()); result.Add(new CodeFix(project, codeAction, diagnostic)); } } else if (!skipUnsuppress) { var codeAction = await RemoveSuppressionCodeAction.CreateAsync(suppressionTargetInfo, documentOpt, project, diagnostic, this, cancellationToken).ConfigureAwait(false); if (codeAction != null) { result.Add(new CodeFix(project, codeAction, diagnostic)); } } } return(result.ToImmutableAndFree()); }
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)); } }
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 = (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)); } }
private async Task <ImmutableArray <CodeFix> > GetSuppressionsAsync( Document documentOpt, Project project, IEnumerable <Diagnostic> diagnostics, SuppressionTargetInfo suppressionTargetInfo, bool skipSuppressMessage, bool skipUnsuppress, CancellationToken cancellationToken) { // We only care about diagnostics that can be suppressed/unsuppressed. diagnostics = diagnostics.Where(IsFixableDiagnostic); if (diagnostics.IsEmpty()) { return(ImmutableArray <CodeFix> .Empty); } INamedTypeSymbol suppressMessageAttribute = null; if (!skipSuppressMessage) { var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); suppressMessageAttribute = compilation.SuppressMessageAttributeType(); skipSuppressMessage = suppressMessageAttribute == null || !suppressMessageAttribute.IsAttribute(); } var lazyFormattingOptions = (SyntaxFormattingOptions)null; var result = ArrayBuilder <CodeFix> .GetInstance(); foreach (var diagnostic in diagnostics) { if (!diagnostic.IsSuppressed) { var nestedActions = ArrayBuilder <NestedSuppressionCodeAction> .GetInstance(); if (diagnostic.Location.IsInSource && documentOpt != null) { // pragma warning disable. lazyFormattingOptions ??= await SyntaxFormattingOptions.FromDocumentAsync(documentOpt, cancellationToken).ConfigureAwait(false); nestedActions.Add(PragmaWarningCodeAction.Create(suppressionTargetInfo, documentOpt, lazyFormattingOptions, diagnostic, this)); } // SuppressMessageAttribute suppression is not supported for compiler diagnostics. if (!skipSuppressMessage && SuppressionHelpers.CanBeSuppressedWithAttribute(diagnostic)) { // global assembly-level suppress message attribute. nestedActions.Add(new GlobalSuppressMessageCodeAction( suppressionTargetInfo.TargetSymbol, suppressMessageAttribute, project, diagnostic, this)); // local suppress message attribute // please note that in order to avoid issues with existing unit tests referencing the code fix // by their index this needs to be the last added to nestedActions if (suppressionTargetInfo.TargetMemberNode != null && suppressionTargetInfo.TargetSymbol.Kind != SymbolKind.Namespace) { nestedActions.Add(new LocalSuppressMessageCodeAction( this, suppressionTargetInfo.TargetSymbol, suppressMessageAttribute, suppressionTargetInfo.TargetMemberNode, documentOpt, diagnostic)); } } if (nestedActions.Count > 0) { var codeAction = new TopLevelSuppressionCodeAction( diagnostic, nestedActions.ToImmutableAndFree()); result.Add(new CodeFix(project, codeAction, diagnostic)); } } else if (!skipUnsuppress) { var codeAction = await RemoveSuppressionCodeAction.CreateAsync(suppressionTargetInfo, documentOpt, project, diagnostic, this, cancellationToken).ConfigureAwait(false); if (codeAction != null) { result.Add(new CodeFix(project, codeAction, diagnostic)); } } } return(result.ToImmutableAndFree()); }