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

Only make whitespace visible on otherwise empty lines or if it is trailing. #954

Merged
merged 2 commits into from
Sep 3, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Src/CSharpier.Tests/Utilities/StringDifferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,100 @@ public void PrintDifference_Should_Print_Multi_Line_Difference()
);
}

[Test]
public void PrintDifference_Should_Show_Extra_Whitespace_On_Empty_Line()
{
var result = PrintDifference(
@"public class ClassName
{
private string field1;

private string field2;
}",
@"public class ClassName
{
private string field1;

private string field2;
}"
);

result
.Should()
.Be(
@"----------------------------- Expected: Around Line 4 -----------------------------
private string field1;

private string field2;
----------------------------- Actual: Around Line 4 -----------------------------
private string field1;
····
private string field2;
"
);
}

[Test]
public void PrintDifference_Should_Show_Extra_Whitespace_At_End_Of_Line()
{
var result = PrintDifference(
@"public class ClassName
{
private string field1;
}",
@"public class ClassName
{
private string field1;
}"
);

result
.Should()
.Be(
@"----------------------------- Expected: Around Line 3 -----------------------------
{
private string field1;
}
----------------------------- Actual: Around Line 3 -----------------------------
{
private string field1;····
}
"
);
}

[Test]
public void PrintDifference_Should_Not_Show_Whitespace_In_Middle_Of_Line()
{
var result = PrintDifference("public class ClassName { }", "public class ClassName { }");

result
.Should()
.Be(
@"----------------------------- Expected: Around Line 1 -----------------------------
public class ClassName { }
----------------------------- Actual: Around Line 1 -----------------------------
public class ClassName { }
"
);
}

[Test]
public void PrintDifference_Should_Show_Extra_Whitespace_After_Line()
{
var result = PrintDifference("public class ClassName { }", "public class ClassName { } ");

result
.Should()
.Be(
@"----------------------------- Expected: Around Line 1 -----------------------------
public class ClassName { }
----------------------------- Actual: Around Line 1 -----------------------------
public class ClassName { }·
"
);
}

[Test]
public void PrintDifference_Should_Print_Extra_Line_Difference()
{
Expand Down
46 changes: 40 additions & 6 deletions Src/CSharpier/Utilities/StringDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,48 @@ public static string PrintFirstDifference(string expected, string actual)
);
if (previousExpectedLine != null)
{
stringBuilder.AppendLine(MakeWhiteSpaceVisible(previousExpectedLine));
stringBuilder.AppendLine(previousExpectedLine);
}
stringBuilder.AppendLine(MakeWhiteSpaceVisible(expectedLine));

stringBuilder.AppendLine(expectedLine);
var nextExpectedLine = expectedReader.ReadLine();
if (nextExpectedLine != null)
{
stringBuilder.AppendLine(MakeWhiteSpaceVisible(nextExpectedLine));
stringBuilder.AppendLine(nextExpectedLine);
}

stringBuilder.AppendLine(
$"----------------------------- Actual: Around Line {line} -----------------------------"
);
if (previousActualLine != null)
{
stringBuilder.AppendLine(MakeWhiteSpaceVisible(previousActualLine));
stringBuilder.AppendLine(previousActualLine);
}

if (string.IsNullOrWhiteSpace(actualLine))
{
stringBuilder.AppendLine(MakeWhiteSpaceVisible(actualLine));
}
stringBuilder.AppendLine(MakeWhiteSpaceVisible(actualLine));
else
{
if (actualLine[^1] is ' ' or '\t')
{
var lastNonWhitespace = FindIndexOfNonWhitespace(actualLine);
stringBuilder.AppendLine(
actualLine[..lastNonWhitespace]
+ MakeWhiteSpaceVisible(actualLine[lastNonWhitespace..])
);
}
else
{
stringBuilder.AppendLine(actualLine);
}
}

var nextActualLine = actualReader.ReadLine();
if (nextActualLine != null)
{
stringBuilder.AppendLine(MakeWhiteSpaceVisible(nextActualLine));
stringBuilder.AppendLine(nextActualLine);
}

return stringBuilder.ToString();
Expand All @@ -91,6 +112,19 @@ public static string PrintFirstDifference(string expected, string actual)
return "The file contained different line endings than formatting it would result in.";
}

private static int FindIndexOfNonWhitespace(string input)
{
for (var x = input.Length - 1; x >= 0; x--)
{
if (input[x] is not (' ' or '\t'))
{
return x;
}
}

return -1;
}

private static string? MakeWhiteSpaceVisible(string? value)
{
return value?.Replace(' ', '·').Replace('\t', '→');
Expand Down
Loading