Skip to content

Commit

Permalink
Fix #578 ternary expressions in AA literals not properly formatted (#591
Browse files Browse the repository at this point in the history
)
  • Loading branch information
danielzuncke authored Oct 22, 2023
1 parent 08fe5d6 commit 35e55bc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/dfmt/ast_info.d
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct ASTInformation
(structInfoSortedByEndLocation);
sort(ufcsHintLocations);
ufcsHintLocations = ufcsHintLocations.uniq().array();
sort(ternaryColonLocations);
}

/// Locations of end braces for struct bodies
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/allman/issue0578.d.ref
Original file line number Diff line number Diff line change
@@ -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];
}
8 changes: 8 additions & 0 deletions tests/issue0578.d
Original file line number Diff line number Diff line change
@@ -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];
}
8 changes: 8 additions & 0 deletions tests/knr/issue0578.d.ref
Original file line number Diff line number Diff line change
@@ -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];
}
7 changes: 7 additions & 0 deletions tests/otbs/issue0578.d.ref
Original file line number Diff line number Diff line change
@@ -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];
}

0 comments on commit 35e55bc

Please sign in to comment.