Skip to content

Commit

Permalink
handle raw strings when indentation changes from tabs to spaces (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
belav authored Feb 17, 2024
1 parent 3a3807f commit 1dd1674
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
42 changes: 42 additions & 0 deletions Src/CSharpier.Tests/SyntaxNodeComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,48 @@ public void RawStringLiterals_Work_With_Moving_Indentation_3()
result.Should().BeEmpty();
}

[Test]
public void RawStringLiterals_Work_With_Moving_Indentation_And_Tabs_To_Spaces()
{
var left = """"
var someValue = $"""
SomeRawStringWithTab
""";
"""";
var right = """"
var someValue = $"""
SomeRawStringWithTab
""";
"""";

var result = CompareSource(left, right);

result.Should().BeEmpty();
}

[Test]
public void RawStringLiterals_Errors_When_Tabs_Change_To_Spaces()
{
var left = """"
var someValue = $"""
SomeRawStringWithTab
""";
"""";
var right = """"
var someValue = $"""
SomeRawStringWithTab
""";
"""";

var result = CompareSource(left, right);

result.Should().NotBeEmpty();
}

[Test]
public void RawStringLiterals_Error_With_Adding_Indentation_When_There_Was_None()
{
Expand Down
7 changes: 5 additions & 2 deletions Src/CSharpier/SyntaxNodeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,12 @@ originalToken.Parent is InterpolatedStringExpressionSyntax
)
{
// this detects if we added indentation when there was none, or removed all indentation when there was some
// and handles the case of changing /t to " "
var originalFirst = originalToken.ValueText.TrimStart(['\r', '\n'])[0];
var formattedFirst = formattedToken.ValueText.TrimStart(['\r', '\n'])[0];
if (
originalToken.ValueText.TrimStart(['\r', '\n'])[0]
!= formattedToken.ValueText.TrimStart(['\r', '\n'])[0]
originalFirst != formattedFirst
&& (originalFirst is not (' ' or '\t') || formattedFirst is not (' ' or '\t'))
)
{
return NotEqual(originalToken.Span, formattedNode!.Span);
Expand Down

0 comments on commit 1dd1674

Please sign in to comment.