From a163eb105f8b77919c6add56d0bc3b0a56a679aa Mon Sep 17 00:00:00 2001 From: Kotet Date: Sat, 25 Nov 2017 13:20:49 +0900 Subject: [PATCH 1/4] Add support for `static foreach` Fix #303 --- src/dfmt/formatter.d | 14 ++++++++++++++ src/dfmt/indentation.d | 3 ++- tests/allman/issue0303.d.ref | 12 ++++++++++++ tests/issue0303.d | 4 ++++ tests/otbs/issue0303.d.ref | 10 ++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/allman/issue0303.d.ref create mode 100644 tests/issue0303.d create mode 100644 tests/otbs/issue0303.d.ref diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 7003bfe..544f234 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -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() diff --git a/src/dfmt/indentation.d b/src/dfmt/indentation.d index 9c0b275..01c14bd 100644 --- a/src/dfmt/indentation.d +++ b/src/dfmt/indentation.d @@ -197,7 +197,8 @@ private: 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!"{")) + || arr[i + 1] == tok!"else" || arr[i + 1] == tok!"foreach" + || arr[i + 1] == tok!"foreach_reverse") && (i + 2 >= index || arr[i + 2] != tok!"{")) { parenCount = pc; continue; diff --git a/tests/allman/issue0303.d.ref b/tests/allman/issue0303.d.ref new file mode 100644 index 0000000..9b974c5 --- /dev/null +++ b/tests/allman/issue0303.d.ref @@ -0,0 +1,12 @@ +static foreach (thing; things) +{ + doStuff(); +} +static foreach_reverse (thing; things) +{ + doStuff(); +} +static foreach (thing; things) + doStuff(); +static foreach_reverse (thing; things) + doStuff(); diff --git a/tests/issue0303.d b/tests/issue0303.d new file mode 100644 index 0000000..3a4d6b3 --- /dev/null +++ b/tests/issue0303.d @@ -0,0 +1,4 @@ +static foreach (thing; things){doStuff();} +static foreach_reverse (thing; things){doStuff();} +static foreach (thing; things) doStuff(); +static foreach_reverse (thing; things) doStuff(); \ No newline at end of file diff --git a/tests/otbs/issue0303.d.ref b/tests/otbs/issue0303.d.ref new file mode 100644 index 0000000..878435f --- /dev/null +++ b/tests/otbs/issue0303.d.ref @@ -0,0 +1,10 @@ +static foreach (thing; things) { + doStuff(); +} +static foreach_reverse (thing; things) { + doStuff(); +} +static foreach (thing; things) + doStuff(); +static foreach_reverse (thing; things) + doStuff(); From abc3e6d47e377839ae180a0a9597bbc7c61bd3a7 Mon Sep 17 00:00:00 2001 From: Kotet Date: Mon, 27 Nov 2017 07:32:05 +0900 Subject: [PATCH 2/4] Use std.algorithm.comparison.among instead of a lot of comparisons --- src/dfmt/indentation.d | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/dfmt/indentation.d b/src/dfmt/indentation.d index 01c14bd..f065945 100644 --- a/src/dfmt/indentation.d +++ b/src/dfmt/indentation.d @@ -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; @@ -196,9 +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" || arr[i + 1] == tok!"foreach" - || arr[i + 1] == tok!"foreach_reverse") && (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; From 8cd926538434c67f6b7fe7bef15d76bab357295e Mon Sep 17 00:00:00 2001 From: Kotet Date: Mon, 27 Nov 2017 07:37:09 +0900 Subject: [PATCH 3/4] Fix test --- tests/allman/issue0303.d.ref | 8 ++++---- tests/issue0303.d | 8 ++++---- tests/otbs/issue0303.d.ref | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/allman/issue0303.d.ref b/tests/allman/issue0303.d.ref index 9b974c5..807cc28 100644 --- a/tests/allman/issue0303.d.ref +++ b/tests/allman/issue0303.d.ref @@ -1,12 +1,12 @@ static foreach (thing; things) { - doStuff(); + pragma(msg, thing); } static foreach_reverse (thing; things) { - doStuff(); + pragma(msg, thing); } static foreach (thing; things) - doStuff(); + pragma(msg, thing); static foreach_reverse (thing; things) - doStuff(); + pragma(msg, thing); diff --git a/tests/issue0303.d b/tests/issue0303.d index 3a4d6b3..3fcab0e 100644 --- a/tests/issue0303.d +++ b/tests/issue0303.d @@ -1,4 +1,4 @@ -static foreach (thing; things){doStuff();} -static foreach_reverse (thing; things){doStuff();} -static foreach (thing; things) doStuff(); -static foreach_reverse (thing; things) doStuff(); \ No newline at end of file +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); \ No newline at end of file diff --git a/tests/otbs/issue0303.d.ref b/tests/otbs/issue0303.d.ref index 878435f..4f11f69 100644 --- a/tests/otbs/issue0303.d.ref +++ b/tests/otbs/issue0303.d.ref @@ -1,10 +1,10 @@ static foreach (thing; things) { - doStuff(); + pragma(msg, thing); } static foreach_reverse (thing; things) { - doStuff(); + pragma(msg, thing); } static foreach (thing; things) - doStuff(); + pragma(msg, thing); static foreach_reverse (thing; things) - doStuff(); + pragma(msg, thing); From 01a1716183e0773f8de216e37b7c2f581e3d759b Mon Sep 17 00:00:00 2001 From: Kotet Date: Tue, 28 Nov 2017 09:06:56 +0900 Subject: [PATCH 4/4] Update libdparse to v0.7.2-alpha.3 --- dub.json | 2 +- libdparse | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dub.json b/dub.json index d257652..0c3badc 100644 --- a/dub.json +++ b/dub.json @@ -4,6 +4,6 @@ "targetType": "autodetect", "license": "BSL-1.0", "dependencies": { - "libdparse": "~>0.7.1-beta.7" + "libdparse": "~>0.7.2-alpha.3" } } diff --git a/libdparse b/libdparse index 4229f11..a2b492d 160000 --- a/libdparse +++ b/libdparse @@ -1 +1 @@ -Subproject commit 4229f11828a901ea5379409015f14a033e742906 +Subproject commit a2b492d8c7a84881657de85939d9c6b14f867d5b