From 908f32e433f0a0927f65f6e7b0b3843c26a35160 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20Tr=C3=A9guier?= <laurent@treguier.org>
Date: Wed, 10 Oct 2018 11:06:12 +0200
Subject: [PATCH] Properly recognize debug as block header

---
 src/dfmt/formatter.d               | 10 +++++-----
 tests/allman/debug-inside-if.d.ref |  8 ++++++++
 tests/debug-inside-if.d            |  6 ++++++
 tests/otbs/debug-inside-if.d.ref   |  6 ++++++
 4 files changed, 25 insertions(+), 5 deletions(-)
 create mode 100644 tests/allman/debug-inside-if.d.ref
 create mode 100644 tests/debug-inside-if.d
 create mode 100644 tests/otbs/debug-inside-if.d.ref

diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d
index b6b3baa..2102fc3 100644
--- a/src/dfmt/formatter.d
+++ b/src/dfmt/formatter.d
@@ -230,8 +230,8 @@ private:
             writeToken();
             write(" ");
         }
-        else if ((isBlockHeader() || currentIs(tok!"version")
-                || currentIs(tok!"debug")) && peekIs(tok!"(", false))
+        else if (((isBlockHeader() || currentIs(tok!"version")) && peekIs(tok!"("))
+                || (currentIs(tok!"debug") && peekIs(tok!"{")))
         {
             if (!assumeSorted(astInformation.constraintLocations).equalRange(current.index).empty)
                 formatConstraint();
@@ -1910,19 +1910,19 @@ const pure @safe @nogc:
         }
     }
 
-    bool isBlockHeaderToken(IdType t)
+    bool isBlockHeaderToken(const IdType t)
     {
         return t == tok!"for" || t == tok!"foreach" || t == tok!"foreach_reverse"
             || t == tok!"while" || t == tok!"if" || t == tok!"in"|| t == tok!"out"
             || t == tok!"do" || t == tok!"catch" || t == tok!"with"
-            || t == tok!"synchronized" || t == tok!"scope";
+            || t == tok!"synchronized" || t == tok!"scope" || t == tok!"debug";
     }
 
     bool isBlockHeader(int i = 0) nothrow
     {
         if (i + index < 0 || i + index >= tokens.length)
             return false;
-        auto t = tokens[i + index].type;
+        const t = tokens[i + index].type;
         bool isExpressionContract;
 
         if (i + index + 3 < tokens.length)
diff --git a/tests/allman/debug-inside-if.d.ref b/tests/allman/debug-inside-if.d.ref
new file mode 100644
index 0000000..f6f972c
--- /dev/null
+++ b/tests/allman/debug-inside-if.d.ref
@@ -0,0 +1,8 @@
+void main()
+{
+    if (true)
+        debug
+        {
+            foo();
+        }
+}
diff --git a/tests/debug-inside-if.d b/tests/debug-inside-if.d
new file mode 100644
index 0000000..acb0629
--- /dev/null
+++ b/tests/debug-inside-if.d
@@ -0,0 +1,6 @@
+void main()
+{
+if (true)
+debug
+{foo();}
+}
diff --git a/tests/otbs/debug-inside-if.d.ref b/tests/otbs/debug-inside-if.d.ref
new file mode 100644
index 0000000..854eecf
--- /dev/null
+++ b/tests/otbs/debug-inside-if.d.ref
@@ -0,0 +1,6 @@
+void main() {
+    if (true)
+        debug {
+            foo();
+        }
+}