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

Improve formatting of pattern matching (IsPatternExpression, BinaryPattern, etc) #154

Closed
belav opened this issue Apr 26, 2021 · 7 comments · Fixed by #397
Closed

Improve formatting of pattern matching (IsPatternExpression, BinaryPattern, etc) #154

belav opened this issue Apr 26, 2021 · 7 comments · Fixed by #397

Comments

@belav
Copy link
Owner

belav commented Apr 26, 2021

Should IsPatterns break like this?

        var useLine =
            node.OperatorToken.Kind() is SyntaxKind.BarBarToken
                or SyntaxKind.BarToken
                or SyntaxKind.AmpersandAmpersandToken
                or SyntaxKind.AmpersandToken
                or SyntaxKind.PlusToken;

They currently break like

        var useLine =
            node.OperatorToken.Kind()
                is SyntaxKind.BarBarToken
                    or SyntaxKind.BarToken
                    or SyntaxKind.AmpersandAmpersandToken
                    or SyntaxKind.AmpersandToken
                    or SyntaxKind.PlusToken;
@belav
Copy link
Owner Author

belav commented Apr 26, 2021

See also

        if (
            MethodKind
                is not (MethodKind.Ordinary
                    or MethodKind.LocalFunction)
        ) {
            return;
        }

Which could break like this and still be below the print width

        if (
            MethodKind
                is not (MethodKind.Ordinary or MethodKind.LocalFunction)
        ) {
            return;
        }

@belav
Copy link
Owner Author

belav commented Apr 26, 2021

I believe we want to treat BinaryPatterns more like BinaryExpressions. That requires flattening out the patterns into an array, and then grouping them.

@respel
Copy link

respel commented Jun 1, 2021

var useLine =
node.OperatorToken.Kind() is SyntaxKind.BarBarToken
or SyntaxKind.BarToken
or SyntaxKind.AmpersandAmpersandToken
or SyntaxKind.AmpersandToken
or SyntaxKind.PlusToken;

This doesn't look like valid syntax to me. Maybe I'm missing something.

@belav
Copy link
Owner Author

belav commented Jun 7, 2021

It is using pattern matching, I think some of this wasn't introduced until c# 9
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#logical-patterns

@belav belav changed the title IsPattern breaking IsPattern and BinaryPattern breaking Jul 19, 2021
@belav
Copy link
Owner Author

belav commented Jul 19, 2021

BinaryPatterns are essentially the same as a BinaryExpression and should be printed the same.
They have a Left, OperatorToken and Right.
BinaryPatterns are not currently indented, and things like IsPattern do the indentation, which I don't believe is the way it should be handled.
If we keep IsPattern indenting, then SwitchExpression will also need to indent.

See #37, possibly do this at the same time.

Mixed example

if (
      nestedExpression.Children[j]
          is IntermediateToken cSharpToken && cSharpToken.IsCSharp
  ) { }

// should be

if (
    nestedExpression.Children[j]
        is IntermediateToken cSharpToken
    && cSharpToken.IsCSharp
) { }

@belav
Copy link
Owner Author

belav commented Aug 4, 2021

Another edge case

            if (
                !(node
                    is PrefixUnaryExpressionSyntax { Operand: ParenthesizedExpressionSyntax { Expression: IsPatternExpressionSyntax { Pattern: DeclarationPatternSyntax, } isPattern, }, } notExpression)
            ) {
                return;
            }

@belav belav self-assigned this Aug 7, 2021
@belav
Copy link
Owner Author

belav commented Aug 7, 2021

Probably related

            return rightNode switch
            {
                InitializerExpressionSyntax => Layout.BasicConcatWithoutLine,
                ConditionalExpressionSyntax { Condition: BinaryExpressionSyntax }
                or BinaryExpressionSyntax
                or InterpolatedStringExpressionSyntax
                or IsPatternExpressionSyntax
                or LiteralExpressionSyntax
                or QueryExpressionSyntax
                or StackAllocArrayCreationExpressionSyntax
                or ImplicitObjectCreationExpressionSyntax
                  => Layout.BreakAfterOperator,
                _ => Layout.Fluid
            };

@belav belav changed the title IsPattern and BinaryPattern breaking Improve formatting of pattern matching (IsPatternExpression, BinaryPattern, etc) Aug 10, 2021
@belav belav added this to the 0.9.9 milestone Aug 10, 2021
belav added a commit that referenced this issue Aug 10, 2021
shocklateboy92 added a commit that referenced this issue Aug 19, 2021
* Getting pattern matching formatting straightened out.

closes #154

* Fixing case with empty property pattern

* Fixing edge case with long is pattern

* Format is parenthesizes the same as binary parenthesizes

Co-authored-by: Lasath Fernando <devel@lasath.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants