Skip to content

Commit

Permalink
Better formatting for switch expressions
Browse files Browse the repository at this point in the history
closes #237
  • Loading branch information
belav committed Jul 11, 2021
1 parent 38d70b1 commit 88ff592
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ClassName
{
int MethodName()
void MethodName()
{
return 1 switch
{
Expand All @@ -18,5 +18,27 @@ class ClassName
(DoorState.Locked, Action.Unlock, true) => DoorState.Closed,
(var state, _, _) => state
};

return someValue switch
{
SomeSimpleObject => DoSomething(),
SomeLongObject someLongObject
=> CallSomeMethodWith____________________________(someLongObject),
YetAnotherObject
=> CallSomeMethod(
someValue,
andOtherParameters,
thatMakeThisLongEnoughToBreak___________________
),
OneMore
=> "someStrings"
+ "moreStrings"
+ "andMoreStrings_________________________________________",
SomeOtherObject
or AnotherObject
or OrEvenSomeOtherObject_________________
=> CallSomeMethod(someValue),
_ => CallSomeMethod(someValue)
};
}
}
19 changes: 13 additions & 6 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ public static Doc Print(SwitchExpressionSyntax node)
SeparatedSyntaxList.Print(
node.Arms,
o =>
Doc.Concat(
Node.Print(o.Pattern),
" ",
Doc.Group(
Doc.Indent(Node.Print(o.Pattern)),
o.WhenClause != null
? Doc.Concat(Node.Print(o.WhenClause), " ")
? Doc.Concat(" ", Node.Print(o.WhenClause))
: Doc.Null,
Token.PrintWithSuffix(o.EqualsGreaterThanToken, " "),
Node.Print(o.Expression)
// 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
Doc.Align(
2,
Doc.Concat(
Doc.Line,
Token.PrintWithSuffix(o.EqualsGreaterThanToken, " "),
Doc.Align(2, Node.Print(o.Expression))
)
)
),
Doc.HardLine
)
Expand Down

0 comments on commit 88ff592

Please sign in to comment.