Skip to content

Commit 98d397c

Browse files
committed
Fix #93
1 parent c3cb505 commit 98d397c

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

src/dfmt.d

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ private:
300300
writeToken(); // switch
301301
write(" ");
302302
}
303-
else if ((currentIs(tok!"version") || currentIs(tok!"extern"))
304-
&& peekIs(tok!"("))
303+
else if (currentIs(tok!"extern") && peekIs(tok!"("))
305304
{
306305
writeToken();
307306
write(" ");
@@ -326,13 +325,13 @@ private:
326325
if (currentIs(tok!"if") || (currentIs(tok!"static") && peekIs(tok!"if"))
327326
|| currentIs(tok!"version"))
328327
{
329-
if (indents.top() == tok!"if")
328+
if (indents.top() == tok!"if" || indents.top == tok!"version")
330329
indents.pop();
331330
write(" ");
332331
}
333332
else if (!currentIs(tok!"{") && !currentIs(tok!"comment"))
334333
{
335-
if (indents.top() == tok!"if")
334+
if (indents.top() == tok!"if" || indents.top == tok!"version")
336335
indents.pop();
337336
indents.push(tok!"else");
338337
newline();
@@ -941,13 +940,14 @@ private:
941940
auto t = tokens[i + index].type;
942941
return t == tok!"for" || t == tok!"foreach"
943942
|| t == tok!"foreach_reverse" || t == tok!"while"
944-
|| t == tok!"if" || t == tok!"out"
943+
|| t == tok!"if" || t == tok!"out" || t == tok!"version"
945944
|| t == tok!"catch" || t == tok!"with";
946945
}
947946

948947
void newline()
949948
{
950949
import std.range : assumeSorted;
950+
import std.algorithm : max;
951951

952952
if (currentIs(tok!"comment") && current.line == tokenEndLine(tokens[index - 1]))
953953
return;
@@ -976,9 +976,11 @@ private:
976976
bool switchLabel = false;
977977
if (currentIs(tok!"else"))
978978
{
979-
auto l = indents.indentToMostRecent(tok!"if");
980-
if (l != -1)
981-
indentLevel = l;
979+
auto i = indents.indentToMostRecent(tok!"if");
980+
auto v = indents.indentToMostRecent(tok!"version");
981+
auto mostRecent = max(i, v);
982+
if (mostRecent != -1)
983+
indentLevel = mostRecent;
982984
}
983985
else if (currentIs(tok!"identifier") && peekIs(tok!":"))
984986
{
@@ -1037,7 +1039,8 @@ private:
10371039
indents.pop();
10381040
}
10391041
while (indents.length && isTempIndent(indents.top)
1040-
&& (indents.top != tok!"if" || !peekIs(tok!"else")))
1042+
&& ((indents.top != tok!"if" && indents.top != tok!"version")
1043+
|| !peekIs(tok!"else")))
10411044
{
10421045
indents.pop();
10431046
}

tests/issue0039.d.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version (AArch64) int x = 10;
1+
version (AArch64)
2+
int x = 10;

tests/issue0093.d

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
unittest
2+
{
3+
if (x)
4+
{
5+
version (none)
6+
{
7+
}
8+
else
9+
{
10+
}
11+
}
12+
}

tests/issue0093.d.ref

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
unittest
2+
{
3+
if (x)
4+
{
5+
version (none)
6+
{
7+
}
8+
else
9+
{
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)