Skip to content

Commit

Permalink
Fixing regression on csharpier ignore that deleted empty lines
Browse files Browse the repository at this point in the history
closes #879
  • Loading branch information
belav committed Apr 26, 2023
1 parent a066013 commit 6d192ce
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
75 changes: 75 additions & 0 deletions Src/CSharpier.Tests/CSharpierIgnoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using NUnit.Framework;

namespace CSharpier.Tests;

using CSharpier.SyntaxPrinter;
using FluentAssertions;

[TestFixture]
public class CSharpierIgnoreTests
{
[Test]
public void KeepLineBreaks()
{
var testCode =
@"
1
2
";

var result = PrintWithoutFormatting(testCode);

result.Should().Be(testCode);
}

[Test]
public void TrimTrailing()
{
var testCode =
@"1
2";

var result = PrintWithoutFormatting(testCode);

result
.Should()
.Be(
@"1
2"
);
}

[Test]
public void FullProperty()
{
var testCode =
@"
// csharpier-ignore
public string Example
{
get
{
if (_example is not null)
return _example;
var number = Random.Shared.Next();
return _example = number.ToString();
}
}
";

var result = PrintWithoutFormatting(testCode);

result.Should().Be(testCode);
}

private string PrintWithoutFormatting(string code)
{
return CSharpierIgnore.PrintWithoutFormatting(
code,
new FormattingContext { LineEnding = Environment.NewLine }
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,20 @@ public class AttributesAndMethods
// csharpier-ignore - just the method
void MethodThatShouldNotFormat( ) { }
}

public class KeepLineBreaks
{
// csharpier-ignore
public string Example
{
get
{
if (_example is not null)
return _example;

var number = Random.Shared.Next();

return _example = number.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,20 @@ public class AttributesAndMethods
// csharpier-ignore - just the method
void MethodThatShouldNotFormat( ) { }
}

public class KeepLineBreaks
{
// csharpier-ignore
public string Example
{
get
{
if (_example is not null)
return _example;

var number = Random.Shared.Next();

return _example = number.ToString();
}
}
}
7 changes: 6 additions & 1 deletion Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ FormattingContext context
}

public static string PrintWithoutFormatting(SyntaxNode syntaxNode, FormattingContext context)
{
return PrintWithoutFormatting(syntaxNode.GetText().ToString(), context);
}

public static string PrintWithoutFormatting(string code, FormattingContext context)
{
// trim trailing whitespace + replace only existing line endings
return Regex.Replace(syntaxNode.GetText().ToString(), @"\s*(\r\n?|\n)", context.LineEnding);
return Regex.Replace(code, @"[\t\v\f ]*(\r\n?|\n)", context.LineEnding);
}
}

0 comments on commit 6d192ce

Please sign in to comment.