Ejemplo n.º 1
0
        private static FixAllContext GetFixAllContext(
            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(FixAllContext.Create(diagnosticsToFix, fixer, fixAllActionId, CancellationToken.None));
            }

            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 FixAllContext.FixAllDiagnosticProvider(diagnosticIds, getDocumentDiagnosticsAsync, getProjectDiagnosticsAsync);

            return(diagnostic.Location.IsInSource
                ? new FixAllContext(document, fixer, scope, fixAllActionId, diagnosticIds, fixAllDiagnosticProvider, CancellationToken.None)
                : new FixAllContext(document.Project, fixer, scope, fixAllActionId, diagnosticIds, fixAllDiagnosticProvider, CancellationToken.None));
        }
        public Solution GetFix(
            ImmutableDictionary <Project, ImmutableArray <Diagnostic> > diagnosticsToFix,
            Workspace workspace,
            CodeFixProvider fixProvider,
            FixAllProvider fixAllProvider,
            string equivalenceKey,
            string waitDialogTitle,
            string waitDialogMessage,
            CancellationToken cancellationToken)
        {
            var fixMultipleContext = FixAllContext.Create(diagnosticsToFix, fixProvider, equivalenceKey, cancellationToken);
            var triggerDiagnostic  = diagnosticsToFix.First().Value.First();

            var suggestedAction = GetSuggestedAction(fixMultipleContext, triggerDiagnostic, workspace, fixAllProvider, waitDialogTitle, waitDialogMessage, showPreviewChangesDialog: false, cancellationToken: cancellationToken);

            return(suggestedAction.GetChangedSolution(cancellationToken));
        }