Skip to content

Commit

Permalink
Improving ForStatement edge cases (#343)
Browse files Browse the repository at this point in the history
closes #112
  • Loading branch information
belav authored Jun 28, 2021
1 parent 3a37426 commit 00f6eb8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
35 changes: 21 additions & 14 deletions Src/CSharpier.Tests/TestFiles/ForStatement/ForStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,40 @@ class ClassName
Call(SomeLongerValue);

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

for (int i = 0, j = 0; i < length; i++, j++)
{
break;
}

for (
var laksdflasdjfkaskdlfklasdfkljasdf = 0;
laksdflasdjfkaskdlfklasdfkljasdf_____________________
< laksdflasdjfkaskdlfklasdfkljasdf + 1;
laksdflasdjfkaskdlfklasdfkljasdf++
var shorterName = 0;
someLongName____________
< someOtherLongName__________________________________________________;
someLongishName_________________________++
) {
break;
}

for (
var laksdflasdjfkaskdlfklasdfkljasdf = 0,
lkasdfkjlasjldkfljaskdflkasdfljkasdfljk = 1;
laksdflasdjfkaskdlfklasdfkljasdf______________________
< laksdflasdjfkaskdlfklasdfkljasdf + 1;
laksdflasdjfkaskdlfklasdfkljasdf++,
lkasdfkjlasjldkfljaskdflkasdfljkasdfljk
var someShortName = 0, anotherShortName = 1;
someRandomCondition;
someShortName++, anotherShortName++
) {
break;
}

for (
var someLongerName_________________________ = 0,
anotherLongerName________________________________ = 1;
someRandomCondition;
someLongerName_________________________++,
anotherLongerName________________________________++
) {
break;
}
Expand Down
58 changes: 28 additions & 30 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,6 @@ public static Doc Print(ForStatementSyntax node)
{
var groupId = Guid.NewGuid().ToString();

var innerGroup = new List<Doc> { Doc.SoftLine };
if (node.Declaration != null)
{
innerGroup.Add(VariableDeclaration.Print(node.Declaration));
}
innerGroup.Add(SeparatedSyntaxList.Print(node.Initializers, Node.Print, " "));
innerGroup.Add(Token.Print(node.FirstSemicolonToken));
if (node.Condition != null)
{
innerGroup.Add(Doc.Line, Node.Print(node.Condition));
}
else
{
innerGroup.Add(Doc.SoftLine);
}

innerGroup.Add(Token.Print(node.SecondSemicolonToken));
if (node.Incrementors.Any())
{
innerGroup.Add(Doc.Line);
}
else
{
innerGroup.Add(Doc.SoftLine);
}
innerGroup.Add(
Doc.Indent(SeparatedSyntaxList.Print(node.Incrementors, Node.Print, Doc.Line))
);

var docs = new List<Doc>
{
ExtraNewLines.Print(node),
Expand All @@ -52,7 +23,34 @@ public static Doc Print(ForStatementSyntax node)
Token.PrintWithoutLeadingTrivia(node.ForKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(groupId, Doc.Indent(innerGroup), Doc.SoftLine),
Doc.GroupWithId(
groupId,
Doc.Indent(
Doc.SoftLine,
Doc.Group(
node.Declaration != null
? VariableDeclaration.Print(node.Declaration)
: Doc.Null,
SeparatedSyntaxList.Print(node.Initializers, Node.Print, " "),
Token.Print(node.FirstSemicolonToken)
),
node.Condition != null
? Doc.Concat(Doc.Line, Node.Print(node.Condition))
: Doc.SoftLine,
Token.Print(node.SecondSemicolonToken),
node.Incrementors.Any() ? Doc.Line : Doc.SoftLine,
Doc.Group(
Doc.Indent(
SeparatedSyntaxList.Print(
node.Incrementors,
Node.Print,
Doc.Line
)
)
)
),
Doc.SoftLine
),
Token.Print(node.CloseParenToken),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
Expand Down

0 comments on commit 00f6eb8

Please sign in to comment.