@@ -15,7 +15,7 @@ import dfmt.wrapping;
1515import std.array ;
1616
1717void format (OutputRange)(string source_desc, ubyte [] buffer, OutputRange output,
18- Config* formatterConfig)
18+ Config* formatterConfig)
1919{
2020 LexerConfig config;
2121 config.stringBehavior = StringBehavior.source;
@@ -33,7 +33,7 @@ void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
3333 auto tokens = byToken(buffer, config, &cache).array();
3434 auto depths = generateDepthInfo(tokens);
3535 auto tokenFormatter = TokenFormatter! OutputRange(buffer, tokens, depths,
36- output, &astInformation, formatterConfig);
36+ output, &astInformation, formatterConfig);
3737 tokenFormatter.format();
3838}
3939
@@ -79,7 +79,7 @@ struct TokenFormatter(OutputRange)
7979 * decisions.
8080 */
8181 this (const ubyte [] rawSource, const (Token )[] tokens, immutable short [] depths,
82- OutputRange output, ASTInformation* astInformation, Config* config)
82+ OutputRange output, ASTInformation* astInformation, Config* config)
8383 {
8484 this .rawSource = rawSource;
8585 this .tokens = tokens;
@@ -259,7 +259,7 @@ private:
259259 || isBasicType(current.type) || currentIs(tok! " @" )
260260 || currentIs(tok! " if" )
261261 || isNumberLiteral(tokens[index].type) || (inAsm
262- && peekBack2Is(tok! " ;" ) && currentIs(tok! " [" ))))
262+ && peekBack2Is(tok! " ;" ) && currentIs(tok! " [" ))))
263263 {
264264 write(" " );
265265 }
@@ -351,7 +351,7 @@ private:
351351 indents.pop();
352352 else if (peekBack2Is(tok! " ," ) && ! indents.topIs(tok! " ," )
353353 && indents.indentToMostRecent(tok! " enum" ) == - 1 )
354- indents.push (tok! " ," );
354+ pushWrapIndent (tok! " ," );
355355 newline();
356356 }
357357 }
@@ -387,7 +387,7 @@ private:
387387 break ;
388388 else if (t == tok! " import" && ! currentIs(tok! " import" )
389389 && ! currentIs(tok! " }" ) && ! (currentIs(tok! " public" )
390- && peekIs(tok! " import" ))
390+ && peekIs(tok! " import" ))
391391 && ! indents.topIsOneOf(tok! " if" , tok! " debug" , tok! " version" ))
392392 {
393393 simpleNewline();
@@ -440,11 +440,12 @@ private:
440440 }
441441 body
442442 {
443- immutable p = tokens[index] .type;
443+ immutable p = current .type;
444444 regenLineBreakHintsIfNecessary(index);
445445 writeToken();
446446 if (p == tok! " (" )
447447 {
448+ indents.push(p);
448449 spaceAfterParens = true ;
449450 parenDepth++ ;
450451 }
@@ -454,19 +455,18 @@ private:
454455 if (arrayInitializerStart)
455456 {
456457 // Use the close bracket as the indent token to distinguish
457- // the array initialiazer from an array index in the newling
458+ // the array initialiazer from an array index in the newline
458459 // handling code
459460 pushWrapIndent(tok! " ]" );
460461 newline();
461462 immutable size_t j = expressionEndIndex(index);
462463 linebreakHints = chooseLineBreakTokens(index, tokens[index .. j],
463- depths[index .. j], config, currentLineLength, indentLevel);
464+ depths[index .. j], config, currentLineLength, indentLevel);
464465 }
465466 else if (! currentIs(tok! " )" ) && ! currentIs(tok! " ]" )
466467 && (linebreakHints.canFindIndex(index - 1 )
467- || (linebreakHints.length == 0 && currentLineLength > config.max_line_length)))
468+ || (linebreakHints.length == 0 && currentLineLength > config.max_line_length)))
468469 {
469- pushWrapIndent(p);
470470 newline();
471471 }
472472 }
@@ -479,8 +479,9 @@ private:
479479 body
480480 {
481481 parenDepth-- ;
482- if (parenDepth == 0 )
483- indents.popWrapIndents();
482+ indents.popWrapIndents();
483+ if (indents.topIs(tok! " (" ))
484+ indents.pop();
484485
485486 if (parenDepth == 0 && (peekIs(tok! " is" ) || peekIs(tok! " in" )
486487 || peekIs(tok! " out" ) || peekIs(tok! " body" )))
@@ -505,17 +506,13 @@ private:
505506 immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
506507 if (l > config.dfmt_soft_max_line_length)
507508 {
508- // The order of these two calls is intentional
509509 newline();
510- pushWrapIndent(tok! " !" );
511510 }
512511 else
513512 write(" " );
514513 break ;
515514 case always_newline:
516- // The order of these two calls is intentional
517515 newline();
518- pushWrapIndent(tok! " !" );
519516 break ;
520517 case conditional_newline_indent:
521518 immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
@@ -688,7 +685,7 @@ private:
688685 a => tokenLength(a)).sum();
689686 immutable bool multiline = l > config.dfmt_soft_max_line_length
690687 || tokens[index .. e].canFind! (a => a.type == tok! " comment"
691- || isBlockHeaderToken(a.type))();
688+ || isBlockHeaderToken(a.type))();
692689 writeToken();
693690 if (multiline)
694691 {
@@ -801,6 +798,8 @@ private:
801798
802799 void formatBlockHeader ()
803800 {
801+ if (indents.topIs(tok! " !" ))
802+ indents.pop();
804803 immutable bool a = ! currentIs(tok! " version" ) && ! currentIs(tok! " debug" );
805804 immutable bool b = a
806805 || astInformation.conditionalWithElseLocations.canFindIndex(current.index);
@@ -1150,10 +1149,10 @@ private:
11501149 }
11511150 else if (! peekIs(tok! " }" ) && (linebreakHints.canFind(index)
11521151 || (linebreakHints.length == 0
1153- && currentLineLength > config.dfmt_soft_max_line_length)))
1152+ && currentLineLength > config.dfmt_soft_max_line_length)))
11541153 {
1154+ pushWrapIndent();
11551155 writeToken();
1156- pushWrapIndent(tok! " ," );
11571156 newline();
11581157 }
11591158 else
@@ -1172,7 +1171,7 @@ private:
11721171 {
11731172 immutable size_t j = expressionEndIndex(i);
11741173 linebreakHints = chooseLineBreakTokens(i, tokens[i .. j], depths[i .. j],
1175- config, currentLineLength, indentLevel);
1174+ config, currentLineLength, indentLevel);
11761175 }
11771176
11781177 void regenLineBreakHintsIfNecessary (immutable size_t i)
@@ -1271,6 +1270,12 @@ private:
12711270 indentLevel = config.dfmt_align_switch_statements
12721271 == OptionalBoolean.t ? l : indents.indentLevel;
12731272 }
1273+ else if (currentIs(tok! " )" ))
1274+ {
1275+ if (indents.topIs(tok! " (" ))
1276+ indents.pop();
1277+ indentLevel = indents.indentLevel;
1278+ }
12741279 else if (currentIs(tok! " {" ))
12751280 {
12761281 indents.popWrapIndents();
@@ -1292,7 +1297,7 @@ private:
12921297 }
12931298 while (sBraceDepth == 0 && indents.topIsTemp()
12941299 && ((indents.top != tok! " if"
1295- && indents.top != tok! " version" ) || ! peekIs(tok! " else" )))
1300+ && indents.top != tok! " version" ) || ! peekIs(tok! " else" )))
12961301 {
12971302 indents.pop();
12981303 }
@@ -1378,7 +1383,7 @@ private:
13781383 {
13791384 if (currentLineLength >= config.dfmt_soft_max_line_length)
13801385 {
1381- pushWrapIndent(tok ! " ; " );
1386+ pushWrapIndent();
13821387 writeToken();
13831388 newline();
13841389 }
0 commit comments