From b96899ed5704e386fcfafb237b3d834905dabceb Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 25 Mar 2024 20:28:18 -0500 Subject: [PATCH] Include the "IncludeGenerated" option in the SDK (#1218) closes #1215 --- Src/CSharpier.Tests/CodeFormatterTests.cs | 28 +++++++++++++++++++++++ Src/CSharpier/CodeFormatter.cs | 3 ++- Src/CSharpier/CodeFormatterOptions.cs | 1 + Src/CSharpier/PublicAPI.Unshipped.txt | 3 ++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Src/CSharpier.Tests/CodeFormatterTests.cs b/Src/CSharpier.Tests/CodeFormatterTests.cs index 8ba58cec5..47b18c657 100644 --- a/Src/CSharpier.Tests/CodeFormatterTests.cs +++ b/Src/CSharpier.Tests/CodeFormatterTests.cs @@ -98,6 +98,34 @@ public void Format_Should_Use_EndOfLine() result.Code.Should().Be("var someVariable = someValue;\r\n"); } + [Test] + public void Format_Should_Ignore_Generated_Files() + { + var code = """ +// +var someVariable = someValue; +"""; + var result = CodeFormatter.Format(code, new CodeFormatterOptions()); + + result.Code.Should().Be(code); + } + + [Test] + public void Format_Should_Format_Generated_Files() + { + var code = """ +// +var someVariable = someValue; + +"""; + var result = CodeFormatter.Format( + code, + new CodeFormatterOptions { IncludeGenerated = true } + ); + + result.Code.Should().Be(code.Replace(" = ", " = ")); + } + [TestCase("\n")] [TestCase("\r\n")] public void Format_Should_Get_Line_Endings_With_SyntaxTree(string lineEnding) diff --git a/Src/CSharpier/CodeFormatter.cs b/Src/CSharpier/CodeFormatter.cs index c3f81ac71..f6468e530 100644 --- a/Src/CSharpier/CodeFormatter.cs +++ b/Src/CSharpier/CodeFormatter.cs @@ -26,7 +26,8 @@ public static Task FormatAsync( Width = options.Width, UseTabs = options.IndentStyle == IndentStyle.Tabs, TabWidth = options.IndentSize, - EndOfLine = options.EndOfLine + EndOfLine = options.EndOfLine, + IncludeGenerated = options.IncludeGenerated }, cancellationToken ); diff --git a/Src/CSharpier/CodeFormatterOptions.cs b/Src/CSharpier/CodeFormatterOptions.cs index a80e47c3a..ef6841089 100644 --- a/Src/CSharpier/CodeFormatterOptions.cs +++ b/Src/CSharpier/CodeFormatterOptions.cs @@ -6,6 +6,7 @@ public class CodeFormatterOptions public IndentStyle IndentStyle { get; init; } = IndentStyle.Spaces; public int IndentSize { get; init; } = 4; public EndOfLine EndOfLine { get; init; } = EndOfLine.Auto; + public bool IncludeGenerated { get; init; } } public enum IndentStyle diff --git a/Src/CSharpier/PublicAPI.Unshipped.txt b/Src/CSharpier/PublicAPI.Unshipped.txt index 5f282702b..7dd2df3aa 100644 --- a/Src/CSharpier/PublicAPI.Unshipped.txt +++ b/Src/CSharpier/PublicAPI.Unshipped.txt @@ -1 +1,2 @@ - \ No newline at end of file +CSharpier.CodeFormatterOptions.IncludeGenerated.get -> bool +CSharpier.CodeFormatterOptions.IncludeGenerated.init -> void \ No newline at end of file