Skip to content

Commit

Permalink
Fix array literal indentation in foreach (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
belka-ew authored Jun 14, 2022
1 parent 036da91 commit d862d8a
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dfmt.indentation;
import dfmt.tokens;
import dfmt.wrapping;
import std.array;
import std.algorithm.comparison : among;
import std.algorithm.comparison : among, max;

/**
* Formats the code contained in `buffer` into `output`.
Expand Down Expand Up @@ -196,6 +196,11 @@ private:
/// True if the next "else" should be formatted as a single line
bool inlineElse;

/// Tracks paren depth on a single line. This information can be used to
/// indent array literals inside parens, since arrays are indented only once
/// and paren indentation is ignored.line breaks and "[" reset the counter.
int parenDepthOnLine;

void formatStep()
{
import std.range : assumeSorted;
Expand Down Expand Up @@ -597,6 +602,7 @@ private:
writeToken();
if (p == tok!"(")
{
++parenDepthOnLine;
// If the file starts with an open paren, just give up. This isn't
// valid D code.
if (index < 2)
Expand All @@ -616,9 +622,7 @@ private:

if (arrayInitializerStart && isMultilineAt(index - 1))
{
if (peekBack2Is(tok!"(")) {
indents.pop();
}
revertParenIndentation();

// Use the close bracket as the indent token to distinguish
// the array initialiazer from an array index in the newline
Expand All @@ -642,9 +646,7 @@ private:
}
else if (p == tok!"[" && config.dfmt_keep_line_breaks == OptionalBoolean.t)
{
if (peekBack2Is(tok!"(")) {
indents.pop();
}
revertParenIndentation();
IndentStack.Details detail;

detail.wrap = false;
Expand Down Expand Up @@ -697,13 +699,27 @@ private:
}
}

void revertParenIndentation()
{
if (parenDepthOnLine)
{
foreach (i; 0 .. parenDepthOnLine)
{
indents.pop();
}
indents.popTempIndents();
}
parenDepthOnLine = 0;
}

void formatRightParen()
in
{
assert(currentIs(tok!")"));
}
do
{
parenDepthOnLine = max(parenDepthOnLine - 1, 0);
parenDepth--;
indents.popWrapIndents();
while (indents.topIsOneOf(tok!"!", tok!")"))
Expand Down Expand Up @@ -1665,6 +1681,8 @@ private:
import std.algorithm : max, canFind;
import dfmt.editorconfig : OptionalBoolean;

parenDepthOnLine = 0;

if (currentIs(tok!"comment") && index > 0 && current.line == tokenEndLine(tokens[index - 1]))
return;

Expand Down
54 changes: 54 additions & 0 deletions tests/allman/foreach_array.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
static foreach (x; [
1,
2,
3,
])
{
}

static foreach_reverse (x; [
1,
2,
3,
])
{
}

void f()
{
foreach (x; [
1,
2,
3,
])
{
}
foreach_reverse (x; [
1,
2,
3,
])
{
}

if (!SymbolTool.instance.workspacesFilesUris.canFind!sameFile(uri))
{
send(TextDocument.publishDiagnostics, new PublishDiagnosticsParams(uri, [
]));
}

foreach (x; map([
1,
2,
3,
]))
{
}
foreach (x; foo!(map!([
1,
2,
3,
])))
{
}
}
1 change: 1 addition & 0 deletions tests/foreach_array.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--keep_line_breaks true
53 changes: 53 additions & 0 deletions tests/foreach_array.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
static foreach (x; [
1,
2,
3,
])
{
}

static foreach_reverse (x; [
1,
2,
3,
])
{
}

void f()
{
foreach (x; [
1,
2,
3,
])
{
}
foreach_reverse (x; [
1,
2,
3,
])
{
}

if (!SymbolTool.instance.workspacesFilesUris.canFind!sameFile(uri))
{
send(TextDocument.publishDiagnostics, new PublishDiagnosticsParams(uri, []));
}

foreach (x; map([
1,
2,
3,
]))
{
}
foreach (x; foo!(map!([
1,
2,
3,
])))
{
}
}
47 changes: 47 additions & 0 deletions tests/knr/foreach_array.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
static foreach (x; [
1,
2,
3,
]) {
}

static foreach_reverse (x; [
1,
2,
3,
]) {
}

void f()
{
foreach (x; [
1,
2,
3,
]) {
}
foreach_reverse (x; [
1,
2,
3,
]) {
}

if (!SymbolTool.instance.workspacesFilesUris.canFind!sameFile(uri)) {
send(TextDocument.publishDiagnostics, new PublishDiagnosticsParams(uri, [
]));
}

foreach (x; map([
1,
2,
3,
])) {
}
foreach (x; foo!(map!([
1,
2,
3,
]))) {
}
}
46 changes: 46 additions & 0 deletions tests/otbs/foreach_array.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
static foreach (x; [
1,
2,
3,
]) {
}

static foreach_reverse (x; [
1,
2,
3,
]) {
}

void f() {
foreach (x; [
1,
2,
3,
]) {
}
foreach_reverse (x; [
1,
2,
3,
]) {
}

if (!SymbolTool.instance.workspacesFilesUris.canFind!sameFile(uri)) {
send(TextDocument.publishDiagnostics, new PublishDiagnosticsParams(uri, [
]));
}

foreach (x; map([
1,
2,
3,
])) {
}
foreach (x; foo!(map!([
1,
2,
3,
]))) {
}
}
2 changes: 2 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ for braceStyle in allman otbs knr
do
for source in *.d
do
test "$(basename $source '.d')" = 'test' && continue

echo "${source}.ref" "${braceStyle}/${source}.out"
argsFile=$(basename "${source}" .d).args
if [ -e "${argsFile}" ]; then
Expand Down

0 comments on commit d862d8a

Please sign in to comment.