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

Enhance whitespace issue logging with a detailed TextChange message #1017

Merged
merged 1 commit into from Mar 1, 2021
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
25 changes: 23 additions & 2 deletions src/Formatters/DocumentFormatter.cs
Expand Up @@ -159,14 +159,16 @@ private ImmutableArray<FileChange> GetFileChanges(FormatOptions formatOptions, s
}

var fileChanges = ImmutableArray.CreateBuilder<FileChange>();
var changes = formattedText.GetChangeRanges(originalText);
var changes = formattedText.GetTextChanges(originalText);

for (var index = 0; index < changes.Count; index++)
{
var change = changes[index];
var changeMessage = BuildChangeMessage(change);

var changePosition = originalText.Lines.GetLinePosition(change.Span.Start);

var fileChange = new FileChange(changePosition, FormatWarningDescription);
var fileChange = new FileChange(changePosition, $"{FormatWarningDescription}{changeMessage}");
fileChanges.Add(fileChange);

if (!formatOptions.SaveFormattedFiles || formatOptions.LogLevel == LogLevel.Trace)
Expand All @@ -176,6 +178,25 @@ private ImmutableArray<FileChange> GetFileChanges(FormatOptions formatOptions, s
}

return fileChanges.ToImmutable();

static string BuildChangeMessage(TextChange change)
{
var isDelete = string.IsNullOrEmpty(change.NewText);
var isAdd = change.Span.Length == 0;
if (isDelete && isAdd)
{
return string.Empty;
}

// Escape characters in the text changes so that it can be more easily read.
var textChange = change.NewText?.Replace(" ", "\\s").Replace("\t", "\\t").Replace("\n", "\\n").Replace("\r", "\\r");
var message = isDelete
? string.Format(Resources.Delete_0_characters, change.Span.Length)
: isAdd
? string.Format(Resources.Insert_0, textChange)
: string.Format(Resources.Replace_0_characters_with_1, change.Span.Length, textChange);
return $" {message}";
}
}

private static void LogFormattingChanges(string filePath, bool changesAreErrors, ILogger logger, string workspaceFolder, FileChange fileChange)
Expand Down
9 changes: 9 additions & 0 deletions src/Resources.resx
Expand Up @@ -312,4 +312,13 @@
<data name="Project_0_is_using_configuration_from_1" xml:space="preserve">
<value>Project {0} is using configuration from '{1}'.</value>
</data>
<data name="Delete_0_characters" xml:space="preserve">
<value>Delete {0} characters.</value>
</data>
<data name="Insert_0" xml:space="preserve">
<value>Insert '{0}'.</value>
</data>
<data name="Replace_0_characters_with_1" xml:space="preserve">
<value>Replace {0} characters with '{1}'.</value>
</data>
</root>
15 changes: 15 additions & 0 deletions src/xlf/Resources.cs.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">{0} nejde formátovat. Formátovat jde v současné době jenom projekty v jazycích C# a Visual Basic.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">Zjišťuje se diagnostika...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">Zahrňte do operací formátování vygenerované soubory kódu.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Načítá se pracovní prostor.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.de.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">"{0}" konnte nicht formatiert werden. Derzeit wird die Formatierung nur für C#- und Visual Basic-Projekte unterstützt.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">Diagnose wird ermittelt...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">Hiermit werden generierte Codedateien in Formatierungsvorgänge einbezogen.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Arbeitsbereich wird geladen.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.es.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">No se pudo dar formato a "{0}". El formato admite solo proyectos de C# y Visual Basic en este momento.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">Determinando los diagnósticos...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">Incluya los archivos de código generados en las operaciones de formato.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Cargando área de trabajo.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.fr.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">Impossible de mettre en forme '{0}'. L'opération Mettre en forme prend uniquement en charge les projets C# et Visual Basic pour le moment.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">Détermination des diagnostics...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">Ajoutez des fichiers de code générés dans les opérations de mise en forme.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Chargement de l'espace de travail.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.it.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">Non è stato possibile formattare '{0}'. Il formato supporta attualmente solo progetti C# e Visual Basic.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">Determinazione della diagnostica...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">Includere i file del codice generato nelle operazioni di formattazione.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Caricamento dell'area di lavoro.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.ja.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">'{0}' をフォーマットできませんでした。フォーマットは、C# および Visual Basic のプロジェクトでのみサポートされています。</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">診断を決定しています...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">生成されたコード ファイルを書式設定操作に含めます。</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">ワークスペースを読み込んでいます。</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.ko.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">'{0}'의 서식을 지정할 수 없습니다. 서식 지정은 현재 C# 및 Visual Basic 프로젝트만 지원합니다.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">진단을 확인하는 중...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">서식 지정 작업에서 생성된 코드 파일을 포함합니다.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">작업 영역을 로드하는 중입니다.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.pl.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">Nie można sformatować elementu „{0}”. Obecnie format obsługuje tylko projekty C# i Visual Basic.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">Trwa określanie diagnostyki...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">Uwzględnij wygenerowane pliki kodu w operacjach formatowania.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Ładowanie obszaru roboczego.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down
15 changes: 15 additions & 0 deletions src/xlf/Resources.pt-BR.xlf
Expand Up @@ -72,6 +72,11 @@
<target state="translated">Não foi possível formatar '{0}'. O formato atualmente suporta apenas projetos do Visual Basic e C#.</target>
<note />
</trans-unit>
<trans-unit id="Delete_0_characters">
<source>Delete {0} characters.</source>
<target state="new">Delete {0} characters.</target>
<note />
</trans-unit>
<trans-unit id="Determining_diagnostics">
<source>Determining diagnostics...</source>
<target state="translated">Determinando os diagnósticos...</target>
Expand Down Expand Up @@ -152,6 +157,11 @@
<target state="translated">Incluir arquivos de código gerados em operações de formatação.</target>
<note />
</trans-unit>
<trans-unit id="Insert_0">
<source>Insert '{0}'.</source>
<target state="new">Insert '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Carregando espaço de trabalho.</target>
Expand All @@ -172,6 +182,11 @@
<target state="new">Project {0} is using configuration from '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Replace_0_characters_with_1">
<source>Replace {0} characters with '{1}'.</source>
<target state="new">Replace {0} characters with '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="Required_references_did_not_load_for_0_or_referenced_project_Run_dotnet_restore_prior_to_formatting">
<source>Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</source>
<target state="new">Required references did not load for {0} or referenced project. Run `dotnet restore` prior to formatting.</target>
Expand Down