Skip to content

Commit

Permalink
Fixing regression with invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Jan 21, 2024
1 parent 50d8502 commit 6260db7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ var someVariable = this.Property.CallMethod(someValue =>
var someVariable = this.Property()
.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.Property()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ class ClassName
.CallMethod__________________();

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

someThing_______________________
Expand All @@ -246,8 +245,8 @@ class ClassName
IEnumerable<ValueProviderFactory> valueProviderFactories =
new ModelBinderAttribute_______().GetValueProviderFactories(config);

var something________________________________________ = x.SomeProperty
.CallMethod(longParameter_____________, longParameter_____________)
var something________________________________________ = x
.SomeProperty.CallMethod(longParameter_____________, longParameter_____________)
.CallMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private static List<List<PrintedNode>> GroupPrintedNodesOnLines(List<PrintedNode
{
if (printedNodes[index].Node is ConditionalAccessExpressionSyntax)
{
currentGroup = new List<PrintedNode>();
currentGroup = [];
groups.Add(currentGroup);
}
else if (
Expand All @@ -209,7 +209,7 @@ or IdentifierNameSyntax
&& printedNodes[index + -1].Node is not ConditionalAccessExpressionSyntax
)
{
currentGroup = new List<PrintedNode>();
currentGroup = [];
groups.Add(currentGroup);
}

Expand Down Expand Up @@ -258,49 +258,20 @@ List<PrintedNode> printedNodes
}
}

if (printedNodes[0].Node is not InvocationExpressionSyntax)
if (
printedNodes[0].Node is not (InvocationExpressionSyntax or PostfixUnaryExpressionSyntax)
&& index < printedNodes.Count
&& printedNodes[index].Node
is ElementAccessExpressionSyntax
or PostfixUnaryExpressionSyntax
)
{
for (; index + 1 < printedNodes.Count; ++index)
{
/* this handles the special case where we want ?.Property on the same line
someThing_______________________?.Property
.CallMethod__________________()
.CallMethod__________________();
*/
if (
printedNodes[index].Node is ConditionalAccessExpressionSyntax
&& printedNodes[index + 1].Node
is MemberBindingExpressionSyntax { Parent: MemberAccessExpressionSyntax }
)
{
currentGroup.Add(printedNodes[index]);
currentGroup.Add(printedNodes[index + 1]);
index++;
continue;
}

if (
(
IsMemberish(printedNodes[index].Node)
&& (
IsMemberish(printedNodes[index + 1].Node)
|| printedNodes[index + 1].Node is PostfixUnaryExpressionSyntax
)
)
|| printedNodes[index].Node is PostfixUnaryExpressionSyntax
)
{
currentGroup.Add(printedNodes[index]);
}
else
{
break;
}
}
currentGroup.Add(printedNodes[index]);
index++;
}

groups.Add(currentGroup);
currentGroup = new List<PrintedNode>();
currentGroup = [];

var hasSeenNodeThatRequiresBreak = false;
for (; index < printedNodes.Count; index++)
Expand All @@ -313,17 +284,13 @@ or ConditionalAccessExpressionSyntax
)
{
groups.Add(currentGroup);
currentGroup = new List<PrintedNode>();
currentGroup = [];
hasSeenNodeThatRequiresBreak = false;
}

if (
printedNodes[index].Node
is (
InvocationExpressionSyntax
or ElementAccessExpressionSyntax
or MemberBindingExpressionSyntax
)
is (InvocationExpressionSyntax or ElementAccessExpressionSyntax)
)
{
hasSeenNodeThatRequiresBreak = true;
Expand All @@ -339,11 +306,6 @@ or MemberBindingExpressionSyntax
return groups;
}

private static bool IsMemberish(CSharpSyntaxNode node)
{
return node is MemberAccessExpressionSyntax or ConditionalAccessExpressionSyntax;
}

private static Doc PrintIndentedGroup(ExpressionSyntax node, IList<List<PrintedNode>> groups)
{
if (groups.Count == 0)
Expand Down Expand Up @@ -383,11 +345,23 @@ private static bool ShouldMergeFirstTwoGroups(List<List<PrintedNode>> groups)

var firstNode = groups[0][0].Node;

if (firstNode is IdentifierNameSyntax { Identifier.Text.Length: <= 4 })
if (
firstNode
is IdentifierNameSyntax { Identifier.Text.Length: <= 4 }
or ThisExpressionSyntax
or PredefinedTypeSyntax
or BaseExpressionSyntax
&& (
groups[1].Count == 1
|| groups[1].Skip(1).First().Node
is InvocationExpressionSyntax
or PostfixUnaryExpressionSyntax
)
)
{
return true;
}

return firstNode is ThisExpressionSyntax or PredefinedTypeSyntax or BaseExpressionSyntax;
return false;
}
}

0 comments on commit 6260db7

Please sign in to comment.