Ejemplo n.º 1
0
        private async Task <List <CodeFixCollection> > AppendFixesOrSuppressionsAsync(
            Document document,
            TextSpan span,
            IEnumerable <DiagnosticData> diagnosticsWithSameSpan,
            List <CodeFixCollection> result,
            object fixer,
            Func <Diagnostic, bool> hasFix,
            Func <ImmutableArray <Diagnostic>, Task <IEnumerable <CodeFix> > > getFixes,
            CancellationToken cancellationToken)
        {
            var diagnostics = (await diagnosticsWithSameSpan.OrderByDescending(d => d.Severity).ToDiagnosticsAsync(document.Project, cancellationToken).ConfigureAwait(false)).Where(d => hasFix(d)).ToImmutableArray();

            if (diagnostics.Length <= 0)
            {
                // this can happen for suppression case where all diagnostics can't be suppressed
                return(result);
            }

            var extensionManager = document.Project.Solution.Workspace.Services.GetService <IExtensionManager>();
            var fixes            = await extensionManager.PerformFunctionAsync(fixer, () => getFixes(diagnostics), defaultValue : SpecializedCollections.EmptyEnumerable <CodeFix>()).ConfigureAwait(false);

            if (fixes != null && fixes.Any())
            {
                // If the fix provider supports fix all occurrences, then get the corresponding FixAllProviderInfo and fix all context.
                var fixAllProviderInfo = extensionManager.PerformFunction(fixer, () => ImmutableInterlocked.GetOrAdd(ref _fixAllProviderMap, fixer, FixAllProviderInfo.Create), defaultValue: null);

                FixAllProvider            fixAllProvider  = null;
                FixAllContext             fixAllContext   = null;
                IEnumerable <FixAllScope> supportedScopes = null;
                if (fixAllProviderInfo != null)
                {
                    var codeFixProvider = (fixer as CodeFixProvider) ?? new WrapperCodeFixProvider((ISuppressionFixProvider)fixer, diagnostics.Select(d => d.Id));
                    fixAllProvider = fixAllProviderInfo.FixAllProvider;
                    fixAllContext  = FixAllContext.Create(
                        document, fixAllProviderInfo, codeFixProvider, diagnostics,
                        this.GetDocumentDiagnosticsAsync, this.GetProjectDiagnosticsAsync, cancellationToken);
                    supportedScopes = fixAllProviderInfo.SupportedScopes;
                }

                result = result ?? new List <CodeFixCollection>();
                var codeFix = new CodeFixCollection(
                    fixer, span, fixes, fixAllProvider, fixAllContext,
                    supportedScopes, diagnostics.First());
                result.Add(codeFix);
            }

            return(result);
        }