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

Add support for static foreach #304

Merged
merged 4 commits into from Nov 29, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion dub.json
Expand Up @@ -4,6 +4,6 @@
"targetType": "autodetect",
"license": "BSL-1.0",
"dependencies": {
"libdparse": "~>0.7.1-beta.7"
"libdparse": "~>0.7.2-alpha.3"
}
}
14 changes: 14 additions & 0 deletions src/dfmt/formatter.d
Expand Up @@ -895,6 +895,20 @@ private:
indents.push(tok!"if");
formatLeftBrace();
}
else if (currentIs(tok!"{") && indents.topAre(tok!"static", tok!"foreach"))
{
indents.pop();
indents.pop();
indents.push(tok!"foreach");
formatLeftBrace();
}
else if (currentIs(tok!"{") && indents.topAre(tok!"static", tok!"foreach_reverse"))
{
indents.pop();
indents.pop();
indents.push(tok!"foreach_reverse");
formatLeftBrace();
}
}

void formatElse()
Expand Down
6 changes: 4 additions & 2 deletions src/dfmt/indentation.d
Expand Up @@ -176,6 +176,7 @@ private:

int indentSize(const size_t k = size_t.max) const pure nothrow @safe @nogc
{
import std.algorithm : among;
if (index == 0 || k == 0)
return 0;
immutable size_t j = k == size_t.max ? index : k;
Expand All @@ -196,8 +197,9 @@ private:
continue;
immutable currentIsNonWrapTemp = !isWrapIndent(arr[i])
&& isTempIndent(arr[i]) && arr[i] != tok!")" && arr[i] != tok!"!";
if (arr[i] == tok!"static" && (arr[i + 1] == tok!"if"
|| arr[i + 1] == tok!"else") && (i + 2 >= index || arr[i + 2] != tok!"{"))
if (arr[i] == tok!"static"
&& arr[i + 1].among!(tok!"if", tok!"else", tok!"foreach", tok!"foreach_reverse")
&& (i + 2 >= index || arr[i + 2] != tok!"{"))
{
parenCount = pc;
continue;
Expand Down
12 changes: 12 additions & 0 deletions tests/allman/issue0303.d.ref
@@ -0,0 +1,12 @@
static foreach (thing; things)
{
pragma(msg, thing);
}
static foreach_reverse (thing; things)
{
pragma(msg, thing);
}
static foreach (thing; things)
pragma(msg, thing);
static foreach_reverse (thing; things)
pragma(msg, thing);
4 changes: 4 additions & 0 deletions tests/issue0303.d
@@ -0,0 +1,4 @@
static foreach (thing; things){pragma(msg,thing);}
static foreach_reverse (thing; things){pragma(msg,thing);}
static foreach (thing; things) pragma(msg,thing);
static foreach_reverse (thing; things) pragma(msg,thing);
10 changes: 10 additions & 0 deletions tests/otbs/issue0303.d.ref
@@ -0,0 +1,10 @@
static foreach (thing; things) {
pragma(msg, thing);
}
static foreach_reverse (thing; things) {
pragma(msg, thing);
}
static foreach (thing; things)
pragma(msg, thing);
static foreach_reverse (thing; things)
pragma(msg, thing);