internal override async Task <IEnumerable <Tuple <Diagnostic, CodeFixCollection> > > GetDiagnosticAndFixesAsync(TestWorkspace workspace, string fixAllActionId)
        {
            var providerAndFixer = CreateDiagnosticProviderAndFixer(workspace);

            var      provider = providerAndFixer.Item1;
            Document document;
            TextSpan span;
            string   annotation = null;

            if (!TryGetDocumentAndSelectSpan(workspace, out document, out span))
            {
                document = GetDocumentAndAnnotatedSpan(workspace, out annotation, out span);
            }

            using (var testDriver = new TestDiagnosticAnalyzerDriver(document.Project, provider, includeSuppressedDiagnostics: IncludeSuppressedDiagnostics))
            {
                var fixer       = providerAndFixer.Item2;
                var diagnostics = (await testDriver.GetAllDiagnosticsAsync(provider, document, span))
                                  .Where(d => fixer.CanBeSuppressedOrUnsuppressed(d));

                var filteredDiagnostics = FilterDiagnostics(diagnostics);

                var wrapperCodeFixer = new WrapperCodeFixProvider(fixer, filteredDiagnostics.Select(d => d.Id));
                return(await GetDiagnosticAndFixesAsync(filteredDiagnostics, provider, wrapperCodeFixer, testDriver, document, span, annotation, fixAllActionId));
            }
        }
Exemplo n.º 2
0
        internal override async Task <IEnumerable <Tuple <Diagnostic, CodeFixCollection> > > GetDiagnosticAndFixesAsync(TestWorkspace workspace, string fixAllActionId)
        {
            var providerAndFixer = GetOrCreateDiagnosticProviderAndFixer(workspace);

            var      provider = providerAndFixer.Item1;
            Document document;
            TextSpan span;
            string   annotation = null;

            if (!TryGetDocumentAndSelectSpan(workspace, out document, out span))
            {
                document = GetDocumentAndAnnotatedSpan(workspace, out annotation, out span);
            }

            using (var testDriver = new TestDiagnosticAnalyzerDriver(document.Project, provider))
            {
                var diagnostics = await testDriver.GetAllDiagnosticsAsync(provider, document, span);

                AssertNoAnalyzerExceptionDiagnostics(diagnostics);

                var fixer = providerAndFixer.Item2;
                var ids   = new HashSet <string>(fixer.FixableDiagnosticIds);
                var dxs   = diagnostics.Where(d => ids.Contains(d.Id)).ToList();
                return(await GetDiagnosticAndFixesAsync(dxs, provider, fixer, testDriver, document, span, annotation, fixAllActionId));
            }
        }
        internal override async Task <(ImmutableArray <Diagnostic>, ImmutableArray <CodeAction>, CodeAction actionToInvoke)> GetDiagnosticAndFixesAsync(
            TestWorkspace workspace, TestParameters parameters)
        {
            var providerAndFixer = CreateDiagnosticProviderAndFixer(workspace);

            var    provider   = providerAndFixer.Item1;
            string annotation = null;

            if (!TryGetDocumentAndSelectSpan(workspace, out var document, out var span))
            {
                document = GetDocumentAndAnnotatedSpan(workspace, out annotation, out span);
            }

            var testDriver  = new TestDiagnosticAnalyzerDriver(document.Project, provider, includeSuppressedDiagnostics: IncludeSuppressedDiagnostics);
            var fixer       = providerAndFixer.Item2;
            var diagnostics = (await testDriver.GetAllDiagnosticsAsync(provider, document, span))
                              .Where(d => fixer.CanBeSuppressedOrUnsuppressed(d));

            var filteredDiagnostics = FilterDiagnostics(diagnostics);

            var wrapperCodeFixer = new WrapperCodeFixProvider(fixer, filteredDiagnostics.Select(d => d.Id));

            return(await GetDiagnosticAndFixesAsync(
                       filteredDiagnostics, provider, wrapperCodeFixer, testDriver,
                       document, span, annotation, parameters.index));
        }
Exemplo n.º 4
0
        internal override async Task <(ImmutableArray <Diagnostic>, ImmutableArray <CodeAction>, CodeAction actionToInvoke)> GetDiagnosticAndFixesAsync(
            TestWorkspace workspace,
            TestParameters parameters
            )
        {
            AddAnalyzersToWorkspace(workspace);

            string annotation = null;

            if (!TryGetDocumentAndSelectSpan(workspace, out var document, out var span))
            {
                document = GetDocumentAndAnnotatedSpan(workspace, out annotation, out span);
            }

            // Include suppressed diagnostics as they are needed by unnecessary suppressions analyzer.
            var testDriver = new TestDiagnosticAnalyzerDriver(
                workspace,
                document.Project,
                includeSuppressedDiagnostics: true
                );
            var diagnostics = await testDriver.GetAllDiagnosticsAsync(document, span);

            // Filter out suppressed diagnostics before invoking code fix.
            diagnostics = diagnostics.Where(d => !d.IsSuppressed);

            return(await GetDiagnosticAndFixesAsync(
                       diagnostics,
                       CodeFixProvider,
                       testDriver,
                       document,
                       span,
                       annotation,
                       parameters.index
                       ));
        }
Exemplo n.º 5
0
            private async Task <IEnumerable <Diagnostic> > GetProjectDiagnosticsAsync(
                Project project, bool includeAllDocumentDiagnostics, CancellationToken cancellationToken)
            {
                var diags = includeAllDocumentDiagnostics
                    ? await _testDriver.GetAllDiagnosticsAsync(project)
                    : await _testDriver.GetProjectDiagnosticsAsync(project);

                diags = diags.Where(diag => _diagnosticIds.Contains(diag.Id));
                return(diags);
            }
Exemplo n.º 6
0
        private static FixAllState GetFixAllState(
            FixAllProvider fixAllProvider,
            IEnumerable <Diagnostic> diagnostics,
            DiagnosticAnalyzer provider,
            CodeFixProvider fixer,
            TestDiagnosticAnalyzerDriver testDriver,
            Document document,
            FixAllScope scope,
            string fixAllActionId)
        {
            Assert.NotEmpty(diagnostics);

            if (scope == FixAllScope.Custom)
            {
                // Bulk fixing diagnostics in selected scope.
                var diagnosticsToFix = ImmutableDictionary.CreateRange(SpecializedCollections.SingletonEnumerable(KeyValuePair.Create(document, diagnostics.ToImmutableArray())));
                return(FixAllState.Create(fixAllProvider, diagnosticsToFix, fixer, fixAllActionId));
            }

            var diagnostic = diagnostics.First();
            Func <Document, ImmutableHashSet <string>, CancellationToken, Task <IEnumerable <Diagnostic> > > getDocumentDiagnosticsAsync =
                async(d, diagIds, c) =>
            {
                var root = await d.GetSyntaxRootAsync();

                var diags = await testDriver.GetDocumentDiagnosticsAsync(provider, d, root.FullSpan);

                diags = diags.Where(diag => diagIds.Contains(diag.Id));
                return(diags);
            };

            Func <Project, bool, ImmutableHashSet <string>, CancellationToken, Task <IEnumerable <Diagnostic> > > getProjectDiagnosticsAsync =
                async(p, includeAllDocumentDiagnostics, diagIds, c) =>
            {
                var diags = includeAllDocumentDiagnostics
                        ? await testDriver.GetAllDiagnosticsAsync(provider, p)
                        : await testDriver.GetProjectDiagnosticsAsync(provider, p);

                diags = diags.Where(diag => diagIds.Contains(diag.Id));
                return(diags);
            };

            var diagnosticIds            = ImmutableHashSet.Create(diagnostic.Id);
            var fixAllDiagnosticProvider = new FixAllState.FixAllDiagnosticProvider(diagnosticIds, getDocumentDiagnosticsAsync, getProjectDiagnosticsAsync);

            return(diagnostic.Location.IsInSource
                ? new FixAllState(fixAllProvider, document, fixer, scope, fixAllActionId, diagnosticIds, fixAllDiagnosticProvider)
                : new FixAllState(fixAllProvider, document.Project, fixer, scope, fixAllActionId, diagnosticIds, fixAllDiagnosticProvider));
        }
Exemplo n.º 7
0
        internal override async Task <(ImmutableArray <Diagnostic>, ImmutableArray <CodeAction>, CodeAction actionToInvoke)> GetDiagnosticAndFixesAsync(
            TestWorkspace workspace, TestParameters parameters)
        {
            var(analyzer, fixer) = CreateDiagnosticProviderAndFixer(workspace);
            AddAnalyzerToWorkspace(workspace, analyzer, parameters);

            GetDocumentAndSelectSpanOrAnnotatedSpan(workspace, out var document, out var span, out var annotation);

            var testDriver  = new TestDiagnosticAnalyzerDriver(workspace, includeSuppressedDiagnostics: IncludeSuppressedDiagnostics);
            var diagnostics = (await testDriver.GetAllDiagnosticsAsync(document, span))
                              .Where(d => fixer.IsFixableDiagnostic(d));

            var filteredDiagnostics = FilterDiagnostics(diagnostics);

            var wrapperCodeFixer = new WrapperCodeFixProvider(fixer, filteredDiagnostics.Select(d => d.Id));

            return(await GetDiagnosticAndFixesAsync(
                       filteredDiagnostics, wrapperCodeFixer, testDriver, document,
                       span, annotation, parameters.index));
        }