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())
|
else if (isBlockHeader())
|
||||||
{
|
{
|
||||||
|
if (current.type == tok!"if")
|
||||||
|
ifIndents ~= tempIndent;
|
||||||
writeToken();
|
writeToken();
|
||||||
write(" ");
|
write(" ");
|
||||||
writeParens(false);
|
writeParens(false);
|
||||||
if (isBlockHeader() || current.type == tok!"switch")
|
if (current.type == tok!"switch")
|
||||||
{
|
|
||||||
write(" ");
|
write(" ");
|
||||||
}
|
|
||||||
else if (current.type == tok!"comment")
|
else if (current.type == tok!"comment")
|
||||||
{
|
{
|
||||||
if (!peekIs(tok!"{") && !peekIs(tok!";"))
|
if (!peekIs(tok!"{") && !peekIs(tok!";"))
|
||||||
|
|
@ -439,9 +439,12 @@ private:
|
||||||
else if (peekBackIs(tok!"identifier") && (peekBack2Is(tok!";")
|
else if (peekBackIs(tok!"identifier") && (peekBack2Is(tok!";")
|
||||||
|| peekBack2Is(tok!"}") || peekBack2Is(tok!"{")))
|
|| peekBack2Is(tok!"}") || peekBack2Is(tok!"{")))
|
||||||
{
|
{
|
||||||
|
if (tempIndent < 0)
|
||||||
tempIndent = 0;
|
tempIndent = 0;
|
||||||
|
else
|
||||||
|
popIndent();
|
||||||
writeToken();
|
writeToken();
|
||||||
if (isBlockHeader())
|
if (isBlockHeader() && current.type != tok!"if")
|
||||||
write(" ");
|
write(" ");
|
||||||
else if (!currentIs(tok!"{"))
|
else if (!currentIs(tok!"{"))
|
||||||
newline();
|
newline();
|
||||||
|
|
@ -449,7 +452,7 @@ private:
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
write(" : ");
|
write(" : ");
|
||||||
index += 1;
|
index++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case tok!"]":
|
case tok!"]":
|
||||||
|
|
@ -458,7 +461,16 @@ private:
|
||||||
write(" ");
|
write(" ");
|
||||||
break;
|
break;
|
||||||
case tok!";":
|
case tok!";":
|
||||||
|
if (peekIs(tok!"else"))
|
||||||
|
{
|
||||||
|
tempIndent = ifIndents[$ - 1];
|
||||||
|
if (ifIndents.length)
|
||||||
|
ifIndents = ifIndents[0 .. $ - 1];
|
||||||
|
}
|
||||||
|
else if (!peekIs(tok!"}"))
|
||||||
tempIndent = 0;
|
tempIndent = 0;
|
||||||
|
else
|
||||||
|
popIndent();
|
||||||
writeToken();
|
writeToken();
|
||||||
linebreakHints = [];
|
linebreakHints = [];
|
||||||
if (index >= tokens.length || current.type != tok!"comment"
|
if (index >= tokens.length || current.type != tok!"comment"
|
||||||
|
|
@ -472,6 +484,7 @@ private:
|
||||||
if (linebreakHints.canFind(index) || (linebreakHints.length == 0
|
if (linebreakHints.canFind(index) || (linebreakHints.length == 0
|
||||||
&& currentLineLength + nextTokenLength() > config.columnHardLimit))
|
&& currentLineLength + nextTokenLength() > config.columnHardLimit))
|
||||||
{
|
{
|
||||||
|
if (tempIndent < 2)
|
||||||
pushIndent();
|
pushIndent();
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
|
|
@ -483,6 +496,7 @@ private:
|
||||||
&& currentLineLength > config.columnSoftLimit)))
|
&& currentLineLength > config.columnSoftLimit)))
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
|
if (tempIndent < 2)
|
||||||
pushIndent();
|
pushIndent();
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
|
|
@ -546,6 +560,7 @@ private:
|
||||||
binary:
|
binary:
|
||||||
if (linebreakHints.canFind(index))
|
if (linebreakHints.canFind(index))
|
||||||
{
|
{
|
||||||
|
if (tempIndent < 2)
|
||||||
pushIndent();
|
pushIndent();
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
|
|
@ -585,7 +600,6 @@ private:
|
||||||
/// Pushes a temporary indent level
|
/// Pushes a temporary indent level
|
||||||
void pushIndent()
|
void pushIndent()
|
||||||
{
|
{
|
||||||
if (tempIndent == 0)
|
|
||||||
tempIndent++;
|
tempIndent++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -647,7 +661,6 @@ private:
|
||||||
{
|
{
|
||||||
import std.range : assumeSorted;
|
import std.range : assumeSorted;
|
||||||
int depth = 0;
|
int depth = 0;
|
||||||
immutable l = indentLevel;
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (current.type == tok!"{")
|
if (current.type == tok!"{")
|
||||||
|
|
@ -745,6 +758,7 @@ private:
|
||||||
&& currentLineLength > config.columnSoftLimit
|
&& currentLineLength > config.columnSoftLimit
|
||||||
&& current.type != tok!")"))
|
&& current.type != tok!")"))
|
||||||
{
|
{
|
||||||
|
if (tempIndent < 2)
|
||||||
pushIndent();
|
pushIndent();
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
|
|
@ -753,7 +767,7 @@ private:
|
||||||
}
|
}
|
||||||
else if (current.type == tok!")")
|
else if (current.type == tok!")")
|
||||||
{
|
{
|
||||||
if (peekIs(tok!"identifier") || peekIsBasicType())
|
if (peekIsLiteralOrIdent() || peekIsBasicType())
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
if (space_afterwards)
|
if (space_afterwards)
|
||||||
|
|
@ -831,9 +845,7 @@ private:
|
||||||
newline();
|
newline();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (current.type == tok!";")
|
else if (current.type == tok!";" && peekIs(tok!"}"))
|
||||||
{
|
|
||||||
if (peekIs(tok!"}"))
|
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
newline();
|
newline();
|
||||||
|
|
@ -842,9 +854,6 @@ private:
|
||||||
newline();
|
newline();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
goto peek;
|
|
||||||
}
|
|
||||||
else if (current.type == tok!"case")
|
else if (current.type == tok!"case")
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
|
|
@ -859,14 +868,14 @@ private:
|
||||||
writeToken();
|
writeToken();
|
||||||
write(" ");
|
write(" ");
|
||||||
}
|
}
|
||||||
else if (peekIs(tok!"identifier") && peek2Is(tok!":"))
|
else if (peekIsLabel())
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
indentLevel++;
|
pushIndent();
|
||||||
newline();
|
newline();
|
||||||
writeToken();
|
writeToken();
|
||||||
writeToken();
|
writeToken();
|
||||||
indentLevel++;
|
pushIndent();
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -876,12 +885,12 @@ private:
|
||||||
{
|
{
|
||||||
peek:
|
peek:
|
||||||
if (peekIs(tok!"case", false) || peekIs(tok!"default", false)
|
if (peekIs(tok!"case", false) || peekIs(tok!"default", false)
|
||||||
|| peekIs(tok!"}", false) || peekIsLabel())
|
|| peekIs(tok!"}", false))
|
||||||
{
|
{
|
||||||
indentLevel = l;
|
indentLevel = l;
|
||||||
formatStep();
|
if (peekIsLabel())
|
||||||
|
pushIndent();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
formatStep();
|
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)
|
bool peekBackIs(IdType tokenType)
|
||||||
{
|
{
|
||||||
return (index >= 1) && tokens[index - 1].type == tokenType;
|
return (index >= 1) && tokens[index - 1].type == tokenType;
|
||||||
|
|
@ -999,6 +1034,7 @@ private:
|
||||||
void newline()
|
void newline()
|
||||||
{
|
{
|
||||||
import std.range : assumeSorted;
|
import std.range : assumeSorted;
|
||||||
|
|
||||||
output.put("\n");
|
output.put("\n");
|
||||||
immutable bool hasCurrent = index + 1 < tokens.length;
|
immutable bool hasCurrent = index + 1 < tokens.length;
|
||||||
if (!justAddedExtraNewline && index > 0
|
if (!justAddedExtraNewline && index > 0
|
||||||
|
|
@ -1012,10 +1048,9 @@ private:
|
||||||
{
|
{
|
||||||
if (current.type == tok!"}")
|
if (current.type == tok!"}")
|
||||||
indentLevel--;
|
indentLevel--;
|
||||||
else if ((current.type == tok!"identifier" && peekIs(tok!":")
|
else if ((!assumeSorted(astInformation.attributeDeclarationLines)
|
||||||
&& !isBlockHeader(2))
|
.equalRange(current.line).empty) || (current.type == tok!"identifier"
|
||||||
|| (!assumeSorted(astInformation.attributeDeclarationLines)
|
&& peekIs(tok!":") && !isBlockHeader(2)))
|
||||||
.equalRange(current.line).empty))
|
|
||||||
{
|
{
|
||||||
tempIndent--;
|
tempIndent--;
|
||||||
}
|
}
|
||||||
|
|
@ -1080,6 +1115,8 @@ private:
|
||||||
|
|
||||||
size_t[] linebreakHints;
|
size_t[] linebreakHints;
|
||||||
|
|
||||||
|
int[] ifIndents;
|
||||||
|
|
||||||
/// Configuration
|
/// Configuration
|
||||||
FormatterConfig* config;
|
FormatterConfig* config;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ unittest
|
||||||
case 1: case 2:
|
case 1: case 2:
|
||||||
label:doStuff();
|
label:doStuff();
|
||||||
case 3:
|
case 3:
|
||||||
doOtherSTuff();
|
doOtherStuff();
|
||||||
goto label;
|
goto label;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ unittest
|
||||||
label:
|
label:
|
||||||
doStuff();
|
doStuff();
|
||||||
case 3:
|
case 3:
|
||||||
doOtherSTuff();
|
doOtherStuff();
|
||||||
goto label;
|
goto label;
|
||||||
default:
|
default:
|
||||||
break;
|
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