diff --git a/src/dfmt.d b/src/dfmt.d index 2009fc86..5edd239b 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -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; @@ -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(); @@ -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() @@ -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 diff --git a/tests/issue0064.d b/tests/issue0064.d new file mode 100644 index 00000000..82ebda16 --- /dev/null +++ b/tests/issue0064.d @@ -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; +} diff --git a/tests/issue0064.d.ref b/tests/issue0064.d.ref new file mode 100644 index 00000000..0360128f --- /dev/null +++ b/tests/issue0064.d.ref @@ -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; +}