From 7f0b3baecbad8d515de06a661c0226c1e3e7825d Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Sat, 7 Mar 2015 22:42:51 -0800 Subject: [PATCH] Fix #50 --- src/dfmt.d | 115 ++++++++++++++++++++++++++++-------------- tests/issue0043.d | 2 +- tests/issue0043.d.ref | 2 +- tests/issue0050.d | 20 ++++++++ tests/issue0050.d.ref | 21 ++++++++ tests/wrapping1.d.ref | 2 +- 6 files changed, 120 insertions(+), 42 deletions(-) create mode 100644 tests/issue0050.d create mode 100644 tests/issue0050.d.ref diff --git a/src/dfmt.d b/src/dfmt.d index 0611ac9..6bb2e47 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -292,13 +292,13 @@ private: } else if (isBlockHeader()) { + if (current.type == tok!"if") + ifIndents ~= tempIndent; writeToken(); write(" "); writeParens(false); - if (isBlockHeader() || current.type == tok!"switch") - { + if (current.type == tok!"switch") write(" "); - } else if (current.type == tok!"comment") { if (!peekIs(tok!"{") && !peekIs(tok!";")) @@ -439,9 +439,12 @@ private: else if (peekBackIs(tok!"identifier") && (peekBack2Is(tok!";") || peekBack2Is(tok!"}") || peekBack2Is(tok!"{"))) { - tempIndent = 0; + if (tempIndent < 0) + tempIndent = 0; + else + popIndent(); writeToken(); - if (isBlockHeader()) + if (isBlockHeader() && current.type != tok!"if") write(" "); else if (!currentIs(tok!"{")) newline(); @@ -449,7 +452,7 @@ private: else { write(" : "); - index += 1; + index++; } break; case tok!"]": @@ -458,7 +461,16 @@ private: write(" "); break; case tok!";": - tempIndent = 0; + if (peekIs(tok!"else")) + { + tempIndent = ifIndents[$ - 1]; + if (ifIndents.length) + ifIndents = ifIndents[0 .. $ - 1]; + } + else if (!peekIs(tok!"}")) + tempIndent = 0; + else + popIndent(); writeToken(); linebreakHints = []; if (index >= tokens.length || current.type != tok!"comment" @@ -472,7 +484,8 @@ private: if (linebreakHints.canFind(index) || (linebreakHints.length == 0 && currentLineLength + nextTokenLength() > config.columnHardLimit)) { - pushIndent(); + if (tempIndent < 2) + pushIndent(); newline(); } writeToken(); @@ -483,7 +496,8 @@ private: && currentLineLength > config.columnSoftLimit))) { writeToken(); - pushIndent(); + if (tempIndent < 2) + pushIndent(); newline(); } else @@ -546,7 +560,8 @@ private: binary: if (linebreakHints.canFind(index)) { - pushIndent(); + if (tempIndent < 2) + pushIndent(); newline(); } else @@ -585,8 +600,7 @@ private: /// Pushes a temporary indent level void pushIndent() { - if (tempIndent == 0) - tempIndent++; + tempIndent++; } /// Pops a temporary indent level @@ -647,7 +661,6 @@ private: { import std.range : assumeSorted; int depth = 0; - immutable l = indentLevel; do { if (current.type == tok!"{") @@ -745,7 +758,8 @@ private: && currentLineLength > config.columnSoftLimit && current.type != tok!")")) { - pushIndent(); + if (tempIndent < 2) + pushIndent(); newline(); } regenLineBreakHintsIfNecessary(index - 1); @@ -753,7 +767,7 @@ private: } else if (current.type == tok!")") { - if (peekIs(tok!"identifier") || peekIsBasicType()) + if (peekIsLiteralOrIdent() || peekIsBasicType()) { writeToken(); if (space_afterwards) @@ -831,19 +845,14 @@ private: newline(); return; } - else if (current.type == tok!";") + else if (current.type == tok!";" && peekIs(tok!"}")) { - if (peekIs(tok!"}")) - { - writeToken(); - newline(); - indentLevel = l; - writeToken(); - newline(); - return; - } - else - goto peek; + writeToken(); + newline(); + indentLevel = l; + writeToken(); + newline(); + return; } else if (current.type == tok!"case") { @@ -859,14 +868,14 @@ private: writeToken(); write(" "); } - else if (peekIs(tok!"identifier") && peek2Is(tok!":")) + else if (peekIsLabel()) { writeToken(); - indentLevel++; + pushIndent(); newline(); writeToken(); writeToken(); - indentLevel++; + pushIndent(); newline(); } else @@ -876,13 +885,13 @@ private: { peek: if (peekIs(tok!"case", false) || peekIs(tok!"default", false) - || peekIs(tok!"}", false) || peekIsLabel()) + || peekIs(tok!"}", false)) { indentLevel = l; - formatStep(); + if (peekIsLabel()) + pushIndent(); } - else - formatStep(); + formatStep(); } } indentLevel = l; @@ -946,6 +955,32 @@ private: } } + bool peekIsLiteralOrIdent() + { + if (index + 1 >= tokens.length) 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; @@ -998,7 +1033,8 @@ private: void newline() { - import std.range:assumeSorted; + import std.range : assumeSorted; + output.put("\n"); immutable bool hasCurrent = index + 1 < tokens.length; if (!justAddedExtraNewline && index > 0 @@ -1012,10 +1048,9 @@ private: { if (current.type == tok!"}") indentLevel--; - else if ((current.type == tok!"identifier" && peekIs(tok!":") - && !isBlockHeader(2)) - || (!assumeSorted(astInformation.attributeDeclarationLines) - .equalRange(current.line).empty)) + else if ((!assumeSorted(astInformation.attributeDeclarationLines) + .equalRange(current.line).empty) || (current.type == tok!"identifier" + && peekIs(tok!":") && !isBlockHeader(2))) { tempIndent--; } @@ -1080,6 +1115,8 @@ private: size_t[] linebreakHints; + int[] ifIndents; + /// Configuration FormatterConfig* config; diff --git a/tests/issue0043.d b/tests/issue0043.d index df9f8ad..a954df7 100644 --- a/tests/issue0043.d +++ b/tests/issue0043.d @@ -4,7 +4,7 @@ unittest case 1: case 2: label:doStuff(); case 3: - doOtherSTuff(); + doOtherStuff(); goto label; default: break; diff --git a/tests/issue0043.d.ref b/tests/issue0043.d.ref index 4f171c4..12ab96e 100644 --- a/tests/issue0043.d.ref +++ b/tests/issue0043.d.ref @@ -7,7 +7,7 @@ unittest label: doStuff(); case 3: - doOtherSTuff(); + doOtherStuff(); goto label; default: break; diff --git a/tests/issue0050.d b/tests/issue0050.d new file mode 100644 index 0000000..448e8d7 --- /dev/null +++ b/tests/issue0050.d @@ -0,0 +1,20 @@ +void fun() +{ + if (something) foreach (_; 0 .. 100) + if (true) + { + if (stuff) + doStuff(); + else + morestuff(); + } + else + doStuff(); + + cast(structalign_t) 1; + for (*cost = 0; sc; sc = sc.enclosing, (*cost)++) + if (sc.scopesym == scopesym) + break; + else + a++; +} diff --git a/tests/issue0050.d.ref b/tests/issue0050.d.ref new file mode 100644 index 0000000..10a90d0 --- /dev/null +++ b/tests/issue0050.d.ref @@ -0,0 +1,21 @@ +void fun() +{ + if (something) + foreach (_; 0 .. 100) + if (true) + { + if (stuff) + doStuff(); + else + morestuff(); + } + else + doStuff(); + + cast(structalign_t) 1; + for (*cost = 0; sc; sc = sc.enclosing, (*cost)++) + if (sc.scopesym == scopesym) + break; + else + a++; +} diff --git a/tests/wrapping1.d.ref b/tests/wrapping1.d.ref index a8c443e..cd2420a 100644 --- a/tests/wrapping1.d.ref +++ b/tests/wrapping1.d.ref @@ -4,6 +4,6 @@ void main(string[] args) { addErrorMessage(line, column, KEY, "Expression %s is true: already checked on line %d.".format( - expressions[prevLocation].formatted, expressions[prevLocation].line)); + expressions[prevLocation].formatted, expressions[prevLocation].line)); } }