Skip to content

Commit

Permalink
Merge pull request #776 from JoeRobich/fix-error-check
Browse files Browse the repository at this point in the history
Report formatted files and counts based based on reported issues.
  • Loading branch information
JoeRobich committed Sep 8, 2020
2 parents 0ac7395 + 3f45da3 commit 742aa69
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
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

0 comments on commit 742aa69

Please sign in to comment.