Skip to content

Commit 87c7006

Browse files
committed
Merge pull request #217 from Hackerpilot/indentation-madness
0.5.0 changes
2 parents 7fa11c8 + 6327175 commit 87c7006

37 files changed

+404
-279
lines changed

src/dfmt/ast_info.d

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ final class FormatVisitor : ASTVisitor
9696
auto condition = dec.compileCondition;
9797
if (condition.versionCondition !is null)
9898
{
99-
astInformation.conditionalWithElseLocations ~= condition.versionCondition.versionIndex;
99+
astInformation.conditionalWithElseLocations
100+
~= condition.versionCondition.versionIndex;
100101
}
101102
else if (condition.debugCondition !is null)
102103
{
@@ -151,8 +152,10 @@ final class FormatVisitor : ASTVisitor
151152
{
152153
if (functionBody.blockStatement !is null)
153154
astInformation.doubleNewlineLocations ~= functionBody.blockStatement.endLocation;
154-
if (functionBody.bodyStatement !is null && functionBody.bodyStatement.blockStatement !is null)
155-
astInformation.doubleNewlineLocations ~= functionBody.bodyStatement.blockStatement.endLocation;
155+
if (functionBody.bodyStatement !is null && functionBody.bodyStatement
156+
.blockStatement !is null)
157+
astInformation.doubleNewlineLocations
158+
~= functionBody.bodyStatement.blockStatement.endLocation;
156159
functionBody.accept(this);
157160
}
158161

@@ -203,8 +206,8 @@ final class FormatVisitor : ASTVisitor
203206
override void visit(const UnaryExpression unary)
204207
{
205208
if (unary.prefix.type == tok!"~" || unary.prefix.type == tok!"&"
206-
|| unary.prefix.type == tok!"*" || unary.prefix.type == tok!"+"
207-
|| unary.prefix.type == tok!"-")
209+
|| unary.prefix.type == tok!"*"
210+
|| unary.prefix.type == tok!"+" || unary.prefix.type == tok!"-")
208211
{
209212
astInformation.unaryLocations ~= unary.prefix.index;
210213
}

src/dfmt/config.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum BraceStyle
2121

2222
enum TemplateConstraintStyle
2323
{
24-
unspecified,
24+
unspecified,
2525
conditional_newline_indent,
2626
conditional_newline,
2727
always_newline,
@@ -54,7 +54,6 @@ struct Config
5454

5555
mixin StandardEditorConfigFields;
5656

57-
5857
/**
5958
* Initializes the standard EditorConfig properties with default values that
6059
* make sense for D code.
@@ -89,8 +88,9 @@ struct Config
8988

9089
if (dfmt_soft_max_line_length > max_line_length)
9190
{
92-
stderr.writefln("Column hard limit (%d) must be greater than or equal to column soft limit (%d)",
93-
max_line_length, dfmt_soft_max_line_length);
91+
stderr.writefln(
92+
"Column hard limit (%d) must be greater than or equal to column soft limit (%d)",
93+
max_line_length, dfmt_soft_max_line_length);
9494
return false;
9595
}
9696
return true;

src/dfmt/editorconfig.d

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ mixin template StandardEditorConfigFields()
7373
auto thisN = &mixin("this." ~ N);
7474
static if (N == "pattern")
7575
continue;
76-
else static if (is (T == enum))
76+
else static if (is(T == enum))
7777
*thisN = otherN != T.unspecified ? otherN : *thisN;
78-
else static if (is (T == int))
78+
else static if (is(T == int))
7979
*thisN = otherN != -1 ? otherN : *thisN;
80-
else static if (is (T == string))
80+
else static if (is(T == string))
8181
*thisN = otherN !is null ? otherN : *thisN;
8282
else
8383
static assert(false);
@@ -143,6 +143,7 @@ EC getConfigFor(EC)(string path)
143143
static if (__VERSION__ >= 2067)
144144
{
145145
import std.algorithm : each;
146+
146147
configs.each!(a => a.each!(b => result.merge(b, fileName)))();
147148
}
148149
else

src/dfmt/formatter.d

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import dfmt.wrapping;
1515
import std.array;
1616

1717
void 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

Comments
 (0)