private IEnumerable <CodeActionOperation> GetOperationsToCreateSuppressionFile(Project project)
        {
            string       suppressionFileName    = project.AssemblyName + SuppressionFile.SuppressionFileExtension;
            TextDocument projectSuppressionFile =
                project.AdditionalDocuments.FirstOrDefault(d => string.Equals(suppressionFileName, d.Name, StringComparison.OrdinalIgnoreCase)) ??
                SuppressionManager.CreateRoslynAdditionalFile(project);

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

            Solution changedSolution = projectSuppressionFile.Project.Solution;

            if (SuppressionManager.Instance?.BuildActionSetter != null)
            {
                return(new CodeActionOperation[]
                {
                    new ApplyChangesOperation(changedSolution),
                    new LoadNewSuppressionFileOperation(projectSuppressionFile.FilePath, project.AssemblyName),
                    new ChangesBuildActionOperation(project.AssemblyName)
                });
            }
            else
            {
                return(new CodeActionOperation[]
                {
                    new ApplyChangesOperation(changedSolution),
                    new LoadNewSuppressionFileOperation(projectSuppressionFile.FilePath, project.AssemblyName)
                });
            }
        }
Beispiel #2
0
        protected virtual CodeAction GetCodeActionToRegister(Diagnostic diagnostic, CodeFixContext context)
        {
            if (!SuppressionManager.CheckIfInstanceIsInitialized(throwOnNotInitialized: false))
            {
                return(GetSuppressWithCommentCodeAction(diagnostic, context, isNested: false));
            }

            string groupCodeActionNameFormat = nameof(Resources.SuppressDiagnosticGroupCodeActionTitle).GetLocalized().ToString();
            string groupCodeActionName       = string.Format(groupCodeActionNameFormat, diagnostic.Id);

            CodeAction suppressWithCommentCodeAction         = GetSuppressWithCommentCodeAction(diagnostic, context, isNested: true);
            CodeAction suppressWithSuppressionFileCodeAction = GetSuppressWithSuppressionFileCodeAction(diagnostic, context, isNested: true);
            var        suppressionCodeActions = ImmutableArray.CreateBuilder <CodeAction>(initialCapacity: 2);

            if (suppressWithCommentCodeAction != null)
            {
                suppressionCodeActions.Add(suppressWithCommentCodeAction);
            }

            if (suppressWithSuppressionFileCodeAction != null)
            {
                suppressionCodeActions.Add(suppressWithSuppressionFileCodeAction);
            }

            return(CodeActionWithNestedActionsFabric.CreateCodeActionWithNestedActions(groupCodeActionName, suppressionCodeActions.ToImmutable()));
        }
        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);
            }
        }
        private async System.Threading.Tasks.Task SetupSuppressionManagerAsync()
        {
            var workspace = await this.GetVSWorkspaceAsync();

            SuppressionManager.InitOrReset(workspace, generateSuppressionBase: false,
                                           errorProcessorFabric: () => new VsixIOErrorProcessor(),
                                           buildActionSetterFabric: () => new VsixBuildActionSetter());
        }
Beispiel #5
0
        public void Ctor_ThrowDuringRefresh_Suppressed()
        {
            // Arrange
            activeSolutionBoundTrackerMock.SetupGet(x => x.IsActiveSolutionBound).Throws <InvalidCastException>();

            // Act
            var suppressionManager = new SuppressionManager(configurableServiceProvider);

            // Assert
            activeSolutionBoundTrackerMock.Verify(x => x.IsActiveSolutionBound, Times.Once);
            sonarLintOutputMock.Verify(x => x.Write(It.IsAny <string>()), Times.Once);
        }
Beispiel #6
0
        public void Ctor_CallsRefresh()
        {
            // Arrange
            activeSolutionBoundTrackerMock.SetupGet(x => x.IsActiveSolutionBound).Returns(false);

            // Act
            var suppressionManager = new SuppressionManager(configurableServiceProvider);

            // Assert
            activeSolutionBoundTrackerMock.Verify(x => x.IsActiveSolutionBound, Times.Once);
            sonarLintOutputMock.Verify(x => x.Write(It.IsAny <string>()), Times.Never);
        }
        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)
                });
            }
        }
        protected override void Initialize()
        {
            base.Initialize();
            this.InitializeSqm();

            IServiceProvider serviceProvider = this;

            this.sonarAnalyzerManager = new SonarAnalyzerManager(serviceProvider);
            this.suppressionManager   = new SuppressionManager(serviceProvider, new TimerFactory());
            this.usageAnalyzer        = new BoundSolutionAnalyzer(serviceProvider);
            this.commandManager       = new PackageCommandManager(serviceProvider);

            this.commandManager.Initialize();
        }
Beispiel #9
0
        public void ChangingSolution_TriggersRefresh()
        {
            // Arrange
            activeSolutionBoundTrackerMock.SetupGet(x => x.IsActiveSolutionBound).Returns(false);
            var suppressionManager = new SuppressionManager(configurableServiceProvider);

            activeSolutionBoundTrackerMock.ResetCalls();

            // Act
            activeSolutionBoundTrackerMock.Raise(x => x.SolutionBindingChanged += null, new ActiveSolutionBindingEventArgs(false, "dummy key"));

            // Assert
            activeSolutionBoundTrackerMock.Verify(x => x.IsActiveSolutionBound, Times.Once);
        }
Beispiel #10
0
        public void Disposing_UnregistersEventHandler()
        {
            // Arrange
            activeSolutionBoundTrackerMock.SetupGet(x => x.IsActiveSolutionBound).Returns(false);
            var suppressionManager = new SuppressionManager(configurableServiceProvider);

            activeSolutionBoundTrackerMock.ResetCalls();

            // Act
            suppressionManager.Dispose();

            // Assert
            // Raise the event - should not trigger any action
            activeSolutionBoundTrackerMock.Raise(x => x.SolutionBindingChanged += null, new ActiveSolutionBindingEventArgs(false, "dummy key"));
            activeSolutionBoundTrackerMock.Verify(x => x.IsActiveSolutionBound, Times.Never);
        }
        protected override void Initialize()
        {
            base.Initialize();
            this.InitializeSqm();

            IServiceProvider serviceProvider = this;

            this.sonarAnalyzerManager = new SonarAnalyzerManager(serviceProvider);
            this.suppressionManager   = new SuppressionManager(serviceProvider);
            this.usageAnalyzer        = new BoundSolutionAnalyzer(serviceProvider);

            this.commandManager = new PackageCommandManager(serviceProvider);
            this.commandManager.Initialize();

            this.deprecationManager = new DeprecationManager(this.GetMefService <IInfoBarManager>(),
                                                             this.GetMefService <ISonarLintOutput>());
            this.deprecationManager.Initialize(VisualStudioHelpers.VisualStudioVersion);
        }
        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);
            }
        }
Beispiel #13
0
 private void SetupSuppressionManager()
 {
     SuppressionManager.InitOrReset(_vsWorkspace, generateSuppressionBase: false,
                                    errorProcessorFabric: () => new VsixIOErrorProcessor(),
                                    buildActionSetterFabric: () => new VsixBuildActionSetter());
 }