Better formatting for ternary expressions
This commit is contained in:
parent
91107b1cc4
commit
9284f1afb6
|
@ -1 +1 @@
|
|||
Subproject commit f5c30c0528a81cecaed731272747c9bbfb68a355
|
||||
Subproject commit bce47174cc2ea6cfd26d007a9c41108e4fb28f0e
|
25
src/dfmt.d
25
src/dfmt.d
|
@ -266,7 +266,8 @@ private:
|
|||
switch (current.type)
|
||||
{
|
||||
case tok!"*":
|
||||
if (!assumeSorted(astInformation.spaceAfterLocations).equalRange(current.index).empty)
|
||||
if (!assumeSorted(astInformation.spaceAfterLocations)
|
||||
.equalRange(current.index).empty)
|
||||
{
|
||||
writeToken();
|
||||
write(" ");
|
||||
|
@ -287,6 +288,17 @@ private:
|
|||
case tok!"(":
|
||||
writeParens();
|
||||
break;
|
||||
case tok!":":
|
||||
if (!assumeSorted(astInformation.ternaryColonLocations)
|
||||
.equalRange(current.index).empty)
|
||||
{
|
||||
write(" ");
|
||||
writeToken();
|
||||
write(" ");
|
||||
}
|
||||
else
|
||||
writeToken();
|
||||
break;
|
||||
case tok!"@":
|
||||
case tok!"!":
|
||||
case tok!"...":
|
||||
|
@ -294,7 +306,6 @@ private:
|
|||
case tok!"++":
|
||||
case tok!"--":
|
||||
case tok!"$":
|
||||
case tok!":":
|
||||
writeToken();
|
||||
break;
|
||||
case tok!"]":
|
||||
|
@ -769,6 +780,9 @@ struct ASTInformation
|
|||
|
||||
/// Locations of unary operators
|
||||
size_t[] unaryLocations;
|
||||
|
||||
/// Locations of ':' operators in ternary expressions
|
||||
size_t[] ternaryColonLocations;
|
||||
}
|
||||
|
||||
/// Collects information from the AST that is useful for the formatter
|
||||
|
@ -841,6 +855,13 @@ final class FormatVisitor : ASTVisitor
|
|||
unary.accept(this);
|
||||
}
|
||||
|
||||
override void visit(const TernaryExpression ternary)
|
||||
{
|
||||
if (ternary.colon.type != tok!"")
|
||||
astInformation.ternaryColonLocations ~= ternary.colon.index;
|
||||
ternary.accept(this);
|
||||
}
|
||||
|
||||
private:
|
||||
ASTInformation* astInformation;
|
||||
alias visit = ASTVisitor.visit;
|
||||
|
|
Loading…
Reference in New Issue