Skip to content

Commit

Permalink
Remove empty lines at beginning of statements without braces (#980)
Browse files Browse the repository at this point in the history
* Remove empty lines at beginning of statements without braces

closes #979

* formatting files

* try again
  • Loading branch information
belav authored Nov 1, 2023
1 parent fd731c1 commit dd1594e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (true)
CallMethod();
else if (false)
CallMethod();
else
CallMethod();

for (; ; )
CallMethod();

while (true)
CallMethod();
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if (true)

CallMethod();
else if (false)

CallMethod();
else

CallMethod();

for (; ; )

CallMethod();

while (true)

CallMethod();
4 changes: 3 additions & 1 deletion Src/CSharpier/CSharpFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Src/CSharpier/DocTypes/DocUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ private static void RemoveInitialDoubleHardLine(IList<Doc> 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)
Expand Down
4 changes: 3 additions & 1 deletion Src/CSharpier/SyntaxPrinter/OptionalBraces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
}

0 comments on commit dd1594e

Please sign in to comment.