From dd1594eee6d196e7583241bde3e2c4af326a5731 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Tue, 31 Oct 2023 22:37:54 -0500 Subject: [PATCH] Remove empty lines at beginning of statements without braces (#980) * Remove empty lines at beginning of statements without braces closes #979 * formatting files * try again --- .../TestFiles/cs/OptionalBraces.expected.test | 12 ++++++++++++ .../TestFiles/cs/OptionalBraces.test | 17 +++++++++++++++++ Src/CSharpier/CSharpFormatter.cs | 4 +++- Src/CSharpier/DocTypes/DocUtilities.cs | 6 +++--- Src/CSharpier/SyntaxPrinter/OptionalBraces.cs | 4 +++- 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.expected.test create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.test diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.expected.test new file mode 100644 index 000000000..1749214d2 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.expected.test @@ -0,0 +1,12 @@ +if (true) + CallMethod(); +else if (false) + CallMethod(); +else + CallMethod(); + +for (; ; ) + CallMethod(); + +while (true) + CallMethod(); diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.test new file mode 100644 index 000000000..2690cf1a2 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/OptionalBraces.test @@ -0,0 +1,17 @@ +if (true) + + CallMethod(); +else if (false) + + CallMethod(); +else + + CallMethod(); + +for (; ; ) + + CallMethod(); + +while (true) + + CallMethod(); diff --git a/Src/CSharpier/CSharpFormatter.cs b/Src/CSharpier/CSharpFormatter.cs index 5d49fb129..2ef245efa 100644 --- a/Src/CSharpier/CSharpFormatter.cs +++ b/Src/CSharpier/CSharpFormatter.cs @@ -126,7 +126,9 @@ await syntaxTree.GetRootAsync(cancellationToken), ); formattedCode = DocPrinter.DocPrinter.Print(document, printerOptions, lineEnding); reorderedModifiers = reorderedModifiers || formattingContext2.ReorderedModifiers; - reorderedUsingsWithDisabledText = reorderedUsingsWithDisabledText || formattingContext2.ReorderedUsingsWithDisabledText; + reorderedUsingsWithDisabledText = + reorderedUsingsWithDisabledText + || formattingContext2.ReorderedUsingsWithDisabledText; } return new CodeFormatterResult diff --git a/Src/CSharpier/DocTypes/DocUtilities.cs b/Src/CSharpier/DocTypes/DocUtilities.cs index c0ff716c2..d2792bc2a 100644 --- a/Src/CSharpier/DocTypes/DocUtilities.cs +++ b/Src/CSharpier/DocTypes/DocUtilities.cs @@ -63,14 +63,14 @@ private static void RemoveInitialDoubleHardLine(IList docs, ref bool remove x++; } - - return; } - public static void RemoveInitialDoubleHardLine(Doc doc) + public static Doc RemoveInitialDoubleHardLine(Doc doc) { var removeNextHardLine = false; RemoveInitialDoubleHardLine(doc, ref removeNextHardLine); + + return doc; } private static void RemoveInitialDoubleHardLine(Doc doc, ref bool removeNextHardLine) diff --git a/Src/CSharpier/SyntaxPrinter/OptionalBraces.cs b/Src/CSharpier/SyntaxPrinter/OptionalBraces.cs index b3853758d..f489294b8 100644 --- a/Src/CSharpier/SyntaxPrinter/OptionalBraces.cs +++ b/Src/CSharpier/SyntaxPrinter/OptionalBraces.cs @@ -6,6 +6,8 @@ public static Doc Print(StatementSyntax node, FormattingContext context) { return node is BlockSyntax blockSyntax ? Block.Print(blockSyntax, context) - : Doc.Indent(Doc.HardLine, Node.Print(node, context)); + : DocUtilities.RemoveInitialDoubleHardLine( + Doc.Indent(Doc.HardLine, Node.Print(node, context)) + ); } }