diff --git a/src/dfmt/ast_info.d b/src/dfmt/ast_info.d index 56030f1..6ba0f24 100644 --- a/src/dfmt/ast_info.d +++ b/src/dfmt/ast_info.d @@ -63,6 +63,7 @@ struct ASTInformation (structInfoSortedByEndLocation); sort(ufcsHintLocations); ufcsHintLocations = ufcsHintLocations.uniq().array(); + sort(ternaryColonLocations); } /// Locations of end braces for struct bodies @@ -135,6 +136,9 @@ struct ASTInformation /// Opening & closing braces of struct initializers StructInitializerInfo[] structInfoSortedByEndLocation; + + /// Locations ternary expression colons. + size_t[] ternaryColonLocations; } /// Collects information from the AST that is useful for the formatter @@ -438,6 +442,12 @@ final class FormatVisitor : ASTVisitor outStatement.accept(this); } + override void visit(const TernaryExpression ternaryExpression) + { + astInformation.ternaryColonLocations ~= ternaryExpression.colon.index; + ternaryExpression.accept(this); + } + private: ASTInformation* astInformation; } diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 8ea8cc6..d2c0f97 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -841,6 +841,7 @@ private: current.line); immutable bool isStructInitializer = astInformation.structInfoSortedByEndLocation .canFind!(st => st.startLocation < current.index && current.index < st.endLocation); + immutable bool isTernary = astInformation.ternaryColonLocations.canFindIndex(current.index); if (isCase || isAttribute) { @@ -856,7 +857,7 @@ private: newline(); } } - else if (indents.topIs(tok!"]")) // Associative array + else if (indents.topIs(tok!"]") && !isTernary) // Associative array { write(config.dfmt_space_before_aa_colon ? " : " : ": "); ++index; diff --git a/tests/allman/issue0578.d.ref b/tests/allman/issue0578.d.ref new file mode 100644 index 0000000..74fd32b --- /dev/null +++ b/tests/allman/issue0578.d.ref @@ -0,0 +1,8 @@ +void f() +{ + auto t = true ? 1 : 0; + auto a = [true ? 1 : 0]; + auto aa1 = [0: true ? 1 : 0]; + auto aa2 = [0: true ? (false ? 1 : 2) : 3]; + auto aa3 = [0: true ? false ? 1 : 2 : 3]; +} diff --git a/tests/issue0578.d b/tests/issue0578.d new file mode 100644 index 0000000..6e6ea21 --- /dev/null +++ b/tests/issue0578.d @@ -0,0 +1,8 @@ +void f() +{ + auto t = true ? 1 : 0; + auto a = [true ? 1: 0]; + auto aa1 = [0: true ? 1: 0]; + auto aa2 = [0: true ? (false ? 1: 2): 3]; + auto aa3 = [0: true ? false ? 1: 2: 3]; +} diff --git a/tests/knr/issue0578.d.ref b/tests/knr/issue0578.d.ref new file mode 100644 index 0000000..74fd32b --- /dev/null +++ b/tests/knr/issue0578.d.ref @@ -0,0 +1,8 @@ +void f() +{ + auto t = true ? 1 : 0; + auto a = [true ? 1 : 0]; + auto aa1 = [0: true ? 1 : 0]; + auto aa2 = [0: true ? (false ? 1 : 2) : 3]; + auto aa3 = [0: true ? false ? 1 : 2 : 3]; +} diff --git a/tests/otbs/issue0578.d.ref b/tests/otbs/issue0578.d.ref new file mode 100644 index 0000000..9f97fa7 --- /dev/null +++ b/tests/otbs/issue0578.d.ref @@ -0,0 +1,7 @@ +void f() { + auto t = true ? 1 : 0; + auto a = [true ? 1 : 0]; + auto aa1 = [0: true ? 1 : 0]; + auto aa2 = [0: true ? (false ? 1 : 2) : 3]; + auto aa3 = [0: true ? false ? 1 : 2 : 3]; +}