From 9374214fdf60fedbbae250ccaeae3565a29aeb3e Mon Sep 17 00:00:00 2001
From: Hackerpilot <briancschott@gmail.com>
Date: Sat, 7 Mar 2015 23:37:30 -0800
Subject: [PATCH] Fix #51

---
 src/dfmt.d            |  7 ++++---
 tests/issue0051.d     | 15 +++++++++++++++
 tests/issue0051.d.ref | 15 +++++++++++++++
 3 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 tests/issue0051.d
 create mode 100644 tests/issue0051.d.ref

diff --git a/src/dfmt.d b/src/dfmt.d
index f7c6d3d..0359721 100644
--- a/src/dfmt.d
+++ b/src/dfmt.d
@@ -316,7 +316,7 @@ private:
             writeToken();
             if (current.type == tok!"if" || (current.type == tok!"static" && peekIs(tok!"if")))
                 write(" ");
-            else if (current.type != tok!"{")
+            else if (current.type != tok!"{" && current.type != tok!"comment")
             {
                 pushIndent();
                 newline();
@@ -677,8 +677,10 @@ private:
                         && !peekBackIs(tok!"}") && !peekBackIs(tok!";"))
                     {
                         if (config.braceStyle == BraceStyle.otbs)
+                        {
                             write(" ");
-                        else
+                        }
+                        else if (!peekBackIs(tok!"comment") || tokens[index - 1].text[0 .. 2] != "//")
                             newline();
                     }
                     write("{");
@@ -711,7 +713,6 @@ private:
                     }
                     if (config.braceStyle == BraceStyle.otbs)
                     {
-                        index++;
                         if (index < tokens.length && current.type == tok!"else")
                             write(" ");
                     }
diff --git a/tests/issue0051.d b/tests/issue0051.d
new file mode 100644
index 0000000..d4000a5
--- /dev/null
+++ b/tests/issue0051.d
@@ -0,0 +1,15 @@
+void f()
+{
+	if (a)
+	{
+	}
+	else // wat
+	{
+		if (!is_temp_arg_ref)
+		{
+			if (global.params.isOSX)
+				buf.writeByte('_');
+		}
+	}
+	return;
+}
diff --git a/tests/issue0051.d.ref b/tests/issue0051.d.ref
new file mode 100644
index 0000000..2563e6c
--- /dev/null
+++ b/tests/issue0051.d.ref
@@ -0,0 +1,15 @@
+void f()
+{
+    if (a)
+    {
+    }
+    else // wat
+    {
+        if (!is_temp_arg_ref)
+        {
+            if (global.params.isOSX)
+                buf.writeByte('_');
+        }
+    }
+    return;
+}