csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

SwitchExpression - keep things on same line if possible

Open belav opened this issue 4 years ago • 0 comments

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

belav avatar Jul 11 '21 18:07 belav