Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 578 #591

Merged
merged 6 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}
Loading