This commit is contained in:
Hackerpilot 2015-03-07 22:42:51 -08:00
parent 5eef96e797
commit 7f0b3baecb
6 changed files with 120 additions and 42 deletions

View File

@ -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!"{")))
{ {
tempIndent = 0; if (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!";":
tempIndent = 0; if (peekIs(tok!"else"))
{
tempIndent = ifIndents[$ - 1];
if (ifIndents.length)
ifIndents = ifIndents[0 .. $ - 1];
}
else if (!peekIs(tok!"}"))
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,7 +484,8 @@ private:
if (linebreakHints.canFind(index) || (linebreakHints.length == 0 if (linebreakHints.canFind(index) || (linebreakHints.length == 0
&& currentLineLength + nextTokenLength() > config.columnHardLimit)) && currentLineLength + nextTokenLength() > config.columnHardLimit))
{ {
pushIndent(); if (tempIndent < 2)
pushIndent();
newline(); newline();
} }
writeToken(); writeToken();
@ -483,7 +496,8 @@ private:
&& currentLineLength > config.columnSoftLimit))) && currentLineLength > config.columnSoftLimit)))
{ {
writeToken(); writeToken();
pushIndent(); if (tempIndent < 2)
pushIndent();
newline(); newline();
} }
else else
@ -546,7 +560,8 @@ private:
binary: binary:
if (linebreakHints.canFind(index)) if (linebreakHints.canFind(index))
{ {
pushIndent(); if (tempIndent < 2)
pushIndent();
newline(); newline();
} }
else else
@ -585,8 +600,7 @@ private:
/// Pushes a temporary indent level /// Pushes a temporary indent level
void pushIndent() void pushIndent()
{ {
if (tempIndent == 0) tempIndent++;
tempIndent++;
} }
/// Pops a temporary indent level /// Pops a temporary indent level
@ -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,7 +758,8 @@ private:
&& currentLineLength > config.columnSoftLimit && currentLineLength > config.columnSoftLimit
&& current.type != tok!")")) && current.type != tok!")"))
{ {
pushIndent(); if (tempIndent < 2)
pushIndent();
newline(); newline();
} }
regenLineBreakHintsIfNecessary(index - 1); regenLineBreakHintsIfNecessary(index - 1);
@ -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,19 +845,14 @@ private:
newline(); newline();
return; return;
} }
else if (current.type == tok!";") else if (current.type == tok!";" && peekIs(tok!"}"))
{ {
if (peekIs(tok!"}")) writeToken();
{ newline();
writeToken(); indentLevel = l;
newline(); writeToken();
indentLevel = l; newline();
writeToken(); return;
newline();
return;
}
else
goto peek;
} }
else if (current.type == tok!"case") else if (current.type == tok!"case")
{ {
@ -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,13 +885,13 @@ 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();
} }
} }
indentLevel = l; indentLevel = l;
@ -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;
@ -998,7 +1033,8 @@ 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;

View File

@ -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;

View File

@ -7,7 +7,7 @@ unittest
label: label:
doStuff(); doStuff();
case 3: case 3:
doOtherSTuff(); doOtherStuff();
goto label; goto label;
default: default:
break; break;

20
tests/issue0050.d Normal file
View File

@ -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++;
}

21
tests/issue0050.d.ref Normal file
View File

@ -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++;
}

View File

@ -4,6 +4,6 @@ void main(string[] args)
{ {
addErrorMessage(line, column, KEY, addErrorMessage(line, column, KEY,
"Expression %s is true: already checked on line %d.".format( "Expression %s is true: already checked on line %d.".format(
expressions[prevLocation].formatted, expressions[prevLocation].line)); expressions[prevLocation].formatted, expressions[prevLocation].line));
} }
} }