From ee8fcd41f4025662fcb49132c6d6e1177aa8af06 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Fri, 22 Jan 2016 01:51:44 -0800 Subject: [PATCH] Fix #223 --- src/dfmt/formatter.d | 15 ++++++----- src/dfmt/globmatch_editorconfig.d | 6 ++--- src/dfmt/indentation.d | 34 ++++++++++++------------- src/dfmt/wrapping.d | 3 +-- tests/allman/attribute_constraint.d.ref | 2 +- tests/allman/issue0119.d.ref | 4 +-- tests/allman/issue0186.d.ref | 12 ++++----- tests/allman/issue0215a.d.ref | 2 +- tests/allman/issue0215d.d.ref | 2 +- tests/allman/issue0223.d.ref | 8 ++++++ tests/allman/wrapping1.d.ref | 2 +- tests/issue0223.d | 8 ++++++ tests/otbs/attribute_constraint.d.ref | 2 +- tests/otbs/issue0119.d.ref | 4 +-- tests/otbs/issue0186.d.ref | 12 ++++----- tests/otbs/issue0215a.d.ref | 2 +- tests/otbs/issue0215d.d.ref | 2 +- tests/otbs/issue0223.d.ref | 7 +++++ tests/otbs/wrapping1.d.ref | 2 +- 19 files changed, 77 insertions(+), 52 deletions(-) create mode 100644 tests/allman/issue0223.d.ref create mode 100644 tests/issue0223.d create mode 100644 tests/otbs/issue0223.d.ref diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index d18a328..595a44e 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -264,7 +264,7 @@ private: || isBasicType(current.type) || currentIs(tok!"@") || currentIs(tok!"if") || isNumberLiteral(tokens[index].type) || (inAsm - && peekBack2Is(tok!";") && currentIs(tok!"[")))) + && peekBack2Is(tok!";") && currentIs(tok!"[")))) { write(" "); } @@ -493,7 +493,10 @@ private: writeToken(); if (p == tok!"(") { - indents.push(p); + if (isBlockHeaderToken(tokens[index - 2].type)) + indents.push(tok!")"); + else + indents.push(p); spaceAfterParens = true; parenDepth++; } @@ -527,9 +530,9 @@ private: body { parenDepth--; - if (parenDepth == 0 && indents.topIs(tok!"!")) - indents.pop(); indents.popWrapIndents(); + while (indents.topIsOneOf(tok!"!", tok!")")) + indents.pop(); if (indents.topIs(tok!"(")) indents.pop(); @@ -773,7 +776,7 @@ private: write("}"); if (index + 1 < tokens.length && astInformation.doubleNewlineLocations.canFindIndex( - tokens[index].index) && !peekIs(tok!"}") + tokens[index].index) && !peekIs(tok!"}") && !peekIs(tok!";") && !peekIs(tok!"comment", false)) { simpleNewline(); @@ -1225,7 +1228,7 @@ private: if (niBraceDepth > 0 && !peekBackIsSlashSlash() && hasCurrent && tokens[index].type == tok!"}" && !assumeSorted(astInformation.funLitEndLocations).equalRange( - tokens[index].index).empty) + tokens[index].index).empty) { write(" "); return; diff --git a/src/dfmt/globmatch_editorconfig.d b/src/dfmt/globmatch_editorconfig.d index 28d3e75..980ce6c 100644 --- a/src/dfmt/globmatch_editorconfig.d +++ b/src/dfmt/globmatch_editorconfig.d @@ -11,9 +11,9 @@ import std.path : filenameCharCmp, isDirSeparator; // * changes meaning to match all characters except '/' // ** added to take over the old meaning of * bool globMatchEditorConfig(CaseSensitive cs = CaseSensitive.osDefault, C, Range)( - Range path, const(C)[] pattern) @safe pure nothrow if (isForwardRange!Range - && isSomeChar!(ElementEncodingType!Range) && isSomeChar!C - && is(Unqual!C == Unqual!(ElementEncodingType!Range))) + Range path, const(C)[] pattern) @safe pure nothrow + if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) + && isSomeChar!C && is(Unqual!C == Unqual!(ElementEncodingType!Range))) in { // Verify that pattern[] is valid diff --git a/src/dfmt/indentation.d b/src/dfmt/indentation.d index 1a4ab51..ca39613 100644 --- a/src/dfmt/indentation.d +++ b/src/dfmt/indentation.d @@ -13,7 +13,7 @@ import dparse.lexer; bool isWrapIndent(IdType type) pure nothrow @nogc @safe { return type != tok!"{" && type != tok!"case" && type != tok!"@" - && type != tok!"]" && type != tok!"(" && isOperator(type); + && type != tok!"]" && type != tok!"(" && type != tok!")" && isOperator(type); } /** @@ -21,7 +21,7 @@ bool isWrapIndent(IdType type) pure nothrow @nogc @safe */ bool isTempIndent(IdType type) pure nothrow @nogc @safe { - return type != tok!"{" && type != tok!"case" && type != tok!"@"; + return type != tok!")" && type != tok!"{" && type != tok!"case" && type != tok!"@"; } /** @@ -175,29 +175,29 @@ private: int parenCount; foreach (i; 0 .. j) { + immutable int pc = (arr[i] == tok!"!" || arr[i] == tok!"(" || arr[i] == tok!")") ? parenCount + 1 + : parenCount; + if ((isWrapIndent(arr[i]) || arr[i] == tok!"(") && parenCount > 1) + { + parenCount = pc; + continue; + } if (i + 1 < index) { - if (arr[i] == tok!"(") - parenCount++; - else if (arr[i] == tok!"]") + if (arr[i] == tok!"]") continue; - else + immutable currentIsNonWrapTemp = !isWrapIndent(arr[i]) + && isTempIndent(arr[i]) && arr[i] != tok!")" && arr[i] != tok!"!"; + if (currentIsNonWrapTemp && (arr[i + 1] == tok!"switch" + || arr[i + 1] == tok!"{" || arr[i + 1] == tok!")")) { - if (isWrapIndent(arr[i]) && parenCount > 0) - { - parenCount = 0; - continue; - } - parenCount = 0; - } - immutable currentIsNonWrapTemp = !isWrapIndent(arr[i]) && isTempIndent(arr[i]); - immutable nextIsParenOrSwitch = arr[i + 1] == tok!"(" - || arr[i + 1] == tok!"switch" || arr[i + 1] == tok!"{"; - if (currentIsNonWrapTemp && nextIsParenOrSwitch) + parenCount = pc; continue; + } } if (arr[i] == tok!"!") size++; + parenCount = pc; size++; } return size; diff --git a/src/dfmt/wrapping.d b/src/dfmt/wrapping.d index 52ca119..cb82c13 100644 --- a/src/dfmt/wrapping.d +++ b/src/dfmt/wrapping.d @@ -153,8 +153,7 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens, return genRetVal(current.breaks, index); } validMoves!(typeof(open))(open, tokens[0 .. tokensEnd], - depths[0 .. tokensEnd], current.breaks, config, currentLineLength, - indentLevel); + depths[0 .. tokensEnd], current.breaks, config, currentLineLength, indentLevel); } if (open.empty) return genRetVal(lowest.breaks, index); diff --git a/tests/allman/attribute_constraint.d.ref b/tests/allman/attribute_constraint.d.ref index 71da591..c171b80 100644 --- a/tests/allman/attribute_constraint.d.ref +++ b/tests/allman/attribute_constraint.d.ref @@ -8,7 +8,7 @@ struct SomeStructName { void longFunctionName(AAAAAAAA)(AAAAAAAA a) @property if (someThingsAreTrue!AAAAAAAA && long_condition - && is(some < elaborate && expression)) + && is(some < elaborate && expression)) { } } diff --git a/tests/allman/issue0119.d.ref b/tests/allman/issue0119.d.ref index e337743..30596c7 100644 --- a/tests/allman/issue0119.d.ref +++ b/tests/allman/issue0119.d.ref @@ -18,8 +18,8 @@ unittest callFunc({ int i = 10; foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, - delta_longVarName, echo_longVarName, foxtrot_longVarName, - golf_longVarName, echo_longVarName); + delta_longVarName, echo_longVarName, foxtrot_longVarName, + golf_longVarName, echo_longVarName); doStuff(withThings, andOtherStuff); return i; }, more_stuff); diff --git a/tests/allman/issue0186.d.ref b/tests/allman/issue0186.d.ref index 5f3b524..5da362b 100644 --- a/tests/allman/issue0186.d.ref +++ b/tests/allman/issue0186.d.ref @@ -8,12 +8,12 @@ void main() static if (true) { if (true && { - functionName(); - functionName(); - functionName(); - functionName(); - return true; - }()) + functionName(); + functionName(); + functionName(); + functionName(); + return true; + }()) { } diff --git a/tests/allman/issue0215a.d.ref b/tests/allman/issue0215a.d.ref index fb82d3e..d032aa8 100644 --- a/tests/allman/issue0215a.d.ref +++ b/tests/allman/issue0215a.d.ref @@ -10,7 +10,7 @@ unittest bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo, Three charlie, double delta) if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo - && foxtrot && golf && hotel && india && juliet) + && foxtrot && golf && hotel && india && juliet) { } diff --git a/tests/allman/issue0215d.d.ref b/tests/allman/issue0215d.d.ref index 501d7e7..3f7dfc5 100644 --- a/tests/allman/issue0215d.d.ref +++ b/tests/allman/issue0215d.d.ref @@ -11,7 +11,7 @@ unittest bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo, Three charlie, double delta) if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo - && foxtrot && golf && hotel && india && juliet) + && foxtrot && golf && hotel && india && juliet) { } diff --git a/tests/allman/issue0223.d.ref b/tests/allman/issue0223.d.ref new file mode 100644 index 0000000..628992e --- /dev/null +++ b/tests/allman/issue0223.d.ref @@ -0,0 +1,8 @@ +unittest +{ + if (info > 0) + throw new ExceptionWithLongName( + std.string.format( + "During the LU factorization, it was found that the " ~ "%sth diagonal value is exactly zero.", + info), file, line); +} diff --git a/tests/allman/wrapping1.d.ref b/tests/allman/wrapping1.d.ref index 320b14b..e8746a0 100644 --- a/tests/allman/wrapping1.d.ref +++ b/tests/allman/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)); } } diff --git a/tests/issue0223.d b/tests/issue0223.d new file mode 100644 index 0000000..3a4763a --- /dev/null +++ b/tests/issue0223.d @@ -0,0 +1,8 @@ +unittest +{ + if (info > 0) + throw new ExceptionWithLongName( + std.string.format( + "During the LU factorization, it was found that the " ~ "%sth diagonal value is exactly zero.", + info), file, line); +} diff --git a/tests/otbs/attribute_constraint.d.ref b/tests/otbs/attribute_constraint.d.ref index 08280d2..8704767 100644 --- a/tests/otbs/attribute_constraint.d.ref +++ b/tests/otbs/attribute_constraint.d.ref @@ -4,7 +4,7 @@ struct SomeStructName { static if (condition) { void longFunctionName(AAAAAAAA)(AAAAAAAA a) @property if (someThingsAreTrue!AAAAAAAA && long_condition - && is(some < elaborate && expression)) { + && is(some < elaborate && expression)) { } } } diff --git a/tests/otbs/issue0119.d.ref b/tests/otbs/issue0119.d.ref index e20016e..1b961d8 100644 --- a/tests/otbs/issue0119.d.ref +++ b/tests/otbs/issue0119.d.ref @@ -17,8 +17,8 @@ unittest { callFunc({ int i = 10; foo(alpha_longVarName, bravo_longVarName, charlie_longVarName, - delta_longVarName, echo_longVarName, foxtrot_longVarName, - golf_longVarName, echo_longVarName); + delta_longVarName, echo_longVarName, foxtrot_longVarName, + golf_longVarName, echo_longVarName); doStuff(withThings, andOtherStuff); return i; }, more_stuff); diff --git a/tests/otbs/issue0186.d.ref b/tests/otbs/issue0186.d.ref index 8711a7c..9bf7593 100644 --- a/tests/otbs/issue0186.d.ref +++ b/tests/otbs/issue0186.d.ref @@ -5,12 +5,12 @@ void functionName() { void main() { static if (true) { if (true && { - functionName(); - functionName(); - functionName(); - functionName(); - return true; - }()) { + functionName(); + functionName(); + functionName(); + functionName(); + return true; + }()) { } else { diff --git a/tests/otbs/issue0215a.d.ref b/tests/otbs/issue0215a.d.ref index 07dffc8..3dd5734 100644 --- a/tests/otbs/issue0215a.d.ref +++ b/tests/otbs/issue0215a.d.ref @@ -8,7 +8,7 @@ unittest { bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo, Three charlie, double delta) if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo - && foxtrot && golf && hotel && india && juliet) { + && foxtrot && golf && hotel && india && juliet) { } } diff --git a/tests/otbs/issue0215d.d.ref b/tests/otbs/issue0215d.d.ref index 625861f..4377259 100644 --- a/tests/otbs/issue0215d.d.ref +++ b/tests/otbs/issue0215d.d.ref @@ -9,7 +9,7 @@ unittest { bool anotherTemplatedFunction(One, Two, Three)(One alpha, Two bravo, Three charlie, double delta) if (isNumeric!One && isNumeric!Two && isNumeric!Three && echo - && foxtrot && golf && hotel && india && juliet) { + && foxtrot && golf && hotel && india && juliet) { } } diff --git a/tests/otbs/issue0223.d.ref b/tests/otbs/issue0223.d.ref new file mode 100644 index 0000000..0adde3b --- /dev/null +++ b/tests/otbs/issue0223.d.ref @@ -0,0 +1,7 @@ +unittest { + if (info > 0) + throw new ExceptionWithLongName( + std.string.format( + "During the LU factorization, it was found that the " ~ "%sth diagonal value is exactly zero.", + info), file, line); +} diff --git a/tests/otbs/wrapping1.d.ref b/tests/otbs/wrapping1.d.ref index 1e3b571..352e1cc 100644 --- a/tests/otbs/wrapping1.d.ref +++ b/tests/otbs/wrapping1.d.ref @@ -2,6 +2,6 @@ void main(string[] args) { if (prevLocation != size_t.max) { 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)); } }