Fix #50
This commit is contained in:
parent
5eef96e797
commit
7f0b3baecb
85
src/dfmt.d
85
src/dfmt.d
|
|
@ -292,13 +292,13 @@ private:
|
|||
}
|
||||
else if (isBlockHeader())
|
||||
{
|
||||
if (current.type == tok!"if")
|
||||
ifIndents ~= tempIndent;
|
||||
writeToken();
|
||||
write(" ");
|
||||
writeParens(false);
|
||||
if (isBlockHeader() || current.type == tok!"switch")
|
||||
{
|
||||
if (current.type == tok!"switch")
|
||||
write(" ");
|
||||
}
|
||||
else if (current.type == tok!"comment")
|
||||
{
|
||||
if (!peekIs(tok!"{") && !peekIs(tok!";"))
|
||||
|
|
@ -439,9 +439,12 @@ private:
|
|||
else if (peekBackIs(tok!"identifier") && (peekBack2Is(tok!";")
|
||||
|| peekBack2Is(tok!"}") || peekBack2Is(tok!"{")))
|
||||
{
|
||||
if (tempIndent < 0)
|
||||
tempIndent = 0;
|
||||
else
|
||||
popIndent();
|
||||
writeToken();
|
||||
if (isBlockHeader())
|
||||
if (isBlockHeader() && current.type != tok!"if")
|
||||
write(" ");
|
||||
else if (!currentIs(tok!"{"))
|
||||
newline();
|
||||
|
|
@ -449,7 +452,7 @@ private:
|
|||
else
|
||||
{
|
||||
write(" : ");
|
||||
index += 1;
|
||||
index++;
|
||||
}
|
||||
break;
|
||||
case tok!"]":
|
||||
|
|
@ -458,7 +461,16 @@ private:
|
|||
write(" ");
|
||||
break;
|
||||
case tok!";":
|
||||
if (peekIs(tok!"else"))
|
||||
{
|
||||
tempIndent = ifIndents[$ - 1];
|
||||
if (ifIndents.length)
|
||||
ifIndents = ifIndents[0 .. $ - 1];
|
||||
}
|
||||
else if (!peekIs(tok!"}"))
|
||||
tempIndent = 0;
|
||||
else
|
||||
popIndent();
|
||||
writeToken();
|
||||
linebreakHints = [];
|
||||
if (index >= tokens.length || current.type != tok!"comment"
|
||||
|
|
@ -472,6 +484,7 @@ private:
|
|||
if (linebreakHints.canFind(index) || (linebreakHints.length == 0
|
||||
&& currentLineLength + nextTokenLength() > config.columnHardLimit))
|
||||
{
|
||||
if (tempIndent < 2)
|
||||
pushIndent();
|
||||
newline();
|
||||
}
|
||||
|
|
@ -483,6 +496,7 @@ private:
|
|||
&& currentLineLength > config.columnSoftLimit)))
|
||||
{
|
||||
writeToken();
|
||||
if (tempIndent < 2)
|
||||
pushIndent();
|
||||
newline();
|
||||
}
|
||||
|
|
@ -546,6 +560,7 @@ private:
|
|||
binary:
|
||||
if (linebreakHints.canFind(index))
|
||||
{
|
||||
if (tempIndent < 2)
|
||||
pushIndent();
|
||||
newline();
|
||||
}
|
||||
|
|
@ -585,7 +600,6 @@ private:
|
|||
/// Pushes a temporary indent level
|
||||
void pushIndent()
|
||||
{
|
||||
if (tempIndent == 0)
|
||||
tempIndent++;
|
||||
}
|
||||
|
||||
|
|
@ -647,7 +661,6 @@ private:
|
|||
{
|
||||
import std.range : assumeSorted;
|
||||
int depth = 0;
|
||||
immutable l = indentLevel;
|
||||
do
|
||||
{
|
||||
if (current.type == tok!"{")
|
||||
|
|
@ -745,6 +758,7 @@ private:
|
|||
&& currentLineLength > config.columnSoftLimit
|
||||
&& current.type != tok!")"))
|
||||
{
|
||||
if (tempIndent < 2)
|
||||
pushIndent();
|
||||
newline();
|
||||
}
|
||||
|
|
@ -753,7 +767,7 @@ private:
|
|||
}
|
||||
else if (current.type == tok!")")
|
||||
{
|
||||
if (peekIs(tok!"identifier") || peekIsBasicType())
|
||||
if (peekIsLiteralOrIdent() || peekIsBasicType())
|
||||
{
|
||||
writeToken();
|
||||
if (space_afterwards)
|
||||
|
|
@ -831,9 +845,7 @@ private:
|
|||
newline();
|
||||
return;
|
||||
}
|
||||
else if (current.type == tok!";")
|
||||
{
|
||||
if (peekIs(tok!"}"))
|
||||
else if (current.type == tok!";" && peekIs(tok!"}"))
|
||||
{
|
||||
writeToken();
|
||||
newline();
|
||||
|
|
@ -842,9 +854,6 @@ private:
|
|||
newline();
|
||||
return;
|
||||
}
|
||||
else
|
||||
goto peek;
|
||||
}
|
||||
else if (current.type == tok!"case")
|
||||
{
|
||||
writeToken();
|
||||
|
|
@ -859,14 +868,14 @@ private:
|
|||
writeToken();
|
||||
write(" ");
|
||||
}
|
||||
else if (peekIs(tok!"identifier") && peek2Is(tok!":"))
|
||||
else if (peekIsLabel())
|
||||
{
|
||||
writeToken();
|
||||
indentLevel++;
|
||||
pushIndent();
|
||||
newline();
|
||||
writeToken();
|
||||
writeToken();
|
||||
indentLevel++;
|
||||
pushIndent();
|
||||
newline();
|
||||
}
|
||||
else
|
||||
|
|
@ -876,12 +885,12 @@ private:
|
|||
{
|
||||
peek:
|
||||
if (peekIs(tok!"case", false) || peekIs(tok!"default", false)
|
||||
|| peekIs(tok!"}", false) || peekIsLabel())
|
||||
|| peekIs(tok!"}", false))
|
||||
{
|
||||
indentLevel = l;
|
||||
formatStep();
|
||||
if (peekIsLabel())
|
||||
pushIndent();
|
||||
}
|
||||
else
|
||||
formatStep();
|
||||
}
|
||||
}
|
||||
|
|
@ -946,6 +955,32 @@ private:
|
|||
}
|
||||
}
|
||||
|
||||
bool peekIsLiteralOrIdent()
|
||||
{
|
||||
if (index + 1 >= tokens.length) return false;
|
||||
switch (tokens[index + 1].type)
|
||||
{
|
||||
case tok!"doubleLiteral":
|
||||
case tok!"floatLiteral":
|
||||
case tok!"idoubleLiteral":
|
||||
case tok!"ifloatLiteral":
|
||||
case tok!"intLiteral":
|
||||
case tok!"longLiteral":
|
||||
case tok!"realLiteral":
|
||||
case tok!"irealLiteral":
|
||||
case tok!"uintLiteral":
|
||||
case tok!"ulongLiteral":
|
||||
case tok!"characterLiteral":
|
||||
case tok!"identifier":
|
||||
case tok!"stringLiteral":
|
||||
case tok!"wstringLiteral":
|
||||
case tok!"dstringLiteral":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool peekBackIs(IdType tokenType)
|
||||
{
|
||||
return (index >= 1) && tokens[index - 1].type == tokenType;
|
||||
|
|
@ -999,6 +1034,7 @@ private:
|
|||
void newline()
|
||||
{
|
||||
import std.range : assumeSorted;
|
||||
|
||||
output.put("\n");
|
||||
immutable bool hasCurrent = index + 1 < tokens.length;
|
||||
if (!justAddedExtraNewline && index > 0
|
||||
|
|
@ -1012,10 +1048,9 @@ private:
|
|||
{
|
||||
if (current.type == tok!"}")
|
||||
indentLevel--;
|
||||
else if ((current.type == tok!"identifier" && peekIs(tok!":")
|
||||
&& !isBlockHeader(2))
|
||||
|| (!assumeSorted(astInformation.attributeDeclarationLines)
|
||||
.equalRange(current.line).empty))
|
||||
else if ((!assumeSorted(astInformation.attributeDeclarationLines)
|
||||
.equalRange(current.line).empty) || (current.type == tok!"identifier"
|
||||
&& peekIs(tok!":") && !isBlockHeader(2)))
|
||||
{
|
||||
tempIndent--;
|
||||
}
|
||||
|
|
@ -1080,6 +1115,8 @@ private:
|
|||
|
||||
size_t[] linebreakHints;
|
||||
|
||||
int[] ifIndents;
|
||||
|
||||
/// Configuration
|
||||
FormatterConfig* config;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ unittest
|
|||
case 1: case 2:
|
||||
label:doStuff();
|
||||
case 3:
|
||||
doOtherSTuff();
|
||||
doOtherStuff();
|
||||
goto label;
|
||||
default:
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ unittest
|
|||
label:
|
||||
doStuff();
|
||||
case 3:
|
||||
doOtherSTuff();
|
||||
doOtherStuff();
|
||||
goto label;
|
||||
default:
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
void fun()
|
||||
{
|
||||
if (something) foreach (_; 0 .. 100)
|
||||
if (true)
|
||||
{
|
||||
if (stuff)
|
||||
doStuff();
|
||||
else
|
||||
morestuff();
|
||||
}
|
||||
else
|
||||
doStuff();
|
||||
|
||||
cast(structalign_t) 1;
|
||||
for (*cost = 0; sc; sc = sc.enclosing, (*cost)++)
|
||||
if (sc.scopesym == scopesym)
|
||||
break;
|
||||
else
|
||||
a++;
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
void fun()
|
||||
{
|
||||
if (something)
|
||||
foreach (_; 0 .. 100)
|
||||
if (true)
|
||||
{
|
||||
if (stuff)
|
||||
doStuff();
|
||||
else
|
||||
morestuff();
|
||||
}
|
||||
else
|
||||
doStuff();
|
||||
|
||||
cast(structalign_t) 1;
|
||||
for (*cost = 0; sc; sc = sc.enclosing, (*cost)++)
|
||||
if (sc.scopesym == scopesym)
|
||||
break;
|
||||
else
|
||||
a++;
|
||||
}
|
||||
Loading…
Reference in New Issue