From 9284f1afb66360a861c6cd3d726d6572bc998f35 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Fri, 16 Jan 2015 11:34:38 -0800 Subject: [PATCH] Better formatting for ternary expressions --- libdparse | 2 +- src/dfmt.d | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/libdparse b/libdparse index f5c30c0..bce4717 160000 --- a/libdparse +++ b/libdparse @@ -1 +1 @@ -Subproject commit f5c30c0528a81cecaed731272747c9bbfb68a355 +Subproject commit bce47174cc2ea6cfd26d007a9c41108e4fb28f0e diff --git a/src/dfmt.d b/src/dfmt.d index e4d460c..91e05f3 100644 --- a/src/dfmt.d +++ b/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;