Skip to content

Commit

Permalink
Handling a large number of edge cases for pattern matching.
Browse files Browse the repository at this point in the history
closes #413
  • Loading branch information
belav committed Sep 6, 2021
1 parent a6ac2cb commit 3009f26
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,28 @@ class ClassName
return;
}

if (expr is { Length: 5 })
{
return;
}

if (
expr is
{
SomeProperty: "someValue________________",
SomeOtherProperty: "someOtherValue______________"
}
) {
return;
}

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

if (
someRandomValue___________________ is SomeRandomType someRandomType
Expand All @@ -54,29 +69,28 @@ class ClassName
&& someType.SomeProperty
) { }

var value = someOtherValue is { Property: true };

var value =
someOtherValue
is SomeType___________________
{
SomeProperty: SomeOtherType_____________________________________________
}
or SomeThirdType___________;
{
SomeProperty: SomeOtherType_____________________________________________
}
or SomeThirdType___________;

var value =
someOtherValue
is SomeType___________________
{
SomeProperty: SomeOtherType_____________________________________________,
AnotherProperty: SomeType
}
or SomeThirdType___________;
{
SomeProperty: SomeOtherType_____________________________________________,
AnotherProperty: SomeType
}
or SomeThirdType___________;

var value =
someOtherValue
is SomeType___________________
{
SomeProperty: SomeType or SomeOtherType
};
is SomeType___________________ { SomeProperty: SomeType or SomeOtherType };

var value =
someOtherValue
Expand All @@ -89,8 +103,8 @@ class ClassName
var value =
someOtherValue
is SomeType___________________
or SomeOtherType___________________
or SomeThirdType___________
or SomeOtherType___________________
or SomeThirdType___________
&& someLongValue_________________;

if (someOtherValue is (SomeType or SomeOtherType))
Expand Down Expand Up @@ -160,8 +174,8 @@ class ClassName
}

if (
someLongName____________________________________________________
is { } anotherLongName______________________
someLongName____________________________________________________ is
{ } anotherLongName______________________
) {
return;
}
Expand All @@ -179,5 +193,20 @@ class ClassName
) {
return;
}

if (
someLongName_____________
is SomeObjectType
or SomeOtherObjectType
or YetAnotherObjectType
) {
return;
}

if (
someCondition
&& someLongName_____________
is { Kind: SomeObjectType, Value: "___________________________________" }
) { }
}
}
28 changes: 12 additions & 16 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/SwitchExpressions.cst
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ class ClassName
_ => throw new global::System.Exception()
};

var newState = (GetState(), action, hasKey) switch
return (GetState(), action, hasKey) switch
{
// Open/Close
// Comment
(DoorState.Closed, Action.Open, _) => DoorState.Opened,
(DoorState.Opened, Action.Close, _) => DoorState.Closed,

// Locking
// ExtraLineBeforeThis
(DoorState.Closed, Action.Lock, true) => DoorState.Locked,
(DoorState.Locked, Action.Unlock, true) => DoorState.Closed,

// Do nothing
(var state, _, _) => state,
(_, _, _)
=> DoorState.LongStateShouldNotGetExtraLine________________________________________
Expand Down Expand Up @@ -52,27 +50,25 @@ class ClassName
or AnotherObject
or OrEvenSomeOtherObject_________________
=> CallSomeMethod(someValue),
SomeOtherObject
{
SomeProperty: SomeOtherProject
}
SomeOtherObject { SomeProperty: SomeOtherProject }
or AnotherObject
=> CallSomeMethod(someValue),
AnotherObject
or SomeOtherObject
{
SomeProperty: SomeOtherProject
}
or SomeOtherObject { SomeProperty: SomeOtherProject }
=> CallSomeMethod(someValue),
SomeOtherObject { Property: true } => CallSomeMethod(someValue),
SomeOtherObject { SomeProperty: YetAnotherObject { Property: true } }
=> CallSomeMethod(someValue),
{ } otherSyntax => CallSomeMethod(otherSyntax),
{ SomeProperty: true }
=> "Just because this is long, don't break the pattern ",
_ => CallSomeMethod(someValue)
};

return someValue switch
{
{ IsParameter: true, } => noExtraLineAfterThis,
{
IsParameter: someLongValue____________________________________________,
}
{ IsParameter: someLongValue____________________________________________, }
=> someOtherValue
};

Expand Down
21 changes: 21 additions & 0 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/SwitchStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,26 @@ public class ClassName
default:
return;
}

switch (patternMatching)
{
case SomeLongRecursivePattern
{
SomeProperty: " ",
SomeOtherProperty: { SomeProperty: One or Two }
}:
return 1;
case SomeShortRecursivePattern { SomeProperty: "" }:
return 2;
default:
break;
}

switch (positionalPattern1, positionalPattern2)
{
case (SomeValue, _) when !isEquality:
case (_, SomeValue) when !isEquality:
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class BinaryPattern
public static Doc Print(BinaryPatternSyntax node)
{
return Doc.IndentIf(
node.Parent is SubpatternSyntax,
node.Parent is SubpatternSyntax or IsPatternExpressionSyntax,
Doc.Concat(
Node.Print(node.Left),
Doc.Line,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ public static Doc Print(IsPatternExpressionSyntax node)
{
return Doc.Group(
Node.Print(node.Expression),
Doc.Indent(
Doc.Line,
Token.Print(node.IsKeyword),
node.Pattern is RecursivePatternSyntax { Type: null } ? Doc.Null : " ",
Node.Print(node.Pattern)
)
Doc.Indent(Doc.Line, Token.Print(node.IsKeyword), " ", Node.Print(node.Pattern))
);
}

Expand All @@ -32,25 +27,26 @@ public static Doc Print(IsPatternExpressionSyntax node)
);
}

if (recursivePattern.Type is null)
if (recursivePattern.Type is not null)
{
return Doc.Group(
Node.Print(node.Expression),
Doc.Line,
Token.Print(node.IsKeyword),
RecursivePattern.Print(recursivePattern)
Doc.Group(
Node.Print(node.Expression),
Doc.Line,
Token.Print(node.IsKeyword),
" ",
Node.Print(recursivePattern.Type)
),
RecursivePattern.PrintWithOutType(recursivePattern)
);
}

return Doc.Group(
Doc.Group(
Node.Print(node.Expression),
Doc.Line,
Token.Print(node.IsKeyword),
" ",
Node.Print(recursivePattern.Type)
),
RecursivePattern.PrintWithOutType(recursivePattern)
Node.Print(node.Expression),
" ",
Token.Print(node.IsKeyword),
Doc.Line,
RecursivePattern.Print(recursivePattern)
);
}
}
Expand Down
58 changes: 33 additions & 25 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/RecursivePattern.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using CSharpier.DocTypes;
using CSharpier.Utilities;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json;

namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters
{
Expand All @@ -28,7 +30,9 @@ private static Doc Print(RecursivePatternSyntax node, bool includeType)
if (node.PositionalPatternClause != null)
{
result.Add(
node.Parent is SwitchExpressionArmSyntax ? Doc.Null : Doc.SoftLine,
node.Parent is SwitchExpressionArmSyntax or CasePatternSwitchLabelSyntax
? Doc.Null
: Doc.SoftLine,
Token.PrintLeadingTrivia(node.PositionalPatternClause.OpenParenToken),
Doc.Group(
Token.PrintWithoutLeadingTrivia(
Expand Down Expand Up @@ -58,34 +62,38 @@ private static Doc Print(RecursivePatternSyntax node, bool includeType)
{
if (!node.PropertyPatternClause.Subpatterns.Any())
{
result.Add(" { }");
if (node.Type != null)
{
result.Add(" ");
}
result.Add("{ }");
}
else
{
result.Add(
node.Parent switch
{
IsPatternExpressionSyntax => Doc.Line,
SwitchExpressionArmSyntax => Doc.Null,
_ => Doc.SoftLine
},
Token.Print(node.PropertyPatternClause.OpenBraceToken),
Doc.Indent(
node.PropertyPatternClause.Subpatterns.Any() ? Doc.Line : Doc.Null,
SeparatedSyntaxList.Print(
node.PropertyPatternClause.Subpatterns,
subpatternNode =>
Doc.Group(
subpatternNode.NameColon != null
? NameColon.Print(subpatternNode.NameColon)
: Doc.Null,
Node.Print(subpatternNode.Pattern)
),
Doc.Line
)
),
Doc.Line,
Token.Print(node.PropertyPatternClause.CloseBraceToken)
Token.PrintLeadingTrivia(node.PropertyPatternClause.OpenBraceToken),
Doc.Group(
node.Type != null ? Doc.Line : Doc.Null,
Token.PrintWithoutLeadingTrivia(
node.PropertyPatternClause.OpenBraceToken
),
Doc.Indent(
node.PropertyPatternClause.Subpatterns.Any() ? Doc.Line : Doc.Null,
SeparatedSyntaxList.Print(
node.PropertyPatternClause.Subpatterns,
subpatternNode =>
Doc.Group(
subpatternNode.NameColon != null
? NameColon.Print(subpatternNode.NameColon)
: Doc.Null,
Node.Print(subpatternNode.Pattern)
),
Doc.Line
)
),
Doc.Line,
Token.Print(node.PropertyPatternClause.CloseBraceToken)
)
);
}
}
Expand Down

0 comments on commit 3009f26

Please sign in to comment.