diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test index a3fbc915f..135d4b927 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test @@ -21,8 +21,8 @@ class ClassName (DoorState.Closed, Action.Lock, true) => DoorState.Locked, (DoorState.Locked, Action.Unlock, true) => DoorState.Closed, (var state, _, _) => state, - (_, _, _) - => DoorState.LongStateShouldNotGetExtraLine________________________________________, + (_, _, _) => + DoorState.LongStateShouldNotGetExtraLine________________________________________, }; return someValue switch @@ -31,45 +31,42 @@ class ClassName { IsParameter: true } => 1, // this comment shouldn't affect the next line { IsParameter: false } => 0, - SomeLongObject someLongObject - => CallSomeMethodWith____________________________(someLongObject), - YetAnotherObject - => CallSomeMethod( - someValue, - andOtherParameters, - thatMakeThisLongEnoughToBreak___________________ - ), + SomeLongObject someLongObject => CallSomeMethodWith____________________________( + someLongObject + ), + YetAnotherObject => CallSomeMethod( + someValue, + andOtherParameters, + thatMakeThisLongEnoughToBreak___________________ + ), VeryLongObject_______________________________________________________________ - when count > 0 - => CallSomeMethod(someValue), - OneMore - => "someStrings" - + "moreStrings" - + "andMoreStrings_________________________________________", - SomeOtherObject - or AnotherObject - or OrEvenSomeOtherObject_________________ - => CallSomeMethod(someValue), - SomeOtherObject { SomeProperty: SomeOtherProject } - or AnotherObject - => CallSomeMethod(someValue), - AnotherObject - or SomeOtherObject { SomeProperty: SomeOtherProject } - => CallSomeMethod(someValue), + when count > 0 => CallSomeMethod(someValue), + OneMore => "someStrings" + + "moreStrings" + + "andMoreStrings_________________________________________", + SomeOtherObject or AnotherObject or OrEvenSomeOtherObject_________________ => + CallSomeMethod(someValue), + SomeOtherObject { SomeProperty: SomeOtherProject } or AnotherObject => CallSomeMethod( + someValue + ), + AnotherObject or SomeOtherObject { SomeProperty: SomeOtherProject } => CallSomeMethod( + someValue + ), SomeOtherObject { Property: true } => CallSomeMethod(someValue), - SomeOtherObject { SomeProperty: YetAnotherObject { Property: true } } - => CallSomeMethod(someValue), + SomeOtherObject { SomeProperty: YetAnotherObject { Property: true } } => CallSomeMethod( + someValue + ), { } otherSyntax => CallSomeMethod(otherSyntax), - { SomeProperty: true } - => "Just because this is long, don't break the pattern ", + { SomeProperty: true } => + "Just because this is long, don't break the pattern ", _ => CallSomeMethod(someValue), }; return someValue switch { { IsParameter: true } => noExtraLineAfterThis, - { IsParameter: someLongValue____________________________________________ } - => someOtherValue, + { IsParameter: someLongValue____________________________________________ } => + someOtherValue, }; return (original, updated) switch @@ -85,8 +82,7 @@ class ClassName ContainingType: { IsTupleType: true }, TupleElementIndex: var ui } updatedField - ) - => originalField.Type.Equals(updatedField.Type), + ) => originalField.Type.Equals(updatedField.Type), }; return someValue switch @@ -103,12 +99,25 @@ class ClassName Some.Two => 2, }; + return someValue switch + { + Some.One + // comment + => 1, + }; + return someValue switch { (true, true) => someOtherValue, - (false, false) - or someCondition - => someValue_____________________________________________________, + (false, false) or someCondition => + someValue_____________________________________________________, + }; + + return someValue switch + { + _ => throw new ArgumentOutOfRangeException( + "someLongString________________________________________________________________" + ), }; } } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions_Tabs.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions_Tabs.test index ab1922a2f..6951bd3a3 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions_Tabs.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions_Tabs.test @@ -21,8 +21,8 @@ class ClassName (DoorState.Closed, Action.Lock, true) => DoorState.Locked, (DoorState.Locked, Action.Unlock, true) => DoorState.Closed, (var state, _, _) => state, - (_, _, _) - => DoorState.LongStateShouldNotGetExtraLine________________________________________, + (_, _, _) => + DoorState.LongStateShouldNotGetExtraLine________________________________________, }; return someValue switch @@ -31,45 +31,42 @@ class ClassName { IsParameter: true } => 1, // this comment shouldn't affect the next line { IsParameter: false } => 0, - SomeLongObject someLongObject - => CallSomeMethodWith____________________________(someLongObject), - YetAnotherObject - => CallSomeMethod( - someValue, - andOtherParameters, - thatMakeThisLongEnoughToBreak___________________ - ), + SomeLongObject someLongObject => CallSomeMethodWith____________________________( + someLongObject + ), + YetAnotherObject => CallSomeMethod( + someValue, + andOtherParameters, + thatMakeThisLongEnoughToBreak___________________ + ), VeryLongObject_______________________________________________________________ - when count > 0 - => CallSomeMethod(someValue), - OneMore - => "someStrings" - + "moreStrings" - + "andMoreStrings_________________________________________", - SomeOtherObject - or AnotherObject - or OrEvenSomeOtherObject_________________ - => CallSomeMethod(someValue), - SomeOtherObject { SomeProperty: SomeOtherProject } - or AnotherObject - => CallSomeMethod(someValue), - AnotherObject - or SomeOtherObject { SomeProperty: SomeOtherProject } - => CallSomeMethod(someValue), + when count > 0 => CallSomeMethod(someValue), + OneMore => "someStrings" + + "moreStrings" + + "andMoreStrings_________________________________________", + SomeOtherObject or AnotherObject or OrEvenSomeOtherObject_________________ => + CallSomeMethod(someValue), + SomeOtherObject { SomeProperty: SomeOtherProject } or AnotherObject => CallSomeMethod( + someValue + ), + AnotherObject or SomeOtherObject { SomeProperty: SomeOtherProject } => CallSomeMethod( + someValue + ), SomeOtherObject { Property: true } => CallSomeMethod(someValue), - SomeOtherObject { SomeProperty: YetAnotherObject { Property: true } } - => CallSomeMethod(someValue), + SomeOtherObject { SomeProperty: YetAnotherObject { Property: true } } => CallSomeMethod( + someValue + ), { } otherSyntax => CallSomeMethod(otherSyntax), - { SomeProperty: true } - => "Just because this is long, don't break the pattern ", + { SomeProperty: true } => + "Just because this is long, don't break the pattern ", _ => CallSomeMethod(someValue), }; return someValue switch { { IsParameter: true } => noExtraLineAfterThis, - { IsParameter: someLongValue____________________________________________ } - => someOtherValue, + { IsParameter: someLongValue____________________________________________ } => + someOtherValue, }; return (original, updated) switch @@ -85,8 +82,7 @@ class ClassName ContainingType: { IsTupleType: true }, TupleElementIndex: var ui } updatedField - ) - => originalField.Type.Equals(updatedField.Type), + ) => originalField.Type.Equals(updatedField.Type), }; } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs index 580f84485..6edea519d 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs @@ -9,29 +9,7 @@ public static Doc Print(SwitchExpressionSyntax node, FormattingContext context) Doc.HardLine, SeparatedSyntaxList.PrintWithTrailingComma( node.Arms, - (o, _) => - Doc.Concat( - ExtraNewLines.Print(o), - Token.PrintLeadingTrivia( - o.Pattern.GetLeadingTrivia(), - context.WithSkipNextLeadingTrivia() - ), - Doc.Group( - Node.Print(o.Pattern, context), - o.WhenClause != null ? Node.Print(o.WhenClause, context) : Doc.Null, - Doc.Indent( - Doc.Concat( - Doc.Line, - Token.PrintWithSuffix( - o.EqualsGreaterThanToken, - " ", - context - ), - Node.Print(o.Expression, context) - ) - ) - ) - ), + PrintArm, Doc.HardLine, context, node.CloseBraceToken @@ -52,4 +30,52 @@ public static Doc Print(SwitchExpressionSyntax node, FormattingContext context) Token.Print(node.CloseBraceToken, context) ); } + + private static Doc PrintArm( + SwitchExpressionArmSyntax switchExpressionArm, + FormattingContext context + ) + { + var arrowHasComment = switchExpressionArm.EqualsGreaterThanToken.LeadingTrivia.Any(o => + o.IsComment() + ); + + var groupId2 = Guid.NewGuid().ToString(); + var innerContents = arrowHasComment + ? Doc.Indent( + Doc.Concat( + Doc.Line, + Token.PrintWithSuffix(switchExpressionArm.EqualsGreaterThanToken, " ", context), + Node.Print(switchExpressionArm.Expression, context) + ) + ) + : Doc.Concat( + " ", + Token.Print(switchExpressionArm.EqualsGreaterThanToken, context), + Doc.GroupWithId(groupId2, Doc.Indent(Doc.Line)), + Doc.IndentIfBreak(Node.Print(switchExpressionArm.Expression, context), groupId2) + ); + + var groupId1 = Guid.NewGuid().ToString(); + + return Doc.Concat( + ExtraNewLines.Print(switchExpressionArm), + Token.PrintLeadingTrivia( + switchExpressionArm.Pattern.GetLeadingTrivia(), + context.WithSkipNextLeadingTrivia() + ), + Doc.Group( + Doc.GroupWithId( + groupId1, + Doc.Concat( + Node.Print(switchExpressionArm.Pattern, context), + switchExpressionArm.WhenClause != null + ? Node.Print(switchExpressionArm.WhenClause, context) + : Doc.Null + ) + ), + innerContents + ) + ); + } }