protected override async Task SuppressSingleDiagnosticOnNodeAsync(DiagnosticData diagnostic, Document document, SyntaxNode syntaxRoot,
                                                                          SemanticModel semanticModel, SyntaxNode nodeWithDiagnostic)
        {
            if (diagnostic?.DataLocation?.SourceSpan == null)
            {
                return;
            }

            var(suppressionFileRoslynDoc, project) = await GetProjectAndSuppressionFileAsync(diagnostic.ProjectId);

            bool suppressionFileExists = suppressionFileRoslynDoc != null;

            if (!suppressionFileExists)
            {
                if (project == null)
                {
                    return;
                }

                SuppressionFile suppressionFile = SuppressionManager.CreateSuppressionFileForProjectFromCommand(project);
                suppressionFileExists = suppressionFile != null;
            }

            if (!suppressionFileExists ||
                !SuppressionManager.SuppressDiagnostic(semanticModel, diagnostic.Id, diagnostic.DataLocation.SourceSpan.Value,
                                                       diagnostic.DefaultSeverity, Package.DisposalToken))
            {
                ShowErrorMessage(suppressionFileRoslynDoc, project);
            }
        }
        protected override async Task <IEnumerable <CodeActionOperation> > ComputeOperationsAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            Project project = Context.Document?.Project;

            if (project == null)
            {
                return(null);
            }

            SemanticModel semanticModel = await Context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (semanticModel == null)
            {
                return(null);
            }

            SuppressionFile suppressionFile = SuppressionManager.Instance.GetSuppressionFile(project.AssemblyName);

            if (suppressionFile == null)
            {
                IEnumerable <CodeActionOperation> operationsToCreateSuppresionFile = GetOperationsToCreateSuppressionFile(project);

                if (operationsToCreateSuppresionFile == null)
                {
                    return(null);
                }

                var operationsWithSuppressionFileCreation = new List <CodeActionOperation>(4);
                operationsWithSuppressionFileCreation.AddRange(operationsToCreateSuppresionFile);
                operationsWithSuppressionFileCreation.Add(new SuppressInSuppressionFileOperation(Diagnostic, project.AssemblyName, semanticModel));
                return(operationsWithSuppressionFileCreation);
            }
            else
            {
                // For some reason the changes in suppression file will immediately reflect in the code editor
                // only if we call suppress diagnostic in code action manually, not via code action operation
                if (!SuppressionManager.SuppressDiagnostic(semanticModel, Diagnostic.Id, Diagnostic.Location.SourceSpan,
                                                           Diagnostic.DefaultSeverity, cancellationToken))
                {
                    var errorMessage = new LocalizableResourceString(nameof(Resources.DiagnosticSuppression_FailedToAddToSuppressionFile),
                                                                     Resources.ResourceManager, typeof(Resources), suppressionFile.Path);
                    System.Diagnostics.Debug.WriteLine($"{SharedConstants.PackageName.ToUpperInvariant()}: {errorMessage}");
                }

                return(new List <CodeActionOperation>(1)
                {
                    new ApplyChangesOperation(project.Solution)
                });
            }
        }
        public override void Apply(Workspace workspace, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            SuppressionFile suppressionFile = SuppressionManager.Instance.GetSuppressionFile(AssemblyName);

            if (suppressionFile == null)
            {
                ShowLocalizedError(nameof(Resources.DiagnosticSuppression_FailedToFindSuppressionFile), AssemblyName);
                return;
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (!SuppressionManager.SuppressDiagnostic(_semanticModel, _diagnosticToSuppress.Id, _diagnosticToSuppress.Location.SourceSpan,
                                                       _diagnosticToSuppress.DefaultSeverity, cancellationToken))
            {
                ShowLocalizedError(nameof(Resources.DiagnosticSuppression_FailedToAddToSuppressionFile), suppressionFile.Path);
            }
        }