Ejemplo n.º 1
0
 internal static ProjectAnalysisResult Create(
     Project project,
     ImmutableArray <Diagnostic> compilerDiagnostics,
     ImmutableArray <Diagnostic> diagnostics,
     ImmutableDictionary <DiagnosticAnalyzer, AnalyzerTelemetryInfo> telemetry)
 {
     return(new ProjectAnalysisResult(
                SimpleProjectInfo.Create(project),
                ImmutableArray.CreateRange(compilerDiagnostics, f => DiagnosticInfo.Create(f)),
                ImmutableArray.CreateRange(diagnostics, f => DiagnosticInfo.Create(f)),
                telemetry));
 }
Ejemplo n.º 2
0
        public async Task <ProjectFixResult> FixProjectAsync(Project project, CancellationToken cancellationToken = default)
        {
            (ImmutableArray <DiagnosticAnalyzer> analyzers, ImmutableArray <CodeFixProvider> fixers) = _analyzerLoader.GetAnalyzersAndFixers(project: project);

            FixResult fixResult = await FixProjectAsync(project, analyzers, fixers, cancellationToken).ConfigureAwait(false);

            project = CurrentSolution.GetProject(project.Id);

            Compilation compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

            Dictionary <string, ImmutableArray <CodeFixProvider> > fixersById = fixers
                                                                                .SelectMany(f => f.FixableDiagnosticIds.Select(id => (id, fixer: f)))
                                                                                .GroupBy(f => f.id)
                                                                                .ToDictionary(f => f.Key, g => g.Select(f => f.fixer).Distinct().ToImmutableArray());

            ImmutableArray <Diagnostic> unfixableDiagnostics = await GetDiagnosticsAsync(
                analyzers,
                fixResult.FixedDiagnostics,
                compilation,
                project.AnalyzerOptions,
                f => !fixersById.ContainsKey(f.id),
                cancellationToken)
                                                               .ConfigureAwait(false);

            ImmutableArray <Diagnostic> unfixedDiagnostics = await GetDiagnosticsAsync(
                analyzers,
                fixResult.FixedDiagnostics.Concat(unfixableDiagnostics),
                compilation,
                project.AnalyzerOptions,
                f => fixersById.ContainsKey(f.id),
                cancellationToken)
                                                             .ConfigureAwait(false);

            int numberOfAddedFileBanners = 0;

            if (Options.FileBannerLines.Any())
            {
                numberOfAddedFileBanners = await AddFileBannerAsync(CurrentSolution.GetProject(project.Id), Options.FileBannerLines, cancellationToken).ConfigureAwait(false);
            }

            ImmutableArray <DocumentId> formattedDocuments = ImmutableArray <DocumentId> .Empty;

            if (Options.Format)
            {
                formattedDocuments = await FormatProjectAsync(CurrentSolution.GetProject(project.Id), cancellationToken).ConfigureAwait(false);
            }

            var result = new ProjectFixResult(
                kind: fixResult.Kind,
                fixedDiagnostics: fixResult.FixedDiagnostics.Select(f => DiagnosticInfo.Create(f)),
                unfixedDiagnostics: unfixedDiagnostics.Select(f => DiagnosticInfo.Create(f)),
                unfixableDiagnostics: unfixableDiagnostics.Select(f => DiagnosticInfo.Create(f)),
                numberOfFormattedDocuments: (Options.FileBannerLines.Any()) ? formattedDocuments.Length : -1,
                numberOfAddedFileBanners: (Options.Format) ? numberOfAddedFileBanners : -1);

            LogHelpers.WriteFixSummary(
                fixResult.FixedDiagnostics,
                unfixedDiagnostics,
                unfixableDiagnostics,
                baseDirectoryPath: Path.GetDirectoryName(project.FilePath),
                indentation: "  ",
                formatProvider: FormatProvider,
                verbosity: Verbosity.Detailed);

            return(result);
        }