Skip to content

Commit

Permalink
Getting pattern matching formatting straightened out.
Browse files Browse the repository at this point in the history
closes #154
  • Loading branch information
belav committed Aug 10, 2021
1 parent 9aae837 commit 535fb30
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ class ClassName
return;
}

if (
MethodKind
is not (MethodKind.Ordinary_________________________
or MethodKind.LocalFunctionn)
) {
return;
}

if (expr is < 'A' or > 'Z')
{
return;
Expand All @@ -35,14 +27,14 @@ class ClassName
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 (
Nullable.GetUnderlyingType111111111111111(typeof(T)) is Type innerType
&& innerType.IsEnum
someRandomValue___________________ is SomeRandomType someRandomType
&& someRandomType.IsEnum
) {
return;
}
Expand All @@ -56,5 +48,105 @@ class ClassName
{
return;
}

if (
someValue_________________________ is SomeType_________________ someType
&& someType.SomeProperty
) { }

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

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

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

var value =
someOtherValue
is SomeType___________________
{
SomeProperty: SomeLongType_______________
or SomeOtherLongType___________________
};

var value =
someOtherValue
is SomeType___________________
or SomeOtherType___________________
or SomeThirdType___________
&& someLongValue_________________;

if (someOtherValue is (SomeType or SomeOtherType))
{
return;
}

if (someOtherValue is not (SomeType or SomeOtherType))
{
return;
}

if (
someOtherValue_____________
is (SomeLongType_____________ or SomeOtherLongType_____________)
) {
return;
}

if (
someOtherValue_____________
is not (SomeLongType_____________ or SomeOtherLongType_____________)
) {
return;
}

if (
node is SomeType_______________
{
SomeProperty: SomeOtherType_____________________________
}
) {
return;
}

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

if (
!(
node is PrefixUnaryExpressionSyntax
{
Operand: ParenthesizedExpressionSyntax or IsPatternExpressionSyntax
}
)
) {
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,28 @@ class ClassName
or AnotherObject
or OrEvenSomeOtherObject_________________
=> CallSomeMethod(someValue),
SomeOtherObject
{
SomeProperty: SomeOtherProject
}
or AnotherObject
=> CallSomeMethod(someValue),
AnotherObject
or SomeOtherObject
{
SomeProperty: SomeOtherProject
}
=> CallSomeMethod(someValue),
_ => CallSomeMethod(someValue)
};

return someValue switch
{
{ IsParameter: true, } => noExtraLineAfterThis,
{
IsParameter: someLongValue____________________________________________,
}
=> someOtherValue
};
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public static IndentDoc Indent(params Doc[] contents) =>

public static IndentDoc Indent(List<Doc> contents) => new() { Contents = Concat(contents) };

public static Doc IndentIf(bool condition, Concat contents)
{
return condition ? Doc.Indent(contents) : contents;
}

public static IfBreak IfBreak(
Doc breakContents,
Doc flatContents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private static List<Doc> PrintBinaryExpression(SyntaxNode node)
var shouldGroup =
binaryExpressionSyntax.Kind() != binaryExpressionSyntax.Parent!.Kind()
&& binaryExpressionSyntax.Left.GetType() != binaryExpressionSyntax.GetType()
&& binaryExpressionSyntax.Right.GetType() != binaryExpressionSyntax.GetType();
&& binaryExpressionSyntax.Right.GetType() != binaryExpressionSyntax.GetType()
&& binaryExpressionSyntax.Left is not IsPatternExpressionSyntax;

// nested ?? have the next level on the right side, everything else has it on the left
var binaryOnTheRight =
Expand Down
66 changes: 61 additions & 5 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BinaryPattern.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using CSharpier.DocTypes;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand All @@ -7,12 +8,67 @@ public static class BinaryPattern
{
public static Doc Print(BinaryPatternSyntax node)
{
return Doc.Concat(
Node.Print(node.Left),
Doc.Line,
Token.PrintWithSuffix(node.OperatorToken, " "),
Node.Print(node.Right)
return Doc.IndentIf(
node.Parent is SubpatternSyntax,
Doc.Concat(
Node.Print(node.Left),
Doc.Line,
Token.PrintWithSuffix(node.OperatorToken, " "),
Node.Print(node.Right)
)
);
}
}
}
/*
// review this, there are more edge cases
https://github.com/belav/aspnetcore/pull/25/files
// what should this do?
if (
someOtherValue_____________
is (SomeLongType_____________________________________
or SomeLongType_____________________________________)
) {
return;
}
// this is uh, ugly, maybe just don't indent the { } ?
// also the new break fore is makes this version uglier, maybe break before is should only be for variable stuff?
if (
!(
node is PrefixUnaryExpressionSyntax
{
Operand: ParenthesizedExpressionSyntax
{
Expression: IsPatternExpressionSyntax
{
Pattern: DeclarationPatternSyntax,
} isPattern,
},
} notExpression
)
) {
return;
}
// could this be improved??
return someValue switch
{
OrEvenSomeOtherObject_________________
=> CallSomeMethod(someValue),
SomeOtherObject
{
SomeProperty: SomeOtherProject
}
or AnotherObject
=> CallSomeMethod(someValue),
AnotherObject
or SomeOtherObject
{
SomeProperty: SomeOtherProject
}
=> CallSomeMethod(someValue)
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ public static class IsPatternExpression
{
public static Doc Print(IsPatternExpressionSyntax node)
{
var useSpace =
node.Parent is IfStatementSyntax or ParenthesizedExpressionSyntax
&& node.Pattern is not (ParenthesizedPatternSyntax or UnaryPatternSyntax);

return Doc.Group(
Node.Print(node.Expression),
Doc.Indent(
Doc.Line,
Token.PrintWithSuffix(node.IsKeyword, " "),
Doc.Indent(Node.Print(node.Pattern))
Doc.IndentIf(
node.Parent is not (IfStatementSyntax or ParenthesizedExpressionSyntax),
Doc.Concat(
useSpace ? " " : Doc.Line,
Token.Print(node.IsKeyword),
node.Pattern is RecursivePatternSyntax { Type: null } ? Doc.Null : " ",
Node.Print(node.Pattern)
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class ParenthesizedPattern
{
public static Doc Print(ParenthesizedPatternSyntax node)
{
return Doc.Concat(
return Doc.Group(
Token.Print(node.OpenParenToken),
Node.Print(node.Pattern),
Token.Print(node.CloseParenToken)
Expand Down
Loading

0 comments on commit 535fb30

Please sign in to comment.