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

Remove prettier style method chaining #1313

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,18 +1,18 @@
var someVariable =
someObject____________.Property_______________.CallMethod______________________();
var someVariable = someObject____________
.Property_______________
.CallMethod______________________();

var someVariable = someObject.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________
);
var someVariable = someObject
.Property
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);

var someVariable = someObject
.Property()
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);

var someVariable = someObject
.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________
)
.Property
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________)
.CallMethod();

var someVariable = someObject
Expand All @@ -31,10 +31,8 @@ var someVariable = this.CallMethod()
var someVariable = this.Array[1]
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);

var someVariable = this
.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________
)
var someVariable = this.Property
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________)
.CallMethod();

var someVariable = this.CallMethod()
Expand Down
42 changes: 27 additions & 15 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChains.test
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ var someValue = someOtherValue!
.Where(o => someLongCondition__________________________);

var someValue = someOtherValue!
.Thing!.Where(o => someLongCondition__________________________)
.Thing!
.Where(o => someLongCondition__________________________)
.Where(o => someLongCondition__________________________);

var someValue = someOtherValue
.Thing.Where(o => someLongCondition__________________________)
.Thing
.Where(o => someLongCondition__________________________)
.Where(o => someLongCondition__________________________);

var someValue = someOtherValue
Expand All @@ -77,7 +79,8 @@ roleNames
);

roleNames
.Value.Where(o => o.SomeProperty____________________________________)
.Value
.Where(o => o.SomeProperty____________________________________)
.Select(o => o.SomethingElse);

return someCondition
Expand Down Expand Up @@ -105,9 +108,11 @@ CallSomeMethod(
someParameter____________________________________
)!;

var someVariable = someObject.Property.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________________
);
var someVariable = someObject
.Property
.CallMethod(someValue =>
someValue.SomeProperty == someOtherValue___________________________________
);

CallMethod(
firstParameter________________________________,
Expand Down Expand Up @@ -227,41 +232,48 @@ var someValue = CallMethod______________________(
.CallMethod__________________();

someThing_______________________
.Property.CallMethod__________________()
.Property
.CallMethod__________________()
.CallMethod__________________();

someThing_______________________
?.Property.CallMethod__________________()
?.Property
.CallMethod__________________()
.CallMethod__________________();

someThing_______________________
.Property!.CallMethod__________________()
.Property!
.CallMethod__________________()
.CallMethod__________________();

IEnumerable<ValueProviderFactory> valueProviderFactories =
new ModelBinderAttribute_______().GetValueProviderFactories(config);

var something________________________________________ = x
.SomeProperty.CallMethod(longParameter_____________, longParameter_____________)
var something________________________________________ = x.SomeProperty
.CallMethod(longParameter_____________, longParameter_____________)
.CallMethod();

CallMethod(o =>
o.Property.CallMethod_____________________________________________________()
o.Property
.CallMethod_____________________________________________________()
.CallMethod_____________________________________________________()
);

CallMethod(
o,
o.Property.CallMethod_____________________________________________________()
o.Property
.CallMethod_____________________________________________________()
.CallMethod_____________________________________________________()
);

var someValue =
someValue
&& o.Property.CallMethod_____________________________________________________()
&& o.Property
.CallMethod_____________________________________________________()
.CallMethod_____________________________________________________();

o.Property.CallMethod(
o.Property
.CallMethod(
someParameter_____________________________,
someParameter_____________________________
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ int[] someOtherArray =
List<KeyValuePair<string, string>> list =
[
.. attribute
.Targets.Select(target =>
.Targets
.Select(target =>
KeyValuePair.Create(
target,
context.EntityDefinitions.TryGetValue(target, out var type) ? type.ClassName : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public static Doc PrintMemberChain(ExpressionSyntax node, FormattingContext cont

FlattenAndPrintNodes(node, printedNodes, context);

var groups = printedNodes.Any(o => o.Node is InvocationExpressionSyntax)
? GroupPrintedNodesPrettierStyle(printedNodes)
: GroupPrintedNodesOnLines(printedNodes);
var groups = GroupPrintedNodesOnLines(printedNodes);

var oneLine = groups.SelectMany(o => o).Select(o => o.Doc).ToArray();

Expand Down Expand Up @@ -248,93 +246,6 @@ or IdentifierNameSyntax
return groups;
}

private static List<List<PrintedNode>> GroupPrintedNodesPrettierStyle(
List<PrintedNode> printedNodes
)
{
// We want to group the printed nodes in the following manner
//
// a().b.c().d().e
// will be grouped as
// [
// [Identifier, InvocationExpression],
// [MemberAccessExpression], [MemberAccessExpression, InvocationExpression],
// [MemberAccessExpression, InvocationExpression],
// [MemberAccessExpression],
// ]

// so that we can print it as
// a()
// .b.c()
// .d()
// .e

// TODO #451 this whole thing could possibly just turn into a big loop
// based on the current node, and the next/previous node, decide when to create new groups.
// certain nodes need to stay in the current group, other nodes indicate that a new group needs to be created.
var groups = new List<List<PrintedNode>>();
var currentGroup = new List<PrintedNode> { printedNodes[0] };
var index = 1;
for (; index < printedNodes.Count; index++)
{
if (printedNodes[index].Node is InvocationExpressionSyntax)
{
currentGroup.Add(printedNodes[index]);
}
else
{
break;
}
}

if (
printedNodes[0].Node is not (InvocationExpressionSyntax or PostfixUnaryExpressionSyntax)
&& index < printedNodes.Count
&& printedNodes[index].Node
is ElementAccessExpressionSyntax
or PostfixUnaryExpressionSyntax
)
{
currentGroup.Add(printedNodes[index]);
index++;
}

groups.Add(currentGroup);
currentGroup = [];

var hasSeenNodeThatRequiresBreak = false;
for (; index < printedNodes.Count; index++)
{
if (
hasSeenNodeThatRequiresBreak
&& printedNodes[index].Node
is MemberAccessExpressionSyntax
or ConditionalAccessExpressionSyntax
)
{
groups.Add(currentGroup);
currentGroup = [];
hasSeenNodeThatRequiresBreak = false;
}

if (
printedNodes[index].Node
is (InvocationExpressionSyntax or ElementAccessExpressionSyntax)
)
{
hasSeenNodeThatRequiresBreak = true;
}
currentGroup.Add(printedNodes[index]);
}

if (currentGroup.Any())
{
groups.Add(currentGroup);
}

return groups;
}

private static Doc PrintIndentedGroup(ExpressionSyntax node, IList<List<PrintedNode>> groups)
{
if (groups.Count == 0)
Expand Down
Loading