Better line breaking for UFCS chains

This commit is contained in:
Hackerpilot 2016-01-13 04:52:32 -08:00
parent 9f81f0e13e
commit 8c3e151561
6 changed files with 54 additions and 10 deletions

View File

@ -47,14 +47,18 @@ immutable(short[]) generateDepthInfo(const Token[] tokens) pure nothrow @trusted
{ {
switch (t.type) switch (t.type)
{ {
case tok!"{":
case tok!"(":
case tok!"[": case tok!"[":
depth++;
goto case;
case tok!"{":
case tok!"(":
depth++; depth++;
break; break;
case tok!"]":
depth--;
goto case;
case tok!"}": case tok!"}":
case tok!")": case tok!")":
case tok!"]":
depth--; depth--;
break; break;
default: default:
@ -968,6 +972,7 @@ private:
formatRightBrace(); formatRightBrace();
break; break;
case tok!".": case tok!".":
regenLineBreakHintsIfNecessary(index);
if (linebreakHints.canFind(index) || (linebreakHints.length == 0 if (linebreakHints.canFind(index) || (linebreakHints.length == 0
&& currentLineLength + nextTokenLength() > config.max_line_length)) && currentLineLength + nextTokenLength() > config.max_line_length))
{ {

View File

@ -103,9 +103,9 @@ bool isBreakToken(IdType t) pure nothrow @safe @nogc
} }
} }
int breakCost(IdType t) pure nothrow @safe @nogc int breakCost(IdType p, IdType c) pure nothrow @safe @nogc
{ {
switch (t) switch (c)
{ {
case tok!"||": case tok!"||":
case tok!"&&": case tok!"&&":
@ -160,7 +160,7 @@ int breakCost(IdType t) pure nothrow @safe @nogc
case tok!"+=": case tok!"+=":
return 200; return 200;
case tok!".": case tok!".":
return 900; return p == tok!")" ? 0 : 900;
default: default:
return 1000; return 1000;
} }
@ -170,7 +170,7 @@ pure nothrow @safe @nogc unittest
{ {
foreach (ubyte u; 0 .. ubyte.max) foreach (ubyte u; 0 .. ubyte.max)
if (isBreakToken(u)) if (isBreakToken(u))
assert(breakCost(u) != 1000); assert(breakCost(tok!".", u) != 1000);
} }

View File

@ -40,13 +40,14 @@ struct State
} }
else else
{ {
for (size_t i = 0; i != uint.sizeof * 8; ++i) foreach (size_t i; 0 .. (uint.sizeof * 8))
{ {
if (((1 << i) & breaks) == 0) if (((1 << i) & breaks) == 0)
continue; continue;
immutable b = tokens[i].type; immutable prevType = i > 0 ? tokens[i - 1].type : tok!"";
immutable currentType = tokens[i].type;
immutable p = abs(depths[i]); immutable p = abs(depths[i]);
immutable bc = breakCost(b) * (p == 0 ? 1 : p * 2); immutable bc = breakCost(prevType, currentType) * (p == 0 ? 1 : p * 2);
this._cost += bc; this._cost += bc;
} }

View File

@ -0,0 +1,14 @@
unittest
{
{
{
foreach (abcde, def; abcdef.map!(battlecruiser => battlecruiser[123 .. 1231231])
.filter!(bravo => charlie[10] > 90000).sum())
{
}
abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234)
.abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234);
}
}
}

12
tests/breakOnDots.d Normal file
View File

@ -0,0 +1,12 @@
unittest
{
{
{
foreach (abcde, def; abcdef.map!(battlecruiser => battlecruiser[123 .. 1231231]).filter!(bravo => charlie[10] > 90000).sum())
{
}
abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234);
}
}
}

View File

@ -0,0 +1,12 @@
unittest {
{
{
foreach (abcde, def; abcdef.map!(battlecruiser => battlecruiser[123 .. 1231231])
.filter!(bravo => charlie[10] > 90000).sum()) {
}
abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234)
.abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234).abcdeabcdeabcde(12341234);
}
}
}