Skip to content

Commit

Permalink
Fix #64
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackerpilot committed Mar 9, 2015
1 parent 401631b commit 4590dd4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/dfmt.d
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ private:
writeToken();
newline();
}
else if (peekBackIs(tok!"identifier") && (peekBack2Is(tok!";")
|| peekBack2Is(tok!"}") || peekBack2Is(tok!"{")))
else if (peekBackIs(tok!"identifier") && (peekBack2Is(tok!";", true)
|| peekBack2Is(tok!"}", true) || peekBack2Is(tok!"{", true)))
{
if (tempIndent < 0)
tempIndent = 0;
Expand Down Expand Up @@ -483,9 +483,7 @@ private:
}
writeToken();
linebreakHints = [];
if (index >= tokens.length || !currentIs(tok!"comment")
|| current.line != tokens[index - 1].line)
newline();
newline();
break;
case tok!"{":
writeBraces();
Expand Down Expand Up @@ -1003,28 +1001,28 @@ private:
}
}

bool peekBackIs(IdType tokenType)
bool peekBackIs(IdType tokenType, bool ignoreComments = false)
{
return (index >= 1) && tokens[index - 1].type == tokenType;
return peekImplementation(tokenType, -1, ignoreComments);
}

bool peekBack2Is(IdType tokenType)
bool peekBack2Is(IdType tokenType, bool ignoreComments = false)
{
return (index >= 2) && tokens[index - 2].type == tokenType;
return peekImplementation(tokenType, -2, ignoreComments);
}

bool peekImplementation(IdType tokenType, size_t n, bool ignoreComments = true)
bool peekImplementation(IdType tokenType, int n, bool ignoreComments = true)
{
auto i = index + n;
if (ignoreComments)
while (i < tokens.length && tokens[i].type == tok!"comment")
i++;
while (n != 0 && i < tokens.length && tokens[i].type == tok!"comment")
i = n > 0 ? i + 1 : i - 1;
return i < tokens.length && tokens[i].type == tokenType;
}

bool peek2Is(IdType tokenType)
bool peek2Is(IdType tokenType, bool ignoreComments = true)
{
return peekImplementation(tokenType, 2);
return peekImplementation(tokenType, 2, ignoreComments);
}

bool peekIsOperator()
Expand Down Expand Up @@ -1073,6 +1071,9 @@ private:
{
import std.range : assumeSorted;

if (currentIs(tok!"comment") && current.line == tokenEndLine(tokens[index - 1]))
return;

output.put("\n");
immutable bool hasCurrent = index + 1 < tokens.length;
if (!justAddedExtraNewline && index > 0
Expand Down
20 changes: 20 additions & 0 deletions tests/issue0064.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
unittest
{
return true; //
Lnomatch:
//printf("nomatch\n");
return false; // nomatch;
}

unittest
{
if (x)
return true;
}

unittest
{
return true; // match
Lnomatch: //printf("nomatch\n");
return false; // nomatch;
}
20 changes: 20 additions & 0 deletions tests/issue0064.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
unittest
{
return true; //
Lnomatch:
//printf("nomatch\n");
return false; // nomatch;
}

unittest
{
if (x)
return true;
}

unittest
{
return true; // match
Lnomatch: //printf("nomatch\n");
return false; // nomatch;
}

0 comments on commit 4590dd4

Please sign in to comment.