Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwitchExpression formatting. #237

Closed
belav opened this issue May 29, 2021 · 3 comments · Fixed by #350
Closed

SwitchExpression formatting. #237

belav opened this issue May 29, 2021 · 3 comments · Fixed by #350
Assignees
Labels
area:formatting type:bug Something isn't working
Milestone

Comments

@belav
Copy link
Owner

belav commented May 29, 2021

// currently is
            return document switch
            {
                NullDoc => indent + "Doc.Null",
                StringDoc { IsDirective: true } directive => $"{indent}Doc.Directive({directive.Value.Replace("\"", "\\\"")})",
                StringDoc stringDoc => indent + "\"" + stringDoc.Value.Replace("\"", "\\\"") + "\"",
                HardLine hardLine => indent
                + "Doc.HardLine"
                + (hardLine.Squash ? "IfNoPreviousLine" : string.Empty)
                + (hardLine.SkipBreakIfFirstInGroup ? "SkipBreakIfFirstInGroup" : string.Empty),
                LiteralLine => indent + "Doc.LiteralLine",
                Group group => @$"{indent}Doc.Group{(@group.GroupId != null ? "WithId" : string.Empty)}(
{(@group.GroupId != null ? $"{nextIndent}\"{@group.GroupId}\",{newLine}" : string.Empty)}{PrintIndentedDocTree(@group.Contents)}{newLine}{indent})",
                Concat concat => Result(concat),
                _ => throw new Exception("Can't handle " + document)   
            };
// could be
            return document switch
            {
                NullDoc
                    => indent + "Doc.Null",
                StringDoc { IsDirective: true } directive 
                    => $"{indent}Doc.Directive({directive.Value.Replace("\"", "\\\"")})",
                StringDoc stringDoc
                    => indent + "\"" + stringDoc.Value.Replace("\"", "\\\"") + "\"",
                HardLine hardLine
                    => indent
                       + "Doc.HardLine"
                       + (hardLine.Squash ? "IfNoPreviousLine" : string.Empty)
                       + (hardLine.SkipBreakIfFirstInGroup ? "SkipBreakIfFirstInGroup" : string.Empty),
                LiteralLine
                    => indent + "Doc.LiteralLine",
                Group group
                    => @$"{indent}Doc.Group{(@group.GroupId != null ? "WithId" : string.Empty)}(
{(@group.GroupId != null ? $"{nextIndent}\"{@group.GroupId}\",{newLine}" : string.Empty)}{PrintIndentedDocTree(@group.Contents)}{newLine}{indent})",
                Concat concat
                    => Result(concat),
                _   
                    => throw new Exception("Can't handle " + document)   
            };
// or maybe
            return document switch
            {
                NullDoc                                   => indent + "Doc.Null",
                StringDoc { IsDirective: true } directive => $"{indent}Doc.Directive({directive.Value.Replace("\"", "\\\"")})",
                StringDoc stringDoc                       => indent + "\"" + stringDoc.Value.Replace("\"", "\\\"") + "\"",
                HardLine hardLine                         => indent
                                                             + "Doc.HardLine"
                                                             + (hardLine.Squash ? "IfNoPreviousLine" : string.Empty)
                                                             + (hardLine.SkipBreakIfFirstInGroup ? "SkipBreakIfFirstInGroup" : string.Empty),
                LiteralLine                               => indent + "Doc.LiteralLine",
                Group group                               => @$"{indent}Doc.Group{(@group.GroupId != null ? "WithId" : string.Empty)}(
{(@group.GroupId != null ? $"{nextIndent}\"{@group.GroupId}\",{newLine}" : string.Empty)}{PrintIndentedDocTree(@group.Contents)}{newLine}{indent})",
                Concat concat                             => Result(concat),
                _                                         => throw new Exception("Can't handle " + document)   
            };
@shocklateboy92
Copy link
Collaborator

Oh wow....
I really don't think we can afford # 3 with any line width.

I think I like the line breaking strategy of # 2 overall, but I think we shouldn't break at all if the whole statement fits in one line.

@belav
Copy link
Owner Author

belav commented Jun 5, 2021

I really don't think we can afford # 3 with any line width.

Good point, it would probably look nice for some switch expressions, but for this one would end up with lines that are way too long.

I think I like the line breaking strategy of # 2 overall, but I think we shouldn't break at all if the whole statement fits in one line.

Are you thinking each individual statement of the switch wouldn't break unless it gets too long? Or that if any of the individual statements of the switch break then they all break?

@belav
Copy link
Owner Author

belav commented Jun 20, 2021

Another example of a big switch expression, which may change how we decide to format these.

            return node switch
            {
                InvocationExpressionSyntax
                or ParenthesizedLambdaExpressionSyntax
                or ObjectCreationExpressionSyntax
                or ElementAccessExpressionSyntax
                or ArrayCreationExpressionSyntax
                or ImplicitArrayCreationExpressionSyntax => Doc.Group(
                    Doc.GroupWithId(groupId, Doc.Indent(Doc.Line)),
                    Doc.IndentIfBreak(Doc.Group(Node.Print(node)), groupId)
                ),
                InitializerExpressionSyntax when node.Kind()
                is SyntaxKind.CollectionInitializerExpression => Doc.Group(
                    Doc.Line,
                    Node.Print(node)
                ),
                AnonymousObjectCreationExpressionSyntax
                or AnonymousMethodExpressionSyntax
                or InitializerExpressionSyntax
                or ConditionalExpressionSyntax
                or SwitchExpressionSyntax
                or LambdaExpressionSyntax
                or AwaitExpressionSyntax
                or WithExpressionSyntax => Doc.Group(
                    Doc.Group(Doc.Indent(Doc.Line)),
                    Node.Print(node)
                ),
                BinaryExpressionSyntax
                or InterpolatedStringExpressionSyntax
                or IsPatternExpressionSyntax
                or LiteralExpressionSyntax
                or QueryExpressionSyntax
                or StackAllocArrayCreationExpressionSyntax => Doc.Indent(
                    Doc.Group(Doc.Line, Node.Print(node))
                ),
                _ => Doc.Group(Doc.Indent(Doc.Line), Doc.Indent(Node.Print(node)))
            };
// Looks pretty decent as
                InvocationExpressionSyntax
                    or ParenthesizedLambdaExpressionSyntax
                    or ObjectCreationExpressionSyntax
                    or ElementAccessExpressionSyntax
                    or ArrayCreationExpressionSyntax
                    or ImplicitArrayCreationExpressionSyntax 
                => Doc.Group(
                    Doc.GroupWithId(groupId, Doc.Indent(Doc.Line)),
                    Doc.IndentIfBreak(Doc.Group(Node.Print(node)), groupId)
                ),
                InitializerExpressionSyntax when node.Kind()
                    is SyntaxKind.CollectionInitializerExpression
                => Doc.Group(
                    Doc.Line,
                    Node.Print(node)
                ),
                AnonymousObjectCreationExpressionSyntax
                    or AnonymousMethodExpressionSyntax
                    or InitializerExpressionSyntax
                    or ConditionalExpressionSyntax
                    or SwitchExpressionSyntax
                    or LambdaExpressionSyntax
                    or AwaitExpressionSyntax
                    or WithExpressionSyntax 
                => Doc.Group(
                    Doc.Group(Doc.Indent(Doc.Line)),
                    Node.Print(node)
                ),
                BinaryExpressionSyntax
                    or InterpolatedStringExpressionSyntax
                    or IsPatternExpressionSyntax
                    or LiteralExpressionSyntax
                    or QueryExpressionSyntax
                    or StackAllocArrayCreationExpressionSyntax 
                => Doc.Indent(
                    Doc.Group(Doc.Line, Node.Print(node))
                ),
                _ => Doc.Group(Doc.Indent(Doc.Line), Doc.Indent(Node.Print(node)))
            };

@belav belav self-assigned this Jul 11, 2021
@belav belav added this to the 0.9.8 milestone Jul 11, 2021
belav added a commit that referenced this issue Jul 11, 2021
belav added a commit that referenced this issue Jul 12, 2021
* Better formatting for switch expressions

closes #237

* Fixing some edge cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:formatting type:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants