diff --git a/Src/CSharpier.Tests/CSharpierIgnoreTests.cs b/Src/CSharpier.Tests/CSharpierIgnoreTests.cs new file mode 100644 index 000000000..dc4e78326 --- /dev/null +++ b/Src/CSharpier.Tests/CSharpierIgnoreTests.cs @@ -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 } + ); + } +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.expected.test index 19f0595e0..d4ad2ebe2 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.expected.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.expected.test @@ -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(); + } + } +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.test index 40d9d658b..3986d6244 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/CSharpierIgnore.test @@ -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(); + } + } +} \ No newline at end of file diff --git a/Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs b/Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs index 5ef29d401..fb4e750bd 100644 --- a/Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs +++ b/Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs @@ -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); } }