Skip to content

Commit

Permalink
Reduce indentations for nested loops (#920)
Browse files Browse the repository at this point in the history
* Reduce indentations for nested loops

closes #867

* Keep indention for nested loops of mixed types

---------

Co-authored-by: Lasath Fernando <devel@lasath.org>
  • Loading branch information
Rudomitori and shocklateboy92 committed Jul 6, 2023
1 parent c80bab8 commit 65fbc9d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
82 changes: 82 additions & 0 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/cs/NestedLoops.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
public class ClassName
{
public void MethodName()
{
foreach (var subsequence in sequence)
foreach (var item in subsequence)
item.DoSomething();

foreach (var item in subsequence)
while (item.IsSomething())
item.DoSomething();

foreach (var item in sequence)
do item.DoSomething();
while (false);

foreach (var item in sequence)
for (; ; )
item.DoSomething();

foreach (var subsequence in sequence)
{
foreach (var item in subsequence)
{
item.DoSomething();
item.DoSomething();
}
}

await foreach (var subsequence in sequence)
await foreach (var item in subsequence)
item.DoSomething();

foreach (var subsequence in sequence)
await foreach (var item in subsequence)
{
item.DoSomething();
item.DoSomething();
}

while (true)
foreach (var item in sequence)
item.DoSomething();

while (true)
while (true)
do Console.WriteLine();
while (false);

while (true)
for (; ; )
Console.WriteLine();

while (true)
{
foreach (var item in sequence)
item.DoSomething();
}

for (; ; )
foreach (var item in sequence)
item.DoSomething();

for (; ; )
while (true)
Console.WriteLine();

for (; ; )
do Console.WriteLine();
while (false);

for (; ; )
for (; ; )
item.DoSomething();

for (; ; )
{
for (; ; )
Console.WriteLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ ForEachVariableStatementSyntax forEachVariable
),
Token.Print(node.CloseParenToken, context)
),
OptionalBraces.Print(node.Statement, context)
node.Statement switch
{
CommonForEachStatementSyntax
=> Doc.Group(Doc.HardLine, Node.Print(node.Statement, context)),
_ => OptionalBraces.Print(node.Statement, context)
}
);

return docs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public static Doc Print(ForStatementSyntax node, FormattingContext context)
),
Token.Print(node.CloseParenToken, context)
),
OptionalBraces.Print(node.Statement, context)
node.Statement switch
{
ForStatementSyntax => Doc.Group(Doc.HardLine, Node.Print(node.Statement, context)),
_ => OptionalBraces.Print(node.Statement, context)
}
};

return Doc.Concat(docs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public static Doc Print(WhileStatementSyntax node, FormattingContext context)
),
Token.Print(node.CloseParenToken, context)
),
OptionalBraces.Print(node.Statement, context)
node.Statement switch
{
WhileStatementSyntax
=> Doc.Group(Doc.HardLine, Node.Print(node.Statement, context)),
_ => OptionalBraces.Print(node.Statement, context)
}
);

return result;
Expand Down

0 comments on commit 65fbc9d

Please sign in to comment.