From 98d397cd22da83f75c957e4941ce177ae28d6c6d Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Thu, 12 Mar 2015 12:46:21 -0700 Subject: [PATCH] Fix #93 --- src/dfmt.d | 21 ++++++++++++--------- tests/issue0039.d.ref | 3 ++- tests/issue0093.d | 12 ++++++++++++ tests/issue0093.d.ref | 12 ++++++++++++ 4 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 tests/issue0093.d create mode 100644 tests/issue0093.d.ref diff --git a/src/dfmt.d b/src/dfmt.d index 921190de..a3179f9b 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -300,8 +300,7 @@ private: writeToken(); // switch write(" "); } - else if ((currentIs(tok!"version") || currentIs(tok!"extern")) - && peekIs(tok!"(")) + else if (currentIs(tok!"extern") && peekIs(tok!"(")) { writeToken(); write(" "); @@ -326,13 +325,13 @@ private: if (currentIs(tok!"if") || (currentIs(tok!"static") && peekIs(tok!"if")) || currentIs(tok!"version")) { - if (indents.top() == tok!"if") + if (indents.top() == tok!"if" || indents.top == tok!"version") indents.pop(); write(" "); } else if (!currentIs(tok!"{") && !currentIs(tok!"comment")) { - if (indents.top() == tok!"if") + if (indents.top() == tok!"if" || indents.top == tok!"version") indents.pop(); indents.push(tok!"else"); newline(); @@ -941,13 +940,14 @@ 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!"if" || t == tok!"out" || t == tok!"version" || t == tok!"catch" || t == tok!"with"; } void newline() { import std.range : assumeSorted; + import std.algorithm : max; if (currentIs(tok!"comment") && current.line == tokenEndLine(tokens[index - 1])) return; @@ -976,9 +976,11 @@ private: bool switchLabel = false; if (currentIs(tok!"else")) { - auto l = indents.indentToMostRecent(tok!"if"); - if (l != -1) - indentLevel = l; + auto i = indents.indentToMostRecent(tok!"if"); + auto v = indents.indentToMostRecent(tok!"version"); + auto mostRecent = max(i, v); + if (mostRecent != -1) + indentLevel = mostRecent; } else if (currentIs(tok!"identifier") && peekIs(tok!":")) { @@ -1037,7 +1039,8 @@ private: indents.pop(); } while (indents.length && isTempIndent(indents.top) - && (indents.top != tok!"if" || !peekIs(tok!"else"))) + && ((indents.top != tok!"if" && indents.top != tok!"version") + || !peekIs(tok!"else"))) { indents.pop(); } diff --git a/tests/issue0039.d.ref b/tests/issue0039.d.ref index 206b331c..2e19d4a3 100644 --- a/tests/issue0039.d.ref +++ b/tests/issue0039.d.ref @@ -1 +1,2 @@ -version (AArch64) int x = 10; +version (AArch64) + int x = 10; diff --git a/tests/issue0093.d b/tests/issue0093.d new file mode 100644 index 00000000..193ea293 --- /dev/null +++ b/tests/issue0093.d @@ -0,0 +1,12 @@ +unittest +{ + if (x) + { + version (none) + { + } + else + { + } + } +} diff --git a/tests/issue0093.d.ref b/tests/issue0093.d.ref new file mode 100644 index 00000000..193ea293 --- /dev/null +++ b/tests/issue0093.d.ref @@ -0,0 +1,12 @@ +unittest +{ + if (x) + { + version (none) + { + } + else + { + } + } +}