Skip to content

Commit

Permalink
Working on moving arrow expression (#1295)
Browse files Browse the repository at this point in the history
Other instances of `=>` stay on the line and break after, this seems to
be the only instance that breaks before `=>` and should be consistent
with the others.

closes #1282
  • Loading branch information
belav committed Aug 16, 2024
1 parent 04ce002 commit 7d1dc56
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ClassName
(DoorState.Closed, Action.Lock, true) => DoorState.Locked,
(DoorState.Locked, Action.Unlock, true) => DoorState.Closed,
(var state, _, _) => state,
(_, _, _)
=> DoorState.LongStateShouldNotGetExtraLine________________________________________,
(_, _, _) =>
DoorState.LongStateShouldNotGetExtraLine________________________________________,
};

return someValue switch
Expand All @@ -31,45 +31,42 @@ class ClassName
{ IsParameter: true } => 1,
// this comment shouldn't affect the next line
{ IsParameter: false } => 0,
SomeLongObject someLongObject
=> CallSomeMethodWith____________________________(someLongObject),
YetAnotherObject
=> CallSomeMethod(
someValue,
andOtherParameters,
thatMakeThisLongEnoughToBreak___________________
),
SomeLongObject someLongObject => CallSomeMethodWith____________________________(
someLongObject
),
YetAnotherObject => CallSomeMethod(
someValue,
andOtherParameters,
thatMakeThisLongEnoughToBreak___________________
),
VeryLongObject_______________________________________________________________
when count > 0
=> CallSomeMethod(someValue),
OneMore
=> "someStrings"
+ "moreStrings"
+ "andMoreStrings_________________________________________",
SomeOtherObject
or AnotherObject
or OrEvenSomeOtherObject_________________
=> CallSomeMethod(someValue),
SomeOtherObject { SomeProperty: SomeOtherProject }
or AnotherObject
=> CallSomeMethod(someValue),
AnotherObject
or SomeOtherObject { SomeProperty: SomeOtherProject }
=> CallSomeMethod(someValue),
when count > 0 => CallSomeMethod(someValue),
OneMore => "someStrings"
+ "moreStrings"
+ "andMoreStrings_________________________________________",
SomeOtherObject or AnotherObject or OrEvenSomeOtherObject_________________ =>
CallSomeMethod(someValue),
SomeOtherObject { SomeProperty: SomeOtherProject } or AnotherObject => CallSomeMethod(
someValue
),
AnotherObject or SomeOtherObject { SomeProperty: SomeOtherProject } => CallSomeMethod(
someValue
),
SomeOtherObject { Property: true } => CallSomeMethod(someValue),
SomeOtherObject { SomeProperty: YetAnotherObject { 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 ",
{ SomeProperty: true } =>
"Just because this is long, don't break the pattern ",
_ => CallSomeMethod(someValue),
};

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

return (original, updated) switch
Expand All @@ -85,8 +82,7 @@ class ClassName
ContainingType: { IsTupleType: true },
TupleElementIndex: var ui
} updatedField
)
=> originalField.Type.Equals(updatedField.Type),
) => originalField.Type.Equals(updatedField.Type),
};

return someValue switch
Expand All @@ -103,12 +99,25 @@ class ClassName
Some.Two => 2,
};

return someValue switch
{
Some.One
// comment
=> 1,
};

return someValue switch
{
(true, true) => someOtherValue,
(false, false)
or someCondition
=> someValue_____________________________________________________,
(false, false) or someCondition =>
someValue_____________________________________________________,
};

return someValue switch
{
_ => throw new ArgumentOutOfRangeException(
"someLongString________________________________________________________________"
),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ClassName
(DoorState.Closed, Action.Lock, true) => DoorState.Locked,
(DoorState.Locked, Action.Unlock, true) => DoorState.Closed,
(var state, _, _) => state,
(_, _, _)
=> DoorState.LongStateShouldNotGetExtraLine________________________________________,
(_, _, _) =>
DoorState.LongStateShouldNotGetExtraLine________________________________________,
};

return someValue switch
Expand All @@ -31,45 +31,42 @@ class ClassName
{ IsParameter: true } => 1,
// this comment shouldn't affect the next line
{ IsParameter: false } => 0,
SomeLongObject someLongObject
=> CallSomeMethodWith____________________________(someLongObject),
YetAnotherObject
=> CallSomeMethod(
someValue,
andOtherParameters,
thatMakeThisLongEnoughToBreak___________________
),
SomeLongObject someLongObject => CallSomeMethodWith____________________________(
someLongObject
),
YetAnotherObject => CallSomeMethod(
someValue,
andOtherParameters,
thatMakeThisLongEnoughToBreak___________________
),
VeryLongObject_______________________________________________________________
when count > 0
=> CallSomeMethod(someValue),
OneMore
=> "someStrings"
+ "moreStrings"
+ "andMoreStrings_________________________________________",
SomeOtherObject
or AnotherObject
or OrEvenSomeOtherObject_________________
=> CallSomeMethod(someValue),
SomeOtherObject { SomeProperty: SomeOtherProject }
or AnotherObject
=> CallSomeMethod(someValue),
AnotherObject
or SomeOtherObject { SomeProperty: SomeOtherProject }
=> CallSomeMethod(someValue),
when count > 0 => CallSomeMethod(someValue),
OneMore => "someStrings"
+ "moreStrings"
+ "andMoreStrings_________________________________________",
SomeOtherObject or AnotherObject or OrEvenSomeOtherObject_________________ =>
CallSomeMethod(someValue),
SomeOtherObject { SomeProperty: SomeOtherProject } or AnotherObject => CallSomeMethod(
someValue
),
AnotherObject or SomeOtherObject { SomeProperty: SomeOtherProject } => CallSomeMethod(
someValue
),
SomeOtherObject { Property: true } => CallSomeMethod(someValue),
SomeOtherObject { SomeProperty: YetAnotherObject { 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 ",
{ SomeProperty: true } =>
"Just because this is long, don't break the pattern ",
_ => CallSomeMethod(someValue),
};

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

return (original, updated) switch
Expand All @@ -85,8 +82,7 @@ class ClassName
ContainingType: { IsTupleType: true },
TupleElementIndex: var ui
} updatedField
)
=> originalField.Type.Equals(updatedField.Type),
) => originalField.Type.Equals(updatedField.Type),
};
}
}
72 changes: 49 additions & 23 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/SwitchExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,7 @@ public static Doc Print(SwitchExpressionSyntax node, FormattingContext context)
Doc.HardLine,
SeparatedSyntaxList.PrintWithTrailingComma(
node.Arms,
(o, _) =>
Doc.Concat(
ExtraNewLines.Print(o),
Token.PrintLeadingTrivia(
o.Pattern.GetLeadingTrivia(),
context.WithSkipNextLeadingTrivia()
),
Doc.Group(
Node.Print(o.Pattern, context),
o.WhenClause != null ? Node.Print(o.WhenClause, context) : Doc.Null,
Doc.Indent(
Doc.Concat(
Doc.Line,
Token.PrintWithSuffix(
o.EqualsGreaterThanToken,
" ",
context
),
Node.Print(o.Expression, context)
)
)
)
),
PrintArm,
Doc.HardLine,
context,
node.CloseBraceToken
Expand All @@ -52,4 +30,52 @@ public static Doc Print(SwitchExpressionSyntax node, FormattingContext context)
Token.Print(node.CloseBraceToken, context)
);
}

private static Doc PrintArm(
SwitchExpressionArmSyntax switchExpressionArm,
FormattingContext context
)
{
var arrowHasComment = switchExpressionArm.EqualsGreaterThanToken.LeadingTrivia.Any(o =>
o.IsComment()
);

var groupId2 = Guid.NewGuid().ToString();
var innerContents = arrowHasComment
? Doc.Indent(
Doc.Concat(
Doc.Line,
Token.PrintWithSuffix(switchExpressionArm.EqualsGreaterThanToken, " ", context),
Node.Print(switchExpressionArm.Expression, context)
)
)
: Doc.Concat(
" ",
Token.Print(switchExpressionArm.EqualsGreaterThanToken, context),
Doc.GroupWithId(groupId2, Doc.Indent(Doc.Line)),
Doc.IndentIfBreak(Node.Print(switchExpressionArm.Expression, context), groupId2)
);

var groupId1 = Guid.NewGuid().ToString();

return Doc.Concat(
ExtraNewLines.Print(switchExpressionArm),
Token.PrintLeadingTrivia(
switchExpressionArm.Pattern.GetLeadingTrivia(),
context.WithSkipNextLeadingTrivia()
),
Doc.Group(
Doc.GroupWithId(
groupId1,
Doc.Concat(
Node.Print(switchExpressionArm.Pattern, context),
switchExpressionArm.WhenClause != null
? Node.Print(switchExpressionArm.WhenClause, context)
: Doc.Null
)
),
innerContents
)
);
}
}

0 comments on commit 7d1dc56

Please sign in to comment.