Skip to content

Commit

Permalink
dealing with loops without braces
Browse files Browse the repository at this point in the history
closes #202
  • Loading branch information
belav committed May 18, 2021
1 parent 3d19de6 commit 92d43ab
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 81 deletions.
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
21 changes: 17 additions & 4 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)
DoSomeLongMethodCall111111111111();
else if (someLongCondition == someOtherCondition)
DoSomeLongMethodCall111111111111();
else
DoSomeLongMethodCall1111111111111111111111111111111111111111();

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

if (!true) { }

if (true != false) { }
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;
}
}
54 changes: 28 additions & 26 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForEachStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,36 @@ 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() != 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.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

0 comments on commit 92d43ab

Please sign in to comment.