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

dealing with loops without braces #213

Merged
merged 2 commits into from
May 19, 2021
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
Expand Up @@ -52,5 +52,22 @@ public class ClassName
{
return;
}

foreach (var x in y) CallSomething(x);

foreach (var someLongerNameThatMakesThis in BreakAndLongName)
CallSomething(x);

foreach (
var someLongerNameThatMakesThis in BreakInAnotherWay11111111111111111111
)
CallSomething(x);

foreach (var x in y)
switch (x)
{
case 1:
return;
}
}
}
13 changes: 11 additions & 2 deletions Src/CSharpier.Tests/TestFiles/ForStatement/ForStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ class ClassName
break;
}

for (var x = 0; x < 100; x++)
break;
for (var x = 0; x < 100; x++) break;

for (var someLongerValue = 0; someLongerValue < 1; someLongerValue++)
Call(SomeLongerValue);

for (
var someLongerValue11111111111 = 0;
someLongerValue11111111111 < 1;
someLongerValue11111111111++
)
Call(someLongerValue11111111111);

for (int i = 0, j = 0; i < length; i++, j++)
{
Expand Down
27 changes: 20 additions & 7 deletions Src/CSharpier.Tests/TestFiles/IfStatement/IfStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ public class ClassName
return;
}

if (true)
DoSoemthing();
else if (false)
DoSomething();
if (true) DoSomething();
else if (false) DoSomething();
else
DoSomething;

if (someLongCondition != someOtherCondition)
DoSomeLongMethodCall____________();
else if (someLongCondition == someOtherCondition)
DoSomeLongMethodCall____________();
else
DoSomeLongMethodCall________________________________________();

if (
longStatementName
&& longerStatementName
&& evenLongerStatementName
&& superLongStatementName
)
return;

if (!true) { }

if (true != false) { }
Expand All @@ -35,19 +48,19 @@ public class ClassName

if (true && (true || false)) { }

if (exactly80 || moreTextToMakeItWork1111111111111111111111111111111111)
if (exactly80 || moreTextToMakeItWork__________________________________)
{
return;
}

// leading comment doesn't break differently
if (exactly80 || moreTextToMakeItWork1111111111111111111111111111111111)
if (exactly80 || moreTextToMakeItWork__________________________________)
{
return;
}

if (
justOver80 || moreTextToMakeItWork1111111111111111111111111111111112
justOver80 || moreTextToMakeItWork__________________________________
) {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions Src/CSharpier.Tests/TestFiles/WhileStatement/WhileStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@ class ClassName
) {
return;
}

while (true) return;

while (someLongerStatement && someOtherLongerStatement)
return someLongerValue;

while (
someEvenLongerStatement && someOtherEvenLongerStatement1111111111
)
return someLongerValue;
}
}
55 changes: 29 additions & 26 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForEachStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,37 @@ public static Doc Print(ForEachStatementSyntax node)
ExtraNewLines.Print(node),
leadingTrivia,
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.AwaitKeyword),
node.AwaitKeyword.Kind() != SyntaxKind.None ? " " : Doc.Null,
node.AwaitKeyword.Kind() == SyntaxKind.None
? Token.PrintWithoutLeadingTrivia(node.ForEachKeyword)
: Token.Print(node.ForEachKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(
Doc.SoftLine,
Node.Print(node.Type),
" ",
Token.Print(node.Identifier),
" ",
Token.Print(node.InKeyword),
" ",
Node.Print(node.Expression)
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.AwaitKeyword),
node.AwaitKeyword.Kind()
is not SyntaxKind.None ? " " : Doc.Null,
node.AwaitKeyword.Kind() is SyntaxKind.None
? Token.PrintWithoutLeadingTrivia(node.ForEachKeyword)
: Token.Print(node.ForEachKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(
Doc.SoftLine,
Node.Print(node.Type),
" ",
Token.Print(node.Identifier),
" ",
Token.Print(node.InKeyword),
" ",
Node.Print(node.Expression)
),
Doc.SoftLine
),
Doc.SoftLine
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
node.Statement
is BlockSyntax blockSyntax
? Block.PrintWithConditionalSpace(blockSyntax, groupId)
: Node.Print(node.Statement)
node.Statement
is BlockSyntax blockSyntax
? Block.PrintWithConditionalSpace(blockSyntax, groupId)
: Doc.Indent(Doc.Line, Node.Print(node.Statement))
)
);

return docs;
Expand Down
28 changes: 12 additions & 16 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,21 @@ public static Doc Print(ForStatementSyntax node)
ExtraNewLines.Print(node),
Token.PrintLeadingTrivia(node.ForKeyword),
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.ForKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(groupId, Doc.Indent(innerGroup), Doc.SoftLine),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.ForKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(groupId, Doc.Indent(innerGroup), Doc.SoftLine),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
node.Statement
is BlockSyntax blockSyntax
? Block.PrintWithConditionalSpace(blockSyntax, groupId)
: Doc.Indent(Doc.Line, Node.Print(node.Statement))
)
};

if (node.Statement is BlockSyntax blockSyntax)
{
docs.Add(Block.PrintWithConditionalSpace(blockSyntax, groupId));
}
else
{
// TODO 1 force braces? we do in if and else
docs.Add(Doc.Indent(Doc.HardLine, Node.Print(node.Statement)));
}

return Doc.Concat(docs);
}
}
Expand Down
35 changes: 16 additions & 19 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/IfStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,26 @@ public static Doc Print(IfStatementSyntax node)
var groupId = Guid.NewGuid().ToString();

docs.Add(
Token.PrintLeadingTrivia(node.IfKeyword),
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.IfKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(Doc.SoftLine, Node.Print(node.Condition)),
Doc.SoftLine
Token.PrintLeadingTrivia(node.IfKeyword),
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.IfKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(Doc.SoftLine, Node.Print(node.Condition)),
Doc.SoftLine
),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
node.Statement
is BlockSyntax blockSyntax
? Block.PrintWithConditionalSpace(blockSyntax, groupId)
: Doc.Indent(Doc.Line, Node.Print(node.Statement))
)
);
if (node.Statement is BlockSyntax blockSyntax)
{
docs.Add(Block.PrintWithConditionalSpace(blockSyntax, groupId));
}
else
{
// TODO 1 force braces here? make an option?
docs.Add(Doc.Indent(Doc.Concat(Doc.HardLine, Node.Print(node.Statement))));
}

if (node.Else != null)
{
Expand Down
30 changes: 16 additions & 14 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/WhileStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ public static Doc Print(WhileStatementSyntax node)
ExtraNewLines.Print(node),
Token.PrintLeadingTrivia(node.WhileKeyword),
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.WhileKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(Doc.SoftLine, Node.Print(node.Condition)),
Doc.SoftLine
Doc.Group(
Token.PrintWithoutLeadingTrivia(node.WhileKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(Doc.SoftLine, Node.Print(node.Condition)),
Doc.SoftLine
),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
node.Statement
is BlockSyntax blockSyntax
? Block.PrintWithConditionalSpace(blockSyntax, groupId)
: Node.Print(node.Statement)
node.Statement
is BlockSyntax blockSyntax
? Block.PrintWithConditionalSpace(blockSyntax, groupId)
: Doc.Indent(Doc.Line, Node.Print(node.Statement))
)
);

return result;
Expand Down