From 5073fd689cfbc906f707ef8425984cbb6a90f6c3 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Fri, 28 Jun 2024 11:38:12 -0500 Subject: [PATCH] Fixing an edge case with comments --- .../TestFiles/cs/SwitchExpressions.test | 7 + .../SyntaxNodePrinters/SwitchExpression.cs | 162 ++++++------------ 2 files changed, 56 insertions(+), 113 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test index ca8f50106..135d4b927 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SwitchExpressions.test @@ -99,6 +99,13 @@ class ClassName Some.Two => 2, }; + return someValue switch + { + Some.One + // comment + => 1, + }; + return someValue switch { (true, true) => someOtherValue, diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs index 37a13ae2c..171cb3b3b 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs @@ -4,124 +4,12 @@ internal static class SwitchExpression { public static Doc Print(SwitchExpressionSyntax node, FormattingContext context) { - // TODO also look at keeping stuff with =>, see throw new - /* -public class ClassName { - public bool Foo(object entry) - { - return entry switch - { - string s => s.Length switch - { - 1 => true, - 2 => false, - _ => throw new ArgumentOutOfRangeException( - "this specific string length is not supported" - ), - }, - int i => i, - _ => throw new ArgumentOutOfRangeException( - $"entry type {entry.GetType()} not supported" - ), - }; - } - - also this one with comments - - private static bool TryAddEqualizedFieldsForStatements( - IEnumerable statementsToCheck, - ISymbol otherC, - INamedTypeSymbol type, - ArrayBuilder builder - ) => - statementsToCheck.FirstOrDefault() switch - { - IReturnOperation - { - ReturnedValue: ILiteralOperation - { - ConstantValue.HasValue: true, - ConstantValue.Value: true, - } - } - // we are done with the comparison, the final statment does no checks - => true, - IReturnOperation { ReturnedValue: IOperation value } - => TryAddEqualizedFieldsForCondition( - value, - successRequirement: true, - currentObject: type, - otherObject: otherC, - builder: builder - ), - IConditionalOperation - { - Condition: IOperation condition, - WhenTrue: IOperation whenTrue, - WhenFalse: var whenFalse, - } - // 1. Check structure of if statment, get success requirement - // and any potential statments in the non failure block - // 2. Check condition for compared members - // 3. Check remaining members in non failure block - => TryGetSuccessCondition( - whenTrue, - whenFalse, - statementsToCheck.Skip(1), - out var successRequirement, - out var remainingStatements - ) - && TryAddEqualizedFieldsForCondition( - condition, - successRequirement, - type, - otherC, - builder - ) - && TryAddEqualizedFieldsForStatements( - remainingStatements, - otherC, - type, - builder - ), - _ => false - }; -} - */ var sections = Doc.Group( Doc.Indent( Doc.HardLine, SeparatedSyntaxList.Print( node.Arms, - (o, _) => - { - var groupId1 = Guid.NewGuid().ToString(); - var groupId2 = Guid.NewGuid().ToString(); - return Doc.Concat( - ExtraNewLines.Print(o), - Token.PrintLeadingTrivia( - o.Pattern.GetLeadingTrivia(), - context.WithSkipNextLeadingTrivia() - ), - Doc.Group( - Doc.GroupWithId( - groupId1, - Doc.Concat( - Node.Print(o.Pattern, context), - o.WhenClause != null - ? Node.Print(o.WhenClause, context) - : Doc.Null - ) - ), - Doc.Concat( - " ", - Token.Print(o.EqualsGreaterThanToken, context), - Doc.GroupWithId(groupId2, Doc.Indent(Doc.Line)), - Doc.IndentIfBreak(Node.Print(o.Expression, context), groupId2) - ) - ) - ); - }, + PrintArm, Doc.HardLine, context, trailingSeparator: TrailingComma.Print(node.CloseBraceToken, context) @@ -142,4 +30,52 @@ out var remainingStatements 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 + ) + ); + } }