Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report formatted files and counts based based on reported issues. #776

Merged
merged 2 commits into from Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Analyzers/AnalyzerFormatter.cs
Expand Up @@ -73,13 +73,17 @@ internal class AnalyzerFormatter : ICodeFormatter
var projectDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds;
logger.LogTrace(Resources.Complete_in_0_ms, projectDiagnosticsMS);

logger.LogTrace(Resources.Fixing_diagnostics);
// Only run code fixes when we are saving changes.
if (formatOptions.SaveFormattedFiles)
{
logger.LogTrace(Resources.Fixing_diagnostics);

// Run each analyzer individually and apply fixes if possible.
solution = await FixDiagnosticsAsync(solution, analyzers, fixers, projectDiagnostics, formattablePaths, severity, includeCompilerDiagnostics, logger, cancellationToken).ConfigureAwait(false);
// Run each analyzer individually and apply fixes if possible.
solution = await FixDiagnosticsAsync(solution, analyzers, fixers, projectDiagnostics, formattablePaths, severity, includeCompilerDiagnostics, logger, cancellationToken).ConfigureAwait(false);

var fixDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds - projectDiagnosticsMS;
logger.LogTrace(Resources.Complete_in_0_ms, fixDiagnosticsMS);
var fixDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds - projectDiagnosticsMS;
logger.LogTrace(Resources.Complete_in_0_ms, fixDiagnosticsMS);
}

logger.LogTrace(Resources.Analysis_complete_in_0ms_, analysisStopwatch.ElapsedMilliseconds);

Expand Down
22 changes: 6 additions & 16 deletions src/CodeFormatter.cs
Expand Up @@ -73,22 +73,12 @@ internal static class CodeFormatter
var formatterRanMS = workspaceStopwatch.ElapsedMilliseconds - loadWorkspaceMS - determineFilesMS;
logger.LogTrace(Resources.Complete_in_0_ms, formatterRanMS);

var solutionChanges = formattedSolution.GetChanges(solution);

var filesFormatted = 0;
foreach (var projectChanges in solutionChanges.GetProjectChanges())
var documentIdsWithErrors = formattedFiles.Select(file => file.DocumentId).Distinct().ToImmutableArray();
foreach (var documentId in documentIdsWithErrors)
{
foreach (var changedDocumentId in projectChanges.GetChangedDocuments())
{
var changedDocument = solution.GetDocument(changedDocumentId);
if (changedDocument?.FilePath is null)
{
continue;
}
var documentWithError = solution.GetDocument(documentId);

logger.LogInformation(Resources.Formatted_code_file_0, changedDocument.FilePath);
filesFormatted++;
}
logger.LogInformation(Resources.Formatted_code_file_0, documentWithError!.FilePath);
}

var exitCode = 0;
Expand All @@ -104,11 +94,11 @@ internal static class CodeFormatter
ReportWriter.Write(formatOptions.ReportPath!, formattedFiles, logger);
}

logger.LogDebug(Resources.Formatted_0_of_1_files, filesFormatted, fileCount);
logger.LogDebug(Resources.Formatted_0_of_1_files, documentIdsWithErrors.Length, fileCount);

logger.LogInformation(Resources.Format_complete_in_0_ms, workspaceStopwatch.ElapsedMilliseconds);

return new WorkspaceFormatResult(filesFormatted, fileCount, exitCode);
return new WorkspaceFormatResult(documentIdsWithErrors.Length, fileCount, exitCode);
}

private static Workspace OpenFolderWorkspace(string workspacePath, SourceFileMatcher fileMatcher)
Expand Down
7 changes: 7 additions & 0 deletions src/Formatters/UnnecessaryImportsFormatter.cs
Expand Up @@ -36,6 +36,13 @@ internal sealed class UnnecessaryImportsFormatter : DocumentFormatter
return sourceText;
}

// If we are not saving files, do not make changes in this formatter. They will be
// reported by their diagnostic id when the analyzer formatter runs.
if (!formatOptions.SaveFormattedFiles)
{
return sourceText;
}

var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
if (tree is null)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Formatters/AbstractFormatterTests.cs
Expand Up @@ -138,7 +138,7 @@ protected AbstractFormatterTest()
codeStyleSeverity,
fixAnalyzers,
analyzerSeverity,
saveFormattedFiles: false,
saveFormattedFiles: true,
changesAreErrors: false,
fileMatcher,
reportPath: string.Empty,
Expand Down