コード例 #1
0
        private static async Task <(CodeAction, Document)> GetFixAsync(
            ImmutableArray <Diagnostic> diagnostics,
            DiagnosticDescriptor descriptor,
            CodeFixProvider fixer,
            Project project,
            CodeFixerOptions options,
            IFormatProvider formatProvider      = null,
            CancellationToken cancellationToken = default)
        {
            if (diagnostics.Length == 1)
            {
                return(await GetFixAsync(diagnostics[0], fixer, project, options, formatProvider, cancellationToken).ConfigureAwait(false));
            }

            FixAllProvider fixAllProvider = fixer.GetFixAllProvider();

            if (fixAllProvider == null)
            {
                if (options.DiagnosticIdsFixableOneByOne.Contains(descriptor.Id))
                {
                    return(await GetFixAsync(diagnostics[0], fixer, project, options, formatProvider, cancellationToken).ConfigureAwait(false));
                }

                WriteLine($"  Diagnostic '{descriptor.Id}' cannot be fixed with '{fixer.GetType().FullName}' because it does not have FixAllProvider and '{descriptor.Id}' is not allowed to be fixed one by one.", ConsoleColors.Yellow, Verbosity.Diagnostic);
                return(default);
コード例 #2
0
        public CodeFixer(
            Solution solution,
            AnalyzerLoader analyzerLoader,
            IFormatProvider formatProvider = null,
            CodeFixerOptions options       = null)
        {
            _analyzerLoader = analyzerLoader;

            Workspace      = solution.Workspace;
            FormatProvider = formatProvider;
            Options        = options ?? CodeFixerOptions.Default;
        }
コード例 #3
0
        public static async Task <DiagnosticFix> GetFixAsync(
            ImmutableArray <Diagnostic> diagnostics,
            DiagnosticDescriptor descriptor,
            ImmutableArray <CodeFixProvider> fixers,
            Project project,
            CodeFixerOptions options,
            IFormatProvider formatProvider      = null,
            CancellationToken cancellationToken = default)
        {
            CodeFixProvider fixer    = null;
            CodeAction      fix      = null;
            Document        document = null;

            for (int i = 0; i < fixers.Length; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                (CodeAction fixCandidate, Document documentCandidate) = await GetFixAsync(
                    diagnostics,
                    descriptor,
                    fixers[i],
                    project,
                    options,
                    formatProvider,
                    cancellationToken)
                                                                        .ConfigureAwait(false);

                if (fixCandidate != null)
                {
                    if (fix == null)
                    {
                        if (options.DiagnosticFixerMap.IsEmpty ||
                            !options.DiagnosticFixerMap.TryGetValue(descriptor.Id, out string fullTypeName) ||
                            string.Equals(fixers[i].GetType().FullName, fullTypeName, StringComparison.Ordinal))
                        {
                            fix      = fixCandidate;
                            fixer    = fixers[i];
                            document = documentCandidate;
                        }
                    }
                    else if (options.DiagnosticFixerMap.IsEmpty ||
                             !options.DiagnosticFixerMap.ContainsKey(descriptor.Id))
                    {
                        LogHelpers.WriteMultipleFixersSummary(descriptor.Id, fixer, fixers[i]);
                        return(new DiagnosticFix(null, null, fixer, fixers[i]));
                    }
                }
            }

            return(new DiagnosticFix(fix, document, fixer, null));
        }
コード例 #4
0
        public CodeFixer(
            Solution solution,
            IEnumerable <AnalyzerAssembly> analyzerAssemblies = null,
            IFormatProvider formatProvider = null,
            CodeFixerOptions options       = null)
        {
            Workspace = solution.Workspace;

            if (analyzerAssemblies != null)
            {
                _analyzerAssemblies.AddRange(analyzerAssemblies);
            }

            FormatProvider = formatProvider;
            Options        = options ?? CodeFixerOptions.Default;
        }
コード例 #5
0
ファイル: CodeFixer.cs プロジェクト: Maxprofs/Roslynator
        public CodeFixer(
            Solution solution,
            AbstractSyntaxFactsServiceFactory syntaxFactsFactory,
            IEnumerable <string> analyzerAssemblies = null,
            IFormatProvider formatProvider          = null,
            CodeFixerOptions options = null)
        {
            Workspace          = solution.Workspace;
            SyntaxFactsFactory = syntaxFactsFactory;
            Options            = options ?? CodeFixerOptions.Default;

            if (analyzerAssemblies != null)
            {
                _analyzerAssemblies.LoadFrom(analyzerAssemblies);
            }

            FormatProvider = formatProvider;
        }
コード例 #6
0
        private static async Task <CodeAction> GetFixAsync(
            Diagnostic diagnostic,
            CodeFixProvider fixer,
            Project project,
            CodeFixerOptions options,
            IFormatProvider formatProvider,
            CancellationToken cancellationToken)
        {
            if (!diagnostic.Location.IsInSource)
            {
                return(null);
            }

            Document document = project.GetDocument(diagnostic.Location.SourceTree);

            Debug.Assert(document != null, "");

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

            CodeAction action = await GetFixAsync(diagnostic, fixer, document, multipleFixesInfos : default, options, cancellationToken).ConfigureAwait(false);
コード例 #7
0
        public static async Task <CodeAction> GetFixAsync(
            ImmutableArray <Diagnostic> diagnostics,
            DiagnosticDescriptor descriptor,
            CodeFixProvider fixer,
            Project project,
            CodeFixerOptions options,
            IFormatProvider formatProvider      = null,
            CancellationToken cancellationToken = default)
        {
            if (diagnostics.Length == 1)
            {
                return(await GetFixAsync(diagnostics[0], fixer, project, options, formatProvider, cancellationToken).ConfigureAwait(false));
            }

            FixAllProvider fixAllProvider = fixer.GetFixAllProvider();

            if (fixAllProvider == null)
            {
                if (options.DiagnosticIdsFixableOneByOne.Contains(descriptor.Id))
                {
                    return(await GetFixAsync(diagnostics[0], fixer, project, options, formatProvider, cancellationToken).ConfigureAwait(false));
                }

                WriteLine($"  '{fixer.GetType().FullName}' does not have FixAllProvider", ConsoleColor.Yellow, Verbosity.Diagnostic);
                return(null);
            }

            if (!fixAllProvider.GetSupportedFixAllDiagnosticIds(fixer).Any(f => f == descriptor.Id))
            {
                WriteLine($"  '{fixAllProvider.GetType().FullName}' does not support diagnostic '{descriptor.Id}'", ConsoleColor.Yellow, Verbosity.Diagnostic);
                return(null);
            }

            if (!fixAllProvider.GetSupportedFixAllScopes().Any(f => f == FixAllScope.Project))
            {
                WriteLine($"  '{fixAllProvider.GetType().FullName}' does not support scope '{FixAllScope.Project}'", ConsoleColor.Yellow, Verbosity.Diagnostic);
                return(null);
            }

            var multipleFixesInfos = new HashSet <MultipleFixesInfo>();

            CodeAction action = null;

            options.DiagnosticFixMap.TryGetValue(descriptor.Id, out string equivalenceKey);

            foreach (Diagnostic diagnostic in diagnostics)
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (!diagnostic.Location.IsInSource)
                {
                    continue;
                }

                Document document = project.GetDocument(diagnostic.Location.SourceTree);

                if (document == null)
                {
                    continue;
                }

                CodeAction fixCandidate = await GetFixAsync(diagnostic, fixer, document, multipleFixesInfos, options, cancellationToken).ConfigureAwait(false);

                if (fixCandidate == null)
                {
                    continue;
                }

                if (equivalenceKey != null &&
                    equivalenceKey != fixCandidate.EquivalenceKey)
                {
                    break;
                }

                action = fixCandidate;

                var fixAllContext = new FixAllContext(
                    document,
                    fixer,
                    FixAllScope.Project,
                    action.EquivalenceKey,
                    new string[] { descriptor.Id },
                    new FixAllDiagnosticProvider(diagnostics),
                    cancellationToken);

                CodeAction fix = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);

                if (fix != null)
                {
                    WriteLine($"  CodeFixProvider: '{fixer.GetType().FullName}'", ConsoleColor.DarkGray, Verbosity.Diagnostic);

                    if (!string.IsNullOrEmpty(action.EquivalenceKey))
                    {
                        WriteLine($"  EquivalenceKey:  '{action.EquivalenceKey}'", ConsoleColor.DarkGray, Verbosity.Diagnostic);
                    }

                    WriteLine($"  FixAllProvider:  '{fixAllProvider.GetType().FullName}'", ConsoleColor.DarkGray, Verbosity.Diagnostic);

                    return(fix);
                }

                WriteLine($"  Fixer '{fixer.GetType().FullName}' registered no action for diagnostic '{descriptor.Id}'", ConsoleColor.DarkGray, Verbosity.Diagnostic);
                WriteDiagnostics(diagnostics, baseDirectoryPath: Path.GetDirectoryName(project.FilePath), formatProvider: formatProvider, indentation: "    ", maxCount: 10, verbosity: Verbosity.Diagnostic);
            }

            return(null);
        }
コード例 #8
0
        public CodeFixer(Workspace workspace, IEnumerable <string> analyzerAssemblies = null, CodeFixerOptions options = null)
        {
            Workspace = workspace;
            Options   = options ?? CodeFixerOptions.Default;

            _analyzerFiles = new AnalyzerFileCache();

            if (analyzerAssemblies != null)
            {
                _analyzerFiles.LoadFrom(analyzerAssemblies);
            }
        }