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 array literal indentation in foreach #554

Merged
merged 2 commits into from
Jun 14, 2022
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
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