Better formatting for ternary expressions

This commit is contained in:
Hackerpilot 2015-01-16 11:34:38 -08:00
parent 91107b1cc4
commit 9284f1afb6
2 changed files with 24 additions and 3 deletions

@ -1 +1 @@
Subproject commit f5c30c0528a81cecaed731272747c9bbfb68a355 Subproject commit bce47174cc2ea6cfd26d007a9c41108e4fb28f0e

View File

@ -266,7 +266,8 @@ private:
switch (current.type) switch (current.type)
{ {
case tok!"*": case tok!"*":
if (!assumeSorted(astInformation.spaceAfterLocations).equalRange(current.index).empty) if (!assumeSorted(astInformation.spaceAfterLocations)
.equalRange(current.index).empty)
{ {
writeToken(); writeToken();
write(" "); write(" ");
@ -287,6 +288,17 @@ private:
case tok!"(": case tok!"(":
writeParens(); writeParens();
break; break;
case tok!":":
if (!assumeSorted(astInformation.ternaryColonLocations)
.equalRange(current.index).empty)
{
write(" ");
writeToken();
write(" ");
}
else
writeToken();
break;
case tok!"@": case tok!"@":
case tok!"!": case tok!"!":
case tok!"...": case tok!"...":
@ -294,7 +306,6 @@ private:
case tok!"++": case tok!"++":
case tok!"--": case tok!"--":
case tok!"$": case tok!"$":
case tok!":":
writeToken(); writeToken();
break; break;
case tok!"]": case tok!"]":
@ -769,6 +780,9 @@ struct ASTInformation
/// Locations of unary operators /// Locations of unary operators
size_t[] unaryLocations; size_t[] unaryLocations;
/// Locations of ':' operators in ternary expressions
size_t[] ternaryColonLocations;
} }
/// Collects information from the AST that is useful for the formatter /// Collects information from the AST that is useful for the formatter
@ -841,6 +855,13 @@ final class FormatVisitor : ASTVisitor
unary.accept(this); unary.accept(this);
} }
override void visit(const TernaryExpression ternary)
{
if (ternary.colon.type != tok!"")
astInformation.ternaryColonLocations ~= ternary.colon.index;
ternary.accept(this);
}
private: private:
ASTInformation* astInformation; ASTInformation* astInformation;
alias visit = ASTVisitor.visit; alias visit = ASTVisitor.visit;