Skip to content

Commit

Permalink
Fixing an edge case with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Jul 27, 2024
1 parent 74cb544 commit 5073fd6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class ClassName
Some.Two => 2,
};

return someValue switch
{
Some.One
// comment
=> 1,
};

return someValue switch
{
(true, true) => someOtherValue,
Expand Down
162 changes: 49 additions & 113 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOperation> statementsToCheck,
ISymbol otherC,
INamedTypeSymbol type,
ArrayBuilder<IFieldSymbol> 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)
Expand All @@ -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
)
);
}
}

0 comments on commit 5073fd6

Please sign in to comment.