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

Make sure empty block doesn't break on method declaration #360

Merged
merged 2 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,6 +1,13 @@
public class ClassName
{
public void DoStuff() { }
public void MethodName() { }

public void LongMethodNameKeepsEmptyBraces__________________________________________________________() { }

public void LongMethodNameKeepsEmptyBraces__________________________________________________________()
{
// comment
}

public void LongMethodNameForceLineBreak(
string oneoneoneoneoneoneoneoneone,
Expand Down
13 changes: 13 additions & 0 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public static Doc PrintWithConditionalSpace(BlockSyntax node, string groupId)

private static Doc Print(BlockSyntax node, string? groupId)
{
if (
node.Statements.Count == 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mabybe I'm getting carried away with pattern matching, but would something like this be valid?

node is { Statements: { Count: 0 }, Parent: MethodDeclarationSyntax {} }

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work but it seems harder to understand, but maybe that's because of how CSharpier formats it.

            if (
                node is { Statements: { Count: 0 }, Parent: MethodDeclarationSyntax }
                && !Token.HasComments(node.CloseBraceToken)
            ) {

If it was formatted this way it could be easier to understand

           if (
                node is
                {
                    Statements: { Count: 0 }, 
                    Parent: MethodDeclarationSyntax
                }
                && !Token.HasComments(node.CloseBraceToken)
            ) {

It could also be that I'm still somewhat new to reading this type of pattern matching.

&& node.Parent is MethodDeclarationSyntax
&& !Token.HasComments(node.CloseBraceToken)
) {
return Doc.Concat(
" ",
Token.Print(node.OpenBraceToken),
" ",
Token.Print(node.CloseBraceToken)
);
}

Doc statementSeparator = node.Parent is AccessorDeclarationSyntax
&& node.Statements.Count <= 1 ? Doc.Line : Doc.HardLine;

Expand Down
14 changes: 14 additions & 0 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,19 @@ private static Doc PrintTrailingTrivia(SyntaxTriviaList trailingTrivia)

return docs.Count > 0 ? Doc.Concat(docs) : Doc.Null;
}

public static bool HasComments(SyntaxToken syntaxToken)
{
return syntaxToken.LeadingTrivia.Any(
o =>
o.Kind() != SyntaxKind.WhitespaceTrivia
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point, but can you use pattern watching with enums here?

o.Kind() is not WhitespaceTrivia or EndOfLineTrivia

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for some reason I have it in my head that it doesn't work with Kind()

&& o.Kind() != SyntaxKind.EndOfLineTrivia
)
|| syntaxToken.TrailingTrivia.Any(
o =>
o.Kind() != SyntaxKind.WhitespaceTrivia
&& o.Kind() != SyntaxKind.EndOfLineTrivia
);
}
}
}