Skip to content

Commit

Permalink
Format multiline arrays first
Browse files Browse the repository at this point in the history
  • Loading branch information
belka-ew committed Jun 12, 2021
1 parent 821b9be commit 5856213
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -612,22 +612,7 @@ private:
immutable bool arrayInitializerStart = p == tok!"["
&& astInformation.arrayStartLocations.canFindIndex(tokens[index - 1].index);

if (p == tok!"[" && config.dfmt_keep_line_breaks == OptionalBoolean.t)
{
IndentStack.Details detail;

detail.wrap = false;
detail.temp = false;
detail.breakEveryItem = false;
detail.mini = tokens[index].line == tokens[index - 1].line;

indents.push(tok!"]", detail);
if (!detail.mini)
{
newline();
}
}
else if (arrayInitializerStart && isMultilineAt(index - 1))
if (arrayInitializerStart && isMultilineAt(index - 1))
{
if (peekBack2Is(tok!"(")) {
indents.pop();
Expand All @@ -653,6 +638,21 @@ private:
linebreakHints = chooseLineBreakTokens(index, tokens[index .. j],
depths[index .. j], config, currentLineLength, indentLevel);
}
else if (p == tok!"[" && config.dfmt_keep_line_breaks == OptionalBoolean.t)
{
IndentStack.Details detail;

detail.wrap = false;
detail.temp = false;
detail.breakEveryItem = false;
detail.mini = tokens[index].line == tokens[index - 1].line;

indents.push(tok!"]", detail);
if (!detail.mini)
{
newline();
}
}
else if (arrayInitializerStart)
{
// This is a short (non-breaking) array/AA value
Expand Down
7 changes: 7 additions & 0 deletions tests/allman/keep_break_in_array_chain.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
unittest
{
functionLengthDoesMatter([
firstFunctionInChain("A").seconFunctionInChain("B").value,
firstFunctionInChain("A").seconFunctionInChain("B").value
]);
}
7 changes: 7 additions & 0 deletions tests/keep_break_in_array_chain.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
unittest
{
functionLengthDoesMatter([
firstFunctionInChain("A").seconFunctionInChain("B").value,
firstFunctionInChain("A").seconFunctionInChain("B").value
]);
}
1 change: 1 addition & 0 deletions tests/keep_break_in_array_chain.d.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--keep_line_breaks=true
6 changes: 6 additions & 0 deletions tests/otbs/keep_break_in_array_chain.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unittest {
functionLengthDoesMatter([
firstFunctionInChain("A").seconFunctionInChain("B").value,
firstFunctionInChain("A").seconFunctionInChain("B").value
]);
}

0 comments on commit 5856213

Please sign in to comment.