Skip to content

Commit

Permalink
Don't indent conditionals too much (#362)
Browse files Browse the repository at this point in the history
closes #83
  • Loading branch information
belav authored Jul 20, 2021
1 parent 7d17434 commit 1b92da6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ public class ClassName

public bool CloseParenIndents = someCondition
? this.SomeMethodCallThatIsLonger____________________(
someLongValue,
someLongValue,
someLongValue
)
someLongValue,
someLongValue,
someLongValue
)
: this.SomeMethodCallThatIsLonger____________________(
someLongValue,
someLongValue,
someLongValue
);
someLongValue,
someLongValue,
someLongValue
);

void Method()
{
var nestedConditions = someCondition
? someOtherCondition______________
? someValue_______________________
: someOtherValue___________________
: someThirdCondition______________
? someThirdValue_________________
: someOtherThirdValue_______________________;

var fileContents = File.ReadAllText(
file,
// leading here should break parameters and GetEncoding should still group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,10 @@ class ClassName
) =>
expression.Type.IsValueType
? (
expression.Type.IsNullableType()
? Condition(
Property(expression, "HasValue"),
ToType(@else, then.Type),
then
)
: @else
)
expression.Type.IsNullableType()
? Condition(Property(expression, "HasValue"), ToType(@else, then.Type), then)
: @else
)
: Condition(ReferenceEqual(expression, Null), then, ToType(@else, then.Type));

private bool VisitChildForCompare(Pair<XmlNode> tuple, ref int result)
Expand All @@ -165,22 +161,16 @@ class ClassName
result = (null == tuple.Item1)
? -1
: (null == tuple.Item2)
? +1
? +1
: 0 != (r = comparer.Compare(tuple.Item1.LocalName, tuple.Item2.LocalName))
? r
: 0
!= (
r = comparer.Compare(
tuple.Item1.LocalName,
tuple.Item2.LocalName
)
)
? r
: 0
!= (
r = comparer.Compare(
tuple.Item1.NamespaceURI,
tuple.Item2.NamespaceURI
)
) ? r : 0
!= (
r = comparer.Compare(
tuple.Item1.NamespaceURI,
tuple.Item2.NamespaceURI
)
) ? r : 0
);
}
}
3 changes: 2 additions & 1 deletion Src/CSharpier/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public static IndentIfBreak IndentIfBreak(Doc contents, string groupId) =>

public static ConditionalGroup ConditionalGroup(params Doc[] options) => new(options);

public static Align Align(int alignment, Doc contents) => new(alignment, contents);
public static Align Align(int alignment, params Doc[] contents) =>
new(alignment, contents.Length == 1 ? contents[0] : Concat(contents));
}

public enum CommentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ public static class ConditionalExpression
{
public static Doc Print(ConditionalExpressionSyntax node)
{
Doc[] contents = {
Doc.Line,
Token.PrintWithSuffix(node.QuestionToken, " "),
Doc.Align(2, Node.Print(node.WhenTrue)),
Doc.Line,
Token.PrintWithSuffix(node.ColonToken, " "),
Doc.Align(2, Node.Print(node.WhenFalse))
};

return Doc.Concat(
Node.Print(node.Condition),
Doc.Group(
Doc.Indent(
Doc.Line,
Token.PrintWithSuffix(node.QuestionToken, " "),
Doc.Indent(Node.Print(node.WhenTrue)),
Doc.Line,
Token.PrintWithSuffix(node.ColonToken, " "),
Doc.Indent(Node.Print(node.WhenFalse))
)
node.Parent is ConditionalExpressionSyntax
? Doc.Align(2, contents)
: Doc.Indent(contents)
)
);
}
Expand Down

0 comments on commit 1b92da6

Please sign in to comment.