Skip to content

Commit 0b7bc36

Browse files
committed
Fix #99
1 parent 65ae9dd commit 0b7bc36

File tree

3 files changed

+147
-66
lines changed

3 files changed

+147
-66
lines changed

src/dfmt.d

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ private:
303303
{
304304
writeToken();
305305
write(" ");
306-
writeParens(true);
307306
}
308307
else if ((isBlockHeader() || currentIs(tok!"version") || currentIs(tok!"debug"))
309308
&& peekIs(tok!"(", false))
@@ -356,7 +355,6 @@ private:
356355
break;
357356
case tok!"cast":
358357
writeToken();
359-
writeParens(true);
360358
break;
361359
case tok!"in":
362360
case tok!"is":
@@ -433,8 +431,43 @@ private:
433431
}
434432
goto binary;
435433
case tok!"(":
436-
writeParens(true);
434+
spaceAfterParens = true;
435+
writeToken();
436+
parenDepth++;
437+
if (linebreakHints.canFindIndex(index - 1) || (linebreakHints.length == 0
438+
&& currentLineLength > config.columnSoftLimit && !currentIs(tok!")")))
439+
{
440+
indents.push(tok!"(");
441+
newline();
442+
}
443+
regenLineBreakHintsIfNecessary(index - 1);
437444
break;
445+
case tok!")":
446+
parenDepth--;
447+
if (parenDepth == 0)
448+
while (indents.length > 0 && isWrapIndent(indents.top))
449+
indents.pop();
450+
if (parenDepth == 0 && (peekIs(tok!"in") || peekIs(tok!"out")
451+
|| peekIs(tok!"body")))
452+
{
453+
writeToken(); // )
454+
newline();
455+
writeToken(); // in/out/body
456+
}
457+
else if (peekIsLiteralOrIdent() || peekIsBasicType() || peekIsKeyword())
458+
{
459+
writeToken();
460+
if (spaceAfterParens || parenDepth > 0)
461+
write(" ");
462+
}
463+
else if ((peekIsKeyword() || peekIs(tok!"@")) && spaceAfterParens)
464+
{
465+
writeToken();
466+
write(" ");
467+
}
468+
else
469+
writeToken();
470+
break;
438471
case tok!"!":
439472
if (peekIs(tok!"is"))
440473
write(" ");
@@ -487,9 +520,20 @@ private:
487520
write(" ");
488521
break;
489522
case tok!";":
490-
writeToken();
491-
linebreakHints = [];
492-
newline();
523+
if (parenDepth > 0)
524+
{
525+
if (!(peekIs(tok!";") || peekIs(tok!")") || peekIs(tok!"}")))
526+
write("; ");
527+
else
528+
write(";");
529+
index++;
530+
}
531+
else
532+
{
533+
writeToken();
534+
linebreakHints = [];
535+
newline();
536+
}
493537
break;
494538
case tok!"{":
495539
if (astInformation.structInitStartLocations.canFindIndex(
@@ -724,66 +768,21 @@ private:
724768
return i;
725769
}
726770

727-
void writeParens(bool space_afterwards)
728-
in
729-
{
730-
assert (currentIs(tok!"("), str(current.type));
731-
}
732-
body
733-
{
734-
int depth = 0;
735-
do
736-
{
737-
if (currentIs(tok!";"))
738-
{
739-
if (!(peekIs(tok!";") || peekIs(tok!")") || peekIs(tok!"}")))
740-
write("; ");
741-
else
742-
write(";");
743-
index++;
744-
}
745-
else if (currentIs(tok!"("))
746-
{
747-
writeToken();
748-
depth++;
749-
if (linebreakHints.canFindIndex(index - 1) || (linebreakHints.length == 0
750-
&& currentLineLength > config.columnSoftLimit && !currentIs(tok!")")))
751-
{
752-
indents.push(tok!"(");
753-
newline();
754-
}
755-
regenLineBreakHintsIfNecessary(index - 1);
756-
}
757-
else if (currentIs(tok!")"))
758-
{
759-
depth--;
760-
if (depth == 0 && (peekIs(tok!"in") || peekIs(tok!"out")
761-
|| peekIs(tok!"body")))
762-
{
763-
writeToken(); // )
764-
newline();
765-
writeToken(); // in/out/body
766-
}
767-
else if (peekIsLiteralOrIdent() || peekIsBasicType() || peekIsKeyword())
768-
{
769-
writeToken();
770-
if (space_afterwards || depth > 0)
771-
write(" ");
772-
}
773-
else if ((peekIsKeyword() || peekIs(tok!"@")) && space_afterwards)
774-
{
775-
writeToken();
776-
write(" ");
777-
}
778-
else
779-
writeToken();
780-
}
781-
else
782-
formatStep();
783-
}
784-
while (index < tokens.length && depth > 0);
785-
linebreakHints = [];
786-
}
771+
void writeParens(bool spaceAfter)
772+
in
773+
{
774+
assert(currentIs(tok!"("), str(current.type));
775+
}
776+
body
777+
{
778+
immutable int depth = parenDepth;
779+
do
780+
{
781+
formatStep();
782+
spaceAfterParens = spaceAfter;
783+
}
784+
while (index < tokens.length && parenDepth > depth);
785+
}
787786

788787
bool peekIsKeyword()
789788
{
@@ -1126,6 +1125,10 @@ private:
11261125
/// Keep track of whether or not an extra newline was just added because of
11271126
/// an import statement.
11281127
bool justAddedExtraNewline;
1128+
1129+
int parenDepth;
1130+
1131+
bool spaceAfterParens;
11291132
}
11301133

11311134
bool isWrapIndent(IdType type) pure nothrow @nogc @safe

tests/issue0099.d

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
unittest
2+
{
3+
if (a)
4+
{
5+
if (b)
6+
if (c)
7+
{
8+
if (excessivelyLongVariableName.longAttributeName && excessivelyLongVariableName.longAttributeName && excessivelyLongVariableName.longAttributeName)
9+
excessivelyLongFunctionName(true);
10+
else
11+
{
12+
excessivelyLongFunctionName(false);
13+
}
14+
}
15+
else
16+
a();
17+
}
18+
}
19+
20+
unittest
21+
{
22+
if (a)
23+
{
24+
if (b)
25+
{
26+
if (c)
27+
{
28+
if (excessivelyLongVariableName.longAttributeName && excessivelyLongVariableName.longAttributeName && excessivelyLongVariableName.longAttributeName)
29+
excessivelyLongFunctionName(true);
30+
else
31+
{
32+
excessivelyLongFunctionName(false);
33+
}
34+
}
35+
}
36+
}
37+
}

tests/issue0099.d.ref

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
unittest
2+
{
3+
if (a)
4+
{
5+
if (b)
6+
if (c)
7+
{
8+
if (excessivelyLongVariableName.longAttributeName
9+
&& excessivelyLongVariableName.longAttributeName
10+
&& excessivelyLongVariableName.longAttributeName)
11+
excessivelyLongFunctionName(true);
12+
else
13+
{
14+
excessivelyLongFunctionName(false);
15+
}
16+
}
17+
else
18+
a();
19+
}
20+
}
21+
22+
unittest
23+
{
24+
if (a)
25+
{
26+
if (b)
27+
{
28+
if (c)
29+
{
30+
if (excessivelyLongVariableName.longAttributeName
31+
&& excessivelyLongVariableName.longAttributeName
32+
&& excessivelyLongVariableName.longAttributeName)
33+
excessivelyLongFunctionName(true);
34+
else
35+
{
36+
excessivelyLongFunctionName(false);
37+
}
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)