From 92d43abbfc176f7866d530de8cab1f8dba205280 Mon Sep 17 00:00:00 2001 From: belav Date: Mon, 17 May 2021 20:18:14 -0500 Subject: [PATCH] dealing with loops without braces closes #202 --- .../ForEachStatement/ForEachStatements.cst | 17 ++++++ .../TestFiles/ForStatement/ForStatements.cst | 13 ++++- .../TestFiles/IfStatement/IfStatements.cst | 21 ++++++-- .../WhileStatement/WhileStatements.cst | 10 ++++ .../SyntaxNodePrinters/ForEachStatement.cs | 54 ++++++++++--------- .../SyntaxNodePrinters/ForStatement.cs | 28 +++++----- .../SyntaxNodePrinters/IfStatement.cs | 35 ++++++------ .../SyntaxNodePrinters/WhileStatement.cs | 30 ++++++----- 8 files changed, 127 insertions(+), 81 deletions(-) diff --git a/Src/CSharpier.Tests/TestFiles/ForEachStatement/ForEachStatements.cst b/Src/CSharpier.Tests/TestFiles/ForEachStatement/ForEachStatements.cst index f4790335c..59365c3ff 100644 --- a/Src/CSharpier.Tests/TestFiles/ForEachStatement/ForEachStatements.cst +++ b/Src/CSharpier.Tests/TestFiles/ForEachStatement/ForEachStatements.cst @@ -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; + } } } diff --git a/Src/CSharpier.Tests/TestFiles/ForStatement/ForStatements.cst b/Src/CSharpier.Tests/TestFiles/ForStatement/ForStatements.cst index 06d7cdad6..dd644956a 100644 --- a/Src/CSharpier.Tests/TestFiles/ForStatement/ForStatements.cst +++ b/Src/CSharpier.Tests/TestFiles/ForStatement/ForStatements.cst @@ -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++) { diff --git a/Src/CSharpier.Tests/TestFiles/IfStatement/IfStatements.cst b/Src/CSharpier.Tests/TestFiles/IfStatement/IfStatements.cst index cd102f72c..64b16fcf1 100644 --- a/Src/CSharpier.Tests/TestFiles/IfStatement/IfStatements.cst +++ b/Src/CSharpier.Tests/TestFiles/IfStatement/IfStatements.cst @@ -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) { } diff --git a/Src/CSharpier.Tests/TestFiles/WhileStatement/WhileStatements.cst b/Src/CSharpier.Tests/TestFiles/WhileStatement/WhileStatements.cst index a301db5d7..0eebd43ea 100644 --- a/Src/CSharpier.Tests/TestFiles/WhileStatement/WhileStatements.cst +++ b/Src/CSharpier.Tests/TestFiles/WhileStatement/WhileStatements.cst @@ -36,5 +36,15 @@ class ClassName ) { return; } + + while (true) return; + + while (someLongerStatement && someOtherLongerStatement) + return someLongerValue; + + while ( + someEvenLongerStatement && someOtherEvenLongerStatement1111111111 + ) + return someLongerValue; } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForEachStatement.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForEachStatement.cs index 61feda9f9..bc9a262fc 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForEachStatement.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForEachStatement.cs @@ -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; diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForStatement.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForStatement.cs index a5967a39f..7cdf67afa 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForStatement.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ForStatement.cs @@ -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); } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/IfStatement.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/IfStatement.cs index df6341b1c..70872ae25 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/IfStatement.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/IfStatement.cs @@ -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) { diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/WhileStatement.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/WhileStatement.cs index 2e3641a55..582e78ff1 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/WhileStatement.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/WhileStatement.cs @@ -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;