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

Pattern matching edge cases #431

Merged
merged 2 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -148,6 +162,18 @@ class ClassName
return;
}

if (
node is PrefixUnaryExpressionSyntax
{
Operand:
{
Expression: IsPatternExpressionSyntax or IsPatternExpressionSyntax______________
},
}
) {
return;
}

if (
!(
node is PrefixUnaryExpressionSyntax
Expand All @@ -160,8 +186,8 @@ class ClassName
}

if (
someLongName____________________________________________________
is { } anotherLongName______________________
someLongName____________________________________________________ is
{ } anotherLongName______________________
) {
return;
}
Expand All @@ -179,5 +205,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 @@ -36,8 +31,9 @@ public static Doc Print(IsPatternExpressionSyntax node)
{
return Doc.Group(
Node.Print(node.Expression),
Doc.Line,
" ",
Token.Print(node.IsKeyword),
Doc.Line,
RecursivePattern.Print(recursivePattern)
);
}
Expand Down
11 changes: 10 additions & 1 deletion Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/NameColon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ public static Doc Print(NameColonSyntax node)
{
return Doc.Concat(
Token.Print(node.Name.Identifier),
Token.PrintWithSuffix(node.ColonToken, " ")
Token.PrintWithSuffix(
node.ColonToken,
node.Parent
is SubpatternSyntax
{
Pattern: RecursivePatternSyntax{ Type: null }
}
? Doc.Line
: " "
)
);
}
}
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