Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty lines at beginning of statements without braces #980

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
);
}
}
Loading