Skip to content

Commit

Permalink
Prefer breaking before when clause, instead of inside (#384)
Browse files Browse the repository at this point in the history
* Prefer breaking before `when` clause, instead of inside

Closes #382

* Use Group instead of GroupWithId
  • Loading branch information
pmccloghrylaing authored Jul 29, 2021
1 parent dcb89b0 commit 14d69d6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class ClassName
andOtherParameters,
thatMakeThisLongEnoughToBreak___________________
),
VeryLongObject_______________________________________________________________
when count > 0
=> CallSomeMethod(someValue),
OneMore
=> "someStrings"
+ "moreStrings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class ClassName
{
case "A" when b > 50:
case "B" when B < 50:
case "C______________________________________________________________________"
when C == 50:
default:
{
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static Doc Print(CasePatternSwitchLabelSyntax node)
docs.Add(Token.PrintWithSuffix(node.Keyword, " "), Node.Print(node.Pattern));
if (node.WhenClause != null)
{
docs.Add(" ", Node.Print(node.WhenClause));
docs.Add(Node.Print(node.WhenClause));
}
docs.Add(Token.Print(node.ColonToken));
return Doc.Concat(docs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;
using CSharpier.DocTypes;
using CSharpier.SyntaxPrinter;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters
Expand All @@ -24,7 +22,7 @@ public static Doc Print(SwitchExpressionSyntax node)
Doc.Group(
Node.Print(o.Pattern),
o.WhenClause != null
? Doc.Concat(" ", Node.Print(o.WhenClause))
? Node.Print(o.WhenClause)
: Doc.Null,
// use align 2 here to make sure that the => never lines up with statements above it
// it makes this more readable for big ugly switch expressions
Expand Down
9 changes: 6 additions & 3 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/WhenClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ public static class WhenClause
{
public static Doc Print(WhenClauseSyntax node)
{
return Doc.Concat(
Token.PrintWithSuffix(node.WhenKeyword, " "),
Node.Print(node.Condition)
return Doc.Group(
Doc.Indent(
Doc.Line,
Token.PrintWithSuffix(node.WhenKeyword, " "),
Node.Print(node.Condition)
)
);
}
}
Expand Down

0 comments on commit 14d69d6

Please sign in to comment.