From 80b853113f117396fbf3b0bd48d8e323982ea479 Mon Sep 17 00:00:00 2001
From: Hackerpilot <briancschott@gmail.com>
Date: Fri, 13 Mar 2015 02:33:38 -0700
Subject: [PATCH] Fix #96

---
 libdparse             |  2 +-
 src/dfmt.d            | 41 ++++++++++++++++++++++++++++++++++++++---
 tests/issue0039.d.ref |  3 +--
 tests/issue0096.d     |  5 +++++
 tests/issue0096.d.ref | 17 +++++++++++++++++
 5 files changed, 62 insertions(+), 6 deletions(-)
 create mode 100644 tests/issue0096.d
 create mode 100644 tests/issue0096.d.ref

diff --git a/libdparse b/libdparse
index 9dc22fb..f20c701 160000
--- a/libdparse
+++ b/libdparse
@@ -1 +1 @@
-Subproject commit 9dc22fb7d7fa95130d787e4a3a96b5b970027f4f
+Subproject commit f20c701c96e6e4a7a16bea9d6a90c632c3d5b599
diff --git a/src/dfmt.d b/src/dfmt.d
index b9e5aea..5445441 100644
--- a/src/dfmt.d
+++ b/src/dfmt.d
@@ -306,9 +306,15 @@ private:
             write(" ");
             writeParens(true);
         }
-        else if (isBlockHeader() && peekIs(tok!"(", false))
+        else if ((isBlockHeader() || currentIs(tok!"version") || currentIs(tok!"debug"))
+            && peekIs(tok!"(", false))
         {
-            indents.push(current.type);
+            immutable bool shouldPushIndent = (!currentIs(tok!"version")
+                && !currentIs(tok!"debug")) || !assumeSorted(
+                    astInformation.conditionalWithElseLocations).equalRange(
+                    current.index).empty;
+            if (shouldPushIndent)
+                indents.push(current.type);
             writeToken();
             write(" ");
             writeParens(false);
@@ -316,6 +322,11 @@ private:
                 write(" ");
             else if (currentIs(tok!"comment"))
                 formatStep();
+            else if (!shouldPushIndent)
+            {
+                if (!currentIs(tok!"{") && !currentIs(tok!";"))
+                    write(" ");
+            }
             else if (!currentIs(tok!"{") && !currentIs(tok!";"))
                 newline();
         }
@@ -940,7 +951,7 @@ private:
         auto t = tokens[i + index].type;
         return t == tok!"for" || t == tok!"foreach"
             || t == tok!"foreach_reverse" || t == tok!"while"
-            || t == tok!"if" || t == tok!"out" || t == tok!"version"
+            || t == tok!"if" || t == tok!"out"
             || t == tok!"catch" || t == tok!"with";
     }
 
@@ -1177,6 +1188,7 @@ struct ASTInformation
         sort(structInitEndLocations);
         sort(funLitStartLocations);
         sort(funLitEndLocations);
+        sort(conditionalWithElseLocations);
     }
 
     /// Locations of end braces for struct bodies
@@ -1205,6 +1217,8 @@ struct ASTInformation
 
     /// Closing braces of function literals
     size_t[] funLitEndLocations;
+
+    size_t[] conditionalWithElseLocations;
 }
 
 /// Collects information from the AST that is useful for the formatter
@@ -1216,6 +1230,27 @@ final class FormatVisitor : ASTVisitor
         this.astInformation = astInformation;
     }
 
+    override void visit(const ConditionalDeclaration conditionalDeclaration)
+    {
+        if (conditionalDeclaration.falseDeclaration !is null)
+        {
+            auto condition = conditionalDeclaration.compileCondition;
+            if (condition.versionCondition !is null)
+            {
+                astInformation.conditionalWithElseLocations ~=
+                    condition.versionCondition.versionIndex;
+            }
+            else if (condition.debugCondition !is null)
+            {
+                astInformation.conditionalWithElseLocations ~=
+                    condition.debugCondition.debugIndex;
+            }
+            // Skip "static if" because the formatting for normal "if" handles
+            // it properly
+        }
+        conditionalDeclaration.accept(this);
+    }
+
     override void visit(const FunctionLiteralExpression funcLit)
     {
         astInformation.funLitStartLocations ~= funcLit.functionBody
diff --git a/tests/issue0039.d.ref b/tests/issue0039.d.ref
index 2e19d4a..206b331 100644
--- a/tests/issue0039.d.ref
+++ b/tests/issue0039.d.ref
@@ -1,2 +1 @@
-version (AArch64)
-    int x = 10;
+version (AArch64) int x = 10;
diff --git a/tests/issue0096.d b/tests/issue0096.d
new file mode 100644
index 0000000..b8e999f
--- /dev/null
+++ b/tests/issue0096.d
@@ -0,0 +1,5 @@
+version (Windows) void func();
+version (Windows) void func();
+else void func();
+version (Windows) {void func();}
+version (Windows) {void func();} else { void func(); }
diff --git a/tests/issue0096.d.ref b/tests/issue0096.d.ref
new file mode 100644
index 0000000..58637e5
--- /dev/null
+++ b/tests/issue0096.d.ref
@@ -0,0 +1,17 @@
+version (Windows) void func();
+version (Windows)
+    void func();
+else
+    void func();
+version (Windows)
+{
+    void func();
+}
+version (Windows)
+{
+    void func();
+}
+else
+{
+    void func();
+}