diff --git a/dub.json b/dub.json index 6cf0dc5..4148322 100644 --- a/dub.json +++ b/dub.json @@ -4,7 +4,7 @@ "targetType": "autodetect", "license": "BSL-1.0", "dependencies": { - "libdparse": "~>0.14.0" + "libdparse": ">=0.14.0 <0.16.0" }, "targetPath" : "bin/", "targetName" : "dfmt", diff --git a/libdparse b/libdparse index 597d9a6..1557eb0 160000 --- a/libdparse +++ b/libdparse @@ -1 +1 @@ -Subproject commit 597d9a697b1f8a51fb2f441c61d0c6cc4eadc6d1 +Subproject commit 1557eb079a2d5958e0a7136f942eea0922d58e8a diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index c922c5f..b9afcb6 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -212,7 +212,9 @@ private: { immutable t = tokens[index].type; if (t == tok!"identifier" || isStringLiteral(t) - || isNumberLiteral(t) || t == tok!"characterLiteral") + || isNumberLiteral(t) || t == tok!"characterLiteral" + // a!"b" function() + || t == tok!"function" || t == tok!"delegate") write(" "); } } @@ -1620,16 +1622,21 @@ private: { import std.range : assumeSorted; import std.algorithm.comparison : min; - import std.algorithm.searching : countUntil; + import std.algorithm.searching : canFind, countUntil; // The end of the tokens considered by the line break algorithm is - // either the expression end index or the next mandatory line break, - // whichever is first. + // either the expression end index or the next mandatory line break + // or a newline inside a string literal, whichever is first. auto r = assumeSorted(astInformation.ufcsHintLocations).upperBound(tokens[i].index); immutable ufcsBreakLocation = r.empty ? size_t.max : tokens[i .. $].countUntil!(t => t.index == r.front) + i; - immutable size_t j = min(expressionEndIndex(i), ufcsBreakLocation); + immutable multilineStringLocation = tokens[i .. $] + .countUntil!(t => t.text.canFind('\n')); + immutable size_t j = min( + expressionEndIndex(i), + ufcsBreakLocation, + multilineStringLocation == -1 ? size_t.max : multilineStringLocation + i + 1); // Use magical negative value for array literals and wrap indents immutable inLvl = (indents.topIsWrap() || indents.topIs(tok!"]")) ? -indentLevel : indentLevel; @@ -1713,7 +1720,14 @@ private: } else if (currentIs(tok!"case") || currentIs(tok!"default")) { - if (peekBackIs(tok!"}", true) || peekBackIs(tok!";", true)) + + if (peekBackIs(tok!"}", true) || peekBackIs(tok!";", true) + /** + * The following code is valid and should be indented flatly + * case A: + * case B: + */ + || peekBackIs(tok!":", true)) { indents.popTempIndents(); if (indents.topIs(tok!"case")) @@ -1830,7 +1844,11 @@ private: case tok!"wstringLiteral": case tok!"dstringLiteral": immutable o = current.text.retro().countUntil('\n'); - currentLineLength += o == -1 ? current.text.length : o; + if (o == -1) { + currentLineLength += current.text.length; + } else { + currentLineLength = cast(uint) o; + } break; default: currentLineLength += current.text.length; diff --git a/src/dfmt/wrapping.d b/src/dfmt/wrapping.d index 41edf2f..79fb85f 100644 --- a/src/dfmt/wrapping.d +++ b/src/dfmt/wrapping.d @@ -172,9 +172,6 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens, void validMoves(OR)(auto ref OR output, const Token[] tokens, immutable short[] depths, uint current, const Config* config, int currentLineLength, int indentLevel) { - import std.algorithm : sort, canFind, min; - import std.array : insertInPlace; - foreach (i, token; tokens) { if (!isBreakToken(token.type) || (((1 << i) & current) != 0)) diff --git a/tests/allman/issue0476.d.ref b/tests/allman/issue0476.d.ref new file mode 100644 index 0000000..c95e2de --- /dev/null +++ b/tests/allman/issue0476.d.ref @@ -0,0 +1,7 @@ +string BuildForwardCall() +{ + return `static if (is(typeof(mocked___.` ~ methodString ~ argsPassed ~ `))) + { + return (mocked___.` ~ methodString ~ argsPassed ~ `); + }`; +} diff --git a/tests/allman/issue0483.d.ref b/tests/allman/issue0483.d.ref new file mode 100644 index 0000000..1d77e68 --- /dev/null +++ b/tests/allman/issue0483.d.ref @@ -0,0 +1,15 @@ +module tests.issue0483; + +void main() +{ + switch (0) + { + case 1: + case 2: + label: + case 3: + break; + default: + break; + } +} diff --git a/tests/allman/issue0497.d.ref b/tests/allman/issue0497.d.ref new file mode 100644 index 0000000..eec042e --- /dev/null +++ b/tests/allman/issue0497.d.ref @@ -0,0 +1,5 @@ +alias f1 = S function(); +alias f2 = S!"foo" function(); +alias f3 = S!5 function(); +alias f4 = S!S function(); +alias f5 = S!(S) function(); diff --git a/tests/issue0476.d b/tests/issue0476.d new file mode 100644 index 0000000..c95e2de --- /dev/null +++ b/tests/issue0476.d @@ -0,0 +1,7 @@ +string BuildForwardCall() +{ + return `static if (is(typeof(mocked___.` ~ methodString ~ argsPassed ~ `))) + { + return (mocked___.` ~ methodString ~ argsPassed ~ `); + }`; +} diff --git a/tests/issue0483.args b/tests/issue0483.args new file mode 100644 index 0000000..91e2439 --- /dev/null +++ b/tests/issue0483.args @@ -0,0 +1 @@ +--align_switch_statements=false diff --git a/tests/issue0483.d b/tests/issue0483.d new file mode 100644 index 0000000..1d77e68 --- /dev/null +++ b/tests/issue0483.d @@ -0,0 +1,15 @@ +module tests.issue0483; + +void main() +{ + switch (0) + { + case 1: + case 2: + label: + case 3: + break; + default: + break; + } +} diff --git a/tests/issue0497.d b/tests/issue0497.d new file mode 100644 index 0000000..eec042e --- /dev/null +++ b/tests/issue0497.d @@ -0,0 +1,5 @@ +alias f1 = S function(); +alias f2 = S!"foo" function(); +alias f3 = S!5 function(); +alias f4 = S!S function(); +alias f5 = S!(S) function(); diff --git a/tests/otbs/issue0476.d.ref b/tests/otbs/issue0476.d.ref new file mode 100644 index 0000000..924e771 --- /dev/null +++ b/tests/otbs/issue0476.d.ref @@ -0,0 +1,6 @@ +string BuildForwardCall() { + return `static if (is(typeof(mocked___.` ~ methodString ~ argsPassed ~ `))) + { + return (mocked___.` ~ methodString ~ argsPassed ~ `); + }`; +} diff --git a/tests/otbs/issue0483.d.ref b/tests/otbs/issue0483.d.ref new file mode 100644 index 0000000..b23c692 --- /dev/null +++ b/tests/otbs/issue0483.d.ref @@ -0,0 +1,13 @@ +module tests.issue0483; + +void main() { + switch (0) { + case 1: + case 2: + label: + case 3: + break; + default: + break; + } +} diff --git a/tests/otbs/issue0497.d.ref b/tests/otbs/issue0497.d.ref new file mode 100644 index 0000000..eec042e --- /dev/null +++ b/tests/otbs/issue0497.d.ref @@ -0,0 +1,5 @@ +alias f1 = S function(); +alias f2 = S!"foo" function(); +alias f3 = S!5 function(); +alias f4 = S!S function(); +alias f5 = S!(S) function();