This commit is contained in:
Hackerpilot 2015-03-07 14:37:30 -08:00
parent 2fa90ff3ba
commit 49633c8901
3 changed files with 84 additions and 16 deletions

View File

@ -475,8 +475,9 @@ private:
writeToken();
break;
case tok!",":
if (linebreakHints.canFind(index) || (linebreakHints.length == 0
&& currentLineLength > config.columnSoftLimit))
if (!peekIs(tok!"}") && (linebreakHints.canFind(index)
|| (linebreakHints.length == 0
&& currentLineLength > config.columnSoftLimit)))
{
writeToken();
pushIndent();
@ -485,8 +486,12 @@ private:
else
{
writeToken();
if (!currentIs(tok!")", false) && !currentIs(tok!"}", false)
&& !currentIs(tok!"]", false))
{
write(" ");
}
}
regenLineBreakHintsIfNecessary(index - 1);
break;
case tok!"=":
@ -592,19 +597,40 @@ private:
size_t expressionEndIndex(size_t i) const pure @safe @nogc
{
int parenDepth = 0;
loop : while (i < tokens.length)
switch (tokens[i].type)
int bracketDepth = 0;
int braceDepth = 0;
loop : while (i < tokens.length) switch (tokens[i].type)
{
case tok!"(":
parenDepth++;
i++;
break;
case tok!"{":
braceDepth++;
i++;
break;
case tok!"[":
bracketDepth++;
i++;
break;
case tok!")":
parenDepth--;
if (parenDepth <= 0)
break loop;
i++;
break;
case tok!"}":
braceDepth--;
if (braceDepth <= 0)
break loop;
i++;
break;
case tok!"]":
bracketDepth--;
if (bracketDepth <= 0)
break loop;
i++;
break;
case tok!";":
break loop;
default:
@ -657,7 +683,7 @@ private:
else
{
// Silly hack to format enums better.
if (peekBackIs(tok!"identifier") || peekBackIs(tok!","))
if (peekBackIsLiteralOrIdent() || peekBackIs(tok!","))
newline();
write("}");
depth--;
@ -861,6 +887,32 @@ private:
return tokens[index - 1];
}
bool peekBackIsLiteralOrIdent()
{
if (index == 0) 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;
@ -871,9 +923,10 @@ private:
return (index >= 2) && tokens[index - 2].type == tokenType;
}
bool peekImplementation(IdType tokenType, size_t n)
bool peekImplementation(IdType tokenType, size_t n, bool ignoreComments = true)
{
auto i = index + n;
if (ignoreComments)
while (i < tokens.length && tokens[i].type == tok!"comment")
i++;
return i < tokens.length && tokens[i].type == tokenType;
@ -889,14 +942,14 @@ private:
return index + 1 < tokens.length && isOperator(tokens[index + 1].type);
}
bool peekIs(IdType tokenType)
bool peekIs(IdType tokenType, bool ignoreComments = true)
{
return peekImplementation(tokenType, 1);
return peekImplementation(tokenType, 1, ignoreComments);
}
bool currentIs(IdType tokenType)
bool currentIs(IdType tokenType, bool ignoreComments = true)
{
return peekImplementation(tokenType, 0);
return peekImplementation(tokenType, 0, ignoreComments);
}
bool isBlockHeader(int i = 0)

9
tests/issue0052.d Normal file
View File

@ -0,0 +1,9 @@
enum Flags : int
{
IS_NOT_TOP_TYPE = 0x1,
MANGLE_RETURN_TYPE = 0x2,
IGNORE_CONST = 0x4,
IS_DMC = 0x8,
}
auto a = [b, c, d, ];

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

@ -0,0 +1,6 @@
enum Flags : int
{
IS_NOT_TOP_TYPE = 0x1, MANGLE_RETURN_TYPE = 0x2, IGNORE_CONST = 0x4, IS_DMC = 0x8,
}
auto a = [b, c, d,];