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

Fixing indentation issues with conditionals inside initializers #534

Merged
merged 7 commits into from
Jan 3, 2022
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
2 changes: 1 addition & 1 deletion Scripts/CreateTestingPR.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ if ($firstRun) {
Write-Output $newPr
}

Pop-Location
Set-Location $repositoryRoot
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,13 @@ public class ClassName
: c
? d
: e;

var invocationIndent = someCondition
? SomeObject
.CallLongMethod__________________________________________()
.CallLongMethod__________________________________________()
: SomeObject
.CallLongMethod__________________________________________()
.CallLongMethod__________________________________________();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ class ClassName
new Thing { One = 1 },
Two = 2
};

var conditionalsAndInvocations = new List<SomeObject>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, this whole block looks really good ✨

{
this.CallSomeMethod________________________________________()
.CallSomeMethod________________________________________()
? one
: two,
SomeOtherMethod(),
SomethingElse
.CallSomeMethod________________________________________()
.CallSomeMethod________________________________________()
? one
: two,
SomeOtherMethod(),
this.CallSomeMethod________________________________________()
.CallSomeMethod________________________________________(),
someLongCondition___________________________________
&& someLongCondition___________________________________
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we decide not to indent binary operators, again?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case if it were indented, then it would line up with the ?
Maybe #3 below is the way to go? I think there might be some cases where binary operators do get indented.

someLongCondition___________________________________
&& someLongCondition___________________________________
    ? one
    : two

someLongCondition___________________________________
    && someLongCondition___________________________________
    ? one
    : two

someLongCondition___________________________________
    && someLongCondition___________________________________
  ? one
  : two

? one
: two
};
}

private SomeObject someObject =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class ClassName
expression.Type.IsValueType
? (
expression.Type.IsNullableType()
? Condition(Property(expression, "HasValue"), ToType(@else, then.Type), then)
: @else
? Condition(Property(expression, "HasValue"), ToType(@else, then.Type), then)
: @else
)
: Condition(ReferenceEqual(expression, Null), then, ToType(@else, then.Type));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ withLineEnding"
someParameter_____________________,
someParameter_____________________
)
? someValue
: someOtherValue;
? someValue
: someOtherValue;

var someValue =
await SomeObject.CallLongAsyncMethod____________________________________________(
Expand All @@ -155,7 +155,19 @@ withLineEnding"

var someValue =
SomeObject.CallLongMethod____________________________________________________()
? someValue
: someOtherValue;

var someValue = SomeObject
.CallLongMethod____________________________________________________()
.CallLongMethod____________________________________________________()
? someValue
: someOtherValue;

var someValue =
someLongCondition___________________________________
&& someOtheLongCondition___________________________________
? someValue
: someOtherValue;
: someValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ node.Parent is ReturnStatementSyntax
)
: Node.Print(node.Condition),
node.Parent is ConditionalExpressionSyntax or ArgumentSyntax or ReturnStatementSyntax
|| node.Condition is InvocationExpressionSyntax
? Doc.Align(2, innerContents)
: Doc.Indent(innerContents)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,18 @@ private static Doc PrintIndentedGroup(ExpressionSyntax node, IList<List<PrintedN
return Doc.Null;
}

var shouldIndent = !(
node.Parent is ConditionalExpressionSyntax conditionalExpressionSyntax
&& conditionalExpressionSyntax.Condition != node
);

return Doc.IndentIf(
node.Parent is not ConditionalExpressionSyntax,
shouldIndent,
Doc.Group(
Doc.HardLine,
Doc.Join(
Doc.HardLine,
groups.Select(o => Doc.Group(o.Select(o => o.Doc).ToArray()))
groups.Select(o => Doc.Group(o.Select(p => p.Doc).ToArray()))
)
)
);
Expand Down