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 - keep things on same line if possible #348

Open
belav opened this issue Jul 11, 2021 · 0 comments
Open

SwitchExpression - keep things on same line if possible #348

belav opened this issue Jul 11, 2021 · 0 comments

Comments

@belav
Copy link
Owner

belav commented Jul 11, 2021

Ideally we would format the following

  var symbol = context.Operation switch
  {
      _ => throw new InvalidOperationException(
          "Unexpected operation kind: " + context.Operation.Kind
      ),
  };
// which is currently
  var symbol = context.Operation switch
  {
      _ 
        => throw new InvalidOperationException(
            "Unexpected operation kind: " + context.Operation.Kind
        ),
  };

The Expression breaks, but the first line of it fits onto the same line as the _ =>
The quick version of this was to add this to SwitchExpression, but that ruined a number of other edge cases.

SeparatedSyntaxList.Print(
    node.Arms,
    o =>
        Doc.Group(
            Node.Print(o.Pattern),
            o.WhenClause != null
                ? Doc.Concat(" ", Node.Print(o.WhenClause))
                : Doc.Null,
            Doc.ConditionalGroup(
                Doc.Concat(
                    " ",
                    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,
                            " "
                        ),
                        Node.Print(o.Expression)
                    )
                )
            )
        ),
    Doc.HardLine
)

This could be re-evaluated after we handle #7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant