This commit is contained in:
Hackerpilot 2015-03-08 18:30:46 -07:00
parent d5eed6a9b0
commit 9b6fbfda20
5 changed files with 71 additions and 46 deletions

View File

@ -175,7 +175,7 @@ private:
import std.algorithm : canFind, startsWith; import std.algorithm : canFind, startsWith;
assert (index < tokens.length); assert (index < tokens.length);
if (current.type == tok!"comment") if (currentIs(tok!"comment"))
{ {
if (index > 0) if (index > 0)
{ {
@ -202,22 +202,22 @@ private:
if (tokens[index].type != tok!"{") if (tokens[index].type != tok!"{")
write(" "); write(" ");
} }
else if (current.type != tok!"{") else if (!currentIs(tok!"{"))
newline(); newline();
} }
else else
newline(); newline();
} }
else if (isStringLiteral(current.type) || isNumberLiteral(current.type) else if (isStringLiteral(current.type) || isNumberLiteral(current.type)
|| current.type == tok!"characterLiteral") || currentIs(tok!"characterLiteral"))
{ {
writeToken(); writeToken();
} }
else if (current.type == tok!"module" || current.type == tok!"import") else if (currentIs(tok!"module") || currentIs(tok!"import"))
{ {
auto t = current.type; auto t = current.type;
writeToken(); writeToken();
if (current.type == tok!"(") if (currentIs(tok!"("))
{ {
writeParens(false); writeParens(false);
return; return;
@ -225,7 +225,7 @@ private:
write(" "); write(" ");
while (index < tokens.length) while (index < tokens.length)
{ {
if (current.type == tok!";") if (currentIs(tok!";"))
{ {
writeToken(); writeToken();
tempIndent = 0; tempIndent = 0;
@ -234,12 +234,12 @@ private:
newline(); newline();
break; break;
} }
if (current.type == tok!"comment" && current.line == peekBack().line) if (currentIs(tok!"comment") && current.line == peekBack().line)
{ {
justAddedExtraNewline = true; justAddedExtraNewline = true;
break; break;
} }
else if ((t == tok!"import" && current.type != tok!"import")) else if ((t == tok!"import" && !currentIs(tok!"import")))
{ {
write("\n"); write("\n");
justAddedExtraNewline = true; justAddedExtraNewline = true;
@ -249,7 +249,7 @@ private:
newline(); newline();
break; break;
} }
else if (current.type == tok!",") else if (currentIs(tok!","))
{ {
// compute length until next , or ; // compute length until next , or ;
int length_of_next_chunk = INVALID_TOKEN_LENGTH; int length_of_next_chunk = INVALID_TOKEN_LENGTH;
@ -275,15 +275,15 @@ private:
formatStep(); formatStep();
} }
} }
else if (current.type == tok!"return") else if (currentIs(tok!"return"))
{ {
writeToken(); writeToken();
if (current.type != tok!";") if (!currentIs(tok!";"))
write(" "); write(" ");
} }
else if (current.type == tok!"switch") else if (currentIs(tok!"switch"))
formatSwitch(); formatSwitch();
else if ((current.type == tok!"version" || current.type == tok!"extern") else if ((currentIs(tok!"version") || currentIs(tok!"extern"))
&& peekIs(tok!"(")) && peekIs(tok!"("))
{ {
writeToken(); writeToken();
@ -292,31 +292,34 @@ private:
} }
else if (isBlockHeader() && peekIs(tok!"(", false)) else if (isBlockHeader() && peekIs(tok!"(", false))
{ {
if (current.type == tok!"if") if (currentIs(tok!"if"))
ifIndents.push(tempIndent); ifIndents.push(tempIndent);
writeToken(); writeToken();
write(" "); write(" ");
writeParens(false); writeParens(false);
if (current.type == tok!"switch") if (currentIs(tok!"switch"))
write(" "); write(" ");
else if (current.type == tok!"comment") else if (currentIs(tok!"comment"))
{ {
if (!peekIs(tok!"{") && !peekIs(tok!";")) if (!peekIs(tok!"{") && !peekIs(tok!";"))
pushIndent(); pushIndent();
formatStep(); formatStep();
} }
else if (current.type != tok!"{" && current.type != tok!";") else if (!currentIs(tok!"{") && !currentIs(tok!";"))
{ {
pushIndent(); pushIndent();
newline(); newline();
} }
} }
else if (current.type == tok!"else") else if (currentIs(tok!"else"))
{ {
writeToken(); writeToken();
if (current.type == tok!"if" || (current.type == tok!"static" && peekIs(tok!"if"))) if (currentIs(tok!"if") || (currentIs(tok!"static") && peekIs(tok!"if"))
|| currentIs(tok!"version"))
{
write(" "); write(" ");
else if (current.type != tok!"{" && current.type != tok!"comment") }
else if (!currentIs(tok!"{") && !currentIs(tok!"comment"))
{ {
pushIndent(); pushIndent();
newline(); newline();
@ -359,7 +362,7 @@ private:
else if (isBasicType(current.type)) else if (isBasicType(current.type))
{ {
writeToken(); writeToken();
if (current.type == tok!"identifier" || isKeyword(current.type)) if (currentIs(tok!"identifier") || isKeyword(current.type))
write(" "); write(" ");
} }
else if (isOperator(current.type)) else if (isOperator(current.type))
@ -371,7 +374,7 @@ private:
.equalRange(current.index).empty) .equalRange(current.index).empty)
{ {
writeToken(); writeToken();
if (current.type != tok!"*" && current.type != tok!")" && current.type != tok!"[") if (!currentIs(tok!"*") && !currentIs(tok!")") && !currentIs(tok!"["))
write(" "); write(" ");
break; break;
} }
@ -445,7 +448,7 @@ private:
else else
popIndent(); popIndent();
writeToken(); writeToken();
if (isBlockHeader() && current.type != tok!"if") if (isBlockHeader() && !currentIs(tok!"if"))
write(" "); write(" ");
else if (!currentIs(tok!"{")) else if (!currentIs(tok!"{"))
newline(); newline();
@ -458,7 +461,7 @@ private:
break; break;
case tok!"]": case tok!"]":
writeToken(); writeToken();
if (current.type == tok!"identifier") if (currentIs(tok!"identifier"))
write(" "); write(" ");
break; break;
case tok!";": case tok!";":
@ -477,7 +480,7 @@ private:
} }
writeToken(); writeToken();
linebreakHints = []; linebreakHints = [];
if (index >= tokens.length || current.type != tok!"comment" if (index >= tokens.length || !currentIs(tok!"comment")
|| current.line != tokens[index - 1].line) || current.line != tokens[index - 1].line)
newline(); newline();
break; break;
@ -580,12 +583,12 @@ private:
assert (false, str(current.type)); assert (false, str(current.type));
} }
} }
else if (current.type == tok!"identifier") else if (currentIs(tok!"identifier"))
{ {
writeToken(); writeToken();
if (index < tokens.length && (current.type == tok!"identifier" if (index < tokens.length && (currentIs(tok!"identifier")
|| isKeyword(current.type) || isBasicType(current.type) || isKeyword(current.type) || isBasicType(current.type)
|| current.type == tok!"@")) || currentIs(tok!"@")))
{ {
write(" "); write(" ");
} }
@ -675,7 +678,7 @@ private:
int depth = 0; int depth = 0;
do do
{ {
if (current.type == tok!"{") if (currentIs(tok!"{"))
{ {
braceIndents.push(tempIndent); braceIndents.push(tempIndent);
depth++; depth++;
@ -702,7 +705,7 @@ private:
newline(); newline();
} }
} }
else if (current.type == tok!"}") else if (currentIs(tok!"}"))
{ {
braceIndents.pop(); braceIndents.pop();
depth--; depth--;
@ -726,7 +729,7 @@ private:
} }
if (config.braceStyle == BraceStyle.otbs) if (config.braceStyle == BraceStyle.otbs)
{ {
if (index < tokens.length && current.type == tok!"else") if (index < tokens.length && currentIs(tok!"else"))
write(" "); write(" ");
} }
index++; index++;
@ -744,7 +747,7 @@ private:
void writeParens(bool space_afterwards) void writeParens(bool space_afterwards)
in in
{ {
assert (current.type == tok!"(", str(current.type)); assert (currentIs(tok!"("), str(current.type));
} }
body body
{ {
@ -754,7 +757,7 @@ private:
int depth = 0; int depth = 0;
do do
{ {
if (current.type == tok!";") if (currentIs(tok!";"))
{ {
if (!(peekIs(tok!";") || peekIs(tok!")"))) if (!(peekIs(tok!";") || peekIs(tok!")")))
write("; "); write("; ");
@ -763,14 +766,14 @@ private:
index++; index++;
continue; continue;
} }
else if (current.type == tok!"(") else if (currentIs(tok!"("))
{ {
writeToken(); writeToken();
depth++; depth++;
if (!assumeSorted(linebreakHints).equalRange(index - 1).empty if (!assumeSorted(linebreakHints).equalRange(index - 1).empty
|| (linebreakHints.length == 0 || (linebreakHints.length == 0
&& currentLineLength > config.columnSoftLimit && currentLineLength > config.columnSoftLimit
&& current.type != tok!")")) && !currentIs(tok!")")))
{ {
if (tempIndent < 2) if (tempIndent < 2)
pushIndent(); pushIndent();
@ -779,7 +782,7 @@ private:
regenLineBreakHintsIfNecessary(index - 1); regenLineBreakHintsIfNecessary(index - 1);
continue; continue;
} }
else if (current.type == tok!")") else if (currentIs(tok!")"))
{ {
depth--; depth--;
if (depth == 0 && (peekIs(tok!"in") || peekIs(tok!"out") if (depth == 0 && (peekIs(tok!"in") || peekIs(tok!"out")
@ -833,13 +836,13 @@ private:
writeToken(); // switch writeToken(); // switch
write(" "); write(" ");
writeParens(true); writeParens(true);
if (current.type == tok!"with") if (currentIs(tok!"with"))
{ {
writeToken(); writeToken();
write(" "); write(" ");
writeParens(true); writeParens(true);
} }
if (current.type != tok!"{") if (!currentIs(tok!"{"))
return; return;
if (config.braceStyle == BraceStyle.otbs) if (config.braceStyle == BraceStyle.otbs)
write(" "); write(" ");
@ -851,7 +854,7 @@ private:
newline(); newline();
while (index < tokens.length) while (index < tokens.length)
{ {
if (current.type == tok!"}") if (currentIs(tok!"}"))
{ {
indentLevel = l; indentLevel = l;
indent(); indent();
@ -859,7 +862,7 @@ private:
newline(); newline();
return; return;
} }
else if (current.type == tok!";" && peekIs(tok!"}", false)) else if (currentIs(tok!";") && peekIs(tok!"}", false))
{ {
writeToken(); writeToken();
newline(); newline();
@ -868,12 +871,12 @@ private:
newline(); newline();
return; return;
} }
else if (current.type == tok!"case") else if (currentIs(tok!"case"))
{ {
writeToken(); writeToken();
write(" "); write(" ");
} }
else if (current.type == tok!":") else if (currentIs(tok!":"))
{ {
if (peekIs(tok!"..")) if (peekIs(tok!".."))
{ {
@ -911,7 +914,7 @@ private:
} }
} }
indentLevel = l; indentLevel = l;
assert (current.type == tok!"}"); assert (currentIs(tok!"}"));
writeToken(); writeToken();
newline(); newline();
} }
@ -1031,7 +1034,7 @@ private:
return peekImplementation(tokenType, 1, ignoreComments); return peekImplementation(tokenType, 1, ignoreComments);
} }
bool currentIs(IdType tokenType, bool ignoreComments = true) bool currentIs(IdType tokenType, bool ignoreComments = false)
{ {
return peekImplementation(tokenType, 0, ignoreComments); return peekImplementation(tokenType, 0, ignoreComments);
} }
@ -1078,13 +1081,13 @@ private:
currentLineLength = 0; currentLineLength = 0;
if (hasCurrent) if (hasCurrent)
{ {
if (current.type == tok!"}") if (currentIs(tok!"}"))
{ {
tempIndent = braceIndents.top(); tempIndent = braceIndents.top();
indentLevel--; indentLevel--;
} }
else if ((!assumeSorted(astInformation.attributeDeclarationLines) else if ((!assumeSorted(astInformation.attributeDeclarationLines)
.equalRange(current.line).empty) || (current.type == tok!"identifier" .equalRange(current.line).empty) || (currentIs(tok!"identifier")
&& peekIs(tok!":") && !isBlockHeader(2))) && peekIs(tok!":") && !isBlockHeader(2)))
{ {
tempIndent--; tempIndent--;

6
tests/issue0068.d Normal file
View File

@ -0,0 +1,6 @@
version (all)
{
}
else version (none)
{
}

6
tests/issue0068.d.ref Normal file
View File

@ -0,0 +1,6 @@
version (all)
{
}
else version (none)
{
}

9
tests/issue0070.d.ref Normal file
View File

@ -0,0 +1,9 @@
unittest
{
if (0)
if (0)
{
something();
something_else();
}
}

View File

@ -3,6 +3,7 @@ set -e
for source in *.d for source in *.d
do do
echo "${source}.ref" "${source}.out"
../bin/dfmt "${source}" >"${source}.out" ../bin/dfmt "${source}" >"${source}.out"
diff -u "${source}.ref" "${source}.out" diff -u "${source}.ref" "${source}.out"
done done