From 98f443d3f14e4194f02ebff538fe006621f7b2d3 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Fri, 22 Jan 2016 04:48:29 -0800 Subject: [PATCH] Fix #220 --- src/dfmt/formatter.d | 43 ++++++++++++++++++----- src/dfmt/indentation.d | 14 ++++++++ tests/allman/issue0220.d.ref | 68 ++++++++++++++++++++++++++++++++++++ tests/issue0220.d | 68 ++++++++++++++++++++++++++++++++++++ tests/otbs/issue0220.d.ref | 60 +++++++++++++++++++++++++++++++ 5 files changed, 244 insertions(+), 9 deletions(-) create mode 100644 tests/allman/issue0220.d.ref create mode 100644 tests/issue0220.d create mode 100644 tests/otbs/issue0220.d.ref diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 595a44e..e9b0d20 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -656,6 +656,11 @@ private: linebreakHints = []; while (indents.topIs(tok!"enum")) indents.pop(); + if (indents.topAre(tok!"static", tok!"else")) + { + indents.pop(); + indents.pop(); + } if (config.dfmt_brace_style == BraceStyle.allman) { if (!currentIs(tok!"{")) @@ -726,7 +731,7 @@ private: } else { - if (indents.length && isTempIndent(indents.top)) + if (indents.topIsTemp && indents.indentToMostRecent(tok!"static") == -1) indentLevel = indents.indentLevel - 1; else indentLevel = indents.indentLevel; @@ -777,13 +782,14 @@ private: if (index + 1 < tokens.length && astInformation.doubleNewlineLocations.canFindIndex( tokens[index].index) && !peekIs(tok!"}") - && !peekIs(tok!";") && !peekIs(tok!"comment", false)) + && !peekIs(tok!"else") && !peekIs(tok!";") && !peekIs(tok!"comment", false)) { simpleNewline(); currentLineLength = 0; justAddedExtraNewline = true; } - if (config.dfmt_brace_style == BraceStyle.otbs && peekIs(tok!"else")) + if (config.dfmt_brace_style == BraceStyle.otbs && peekIs(tok!"else") + && !indents.topAre(tok!"static", tok!"if")) { write(" "); index++; @@ -794,6 +800,8 @@ private: && !peekIs(tok!";") && !peekIs(tok!"{")) { index++; + if (indents.topIs(tok!"static")) + indents.pop(); newline(); } else @@ -825,7 +833,16 @@ private: if (currentIs(tok!"out") && !peekBackIs(tok!"}")) newline(); if (shouldPushIndent) + { + if (peekBackIs(tok!"static")) + { + if (indents.topIs(tok!"else")) + indents.pop(); + if (!indents.topIs(tok!"static")) + indents.push(tok!"static"); + } indents.push(current.type); + } writeToken(); if (currentIs(tok!"(")) { @@ -844,6 +861,14 @@ private: else if (!currentIs(tok!"{") && !currentIs(tok!";") && !currentIs(tok!"in") && !currentIs(tok!"out") && !currentIs(tok!"body")) newline(); + else if (currentIs(tok!"{") && indents.topAre(tok!"static", tok!"if")) + { + // Hacks to format braced vs non-braced static if declarations. + indents.pop(); + indents.pop(); + indents.push(tok!"if"); + formatLeftBrace(); + } } void formatElse() @@ -863,7 +888,7 @@ private: } else if (!currentIs(tok!"{") && !currentIs(tok!"comment")) { - if (indents.topIs(tok!"if") || indents.topIs(tok!"version")) + if (indents.topIsOneOf(tok!"if", tok!"version")) indents.pop(); indents.push(tok!"else"); newline(); @@ -1249,8 +1274,8 @@ private: { if (currentIs(tok!"else")) { - auto i = indents.indentToMostRecent(tok!"if"); - auto v = indents.indentToMostRecent(tok!"version"); + immutable i = indents.indentToMostRecent(tok!"if"); + immutable v = indents.indentToMostRecent(tok!"version"); immutable mostRecent = max(i, v); if (mostRecent != -1) indentLevel = mostRecent; @@ -1302,7 +1327,7 @@ private: else if (currentIs(tok!"}")) { indents.popTempIndents(); - while (indents.topIs(tok!"case") || indents.topIs(tok!"@")) + while (indents.topIsOneOf(tok!"case", tok!"@", tok!"static")) indents.pop(); if (indents.topIs(tok!"{")) { @@ -1310,8 +1335,8 @@ private: indents.pop(); } while (sBraceDepth == 0 && indents.topIsTemp() - && ((indents.top != tok!"if" - && indents.top != tok!"version") || !peekIs(tok!"else"))) + && ((!indents.topIsOneOf(tok!"else", tok!"if", tok!"static", tok!"version")) + || !peekIs(tok!"else"))) { indents.pop(); } diff --git a/src/dfmt/indentation.d b/src/dfmt/indentation.d index ca39613..6ea5f2d 100644 --- a/src/dfmt/indentation.d +++ b/src/dfmt/indentation.d @@ -97,6 +97,14 @@ struct IndentStack index--; } + bool topAre(IdType[] types...) + { + if (types.length > index) + return false; + return arr[index - types.length .. index] == types; + + } + /** * Returns: `true` if the top of the indent stack is the given indent type. */ @@ -188,6 +196,12 @@ private: continue; immutable currentIsNonWrapTemp = !isWrapIndent(arr[i]) && isTempIndent(arr[i]) && arr[i] != tok!")" && arr[i] != tok!"!"; + if (arr[i] == tok!"static" && (arr[i + 1] == tok!"if" || arr[i + 1] == tok!"else") + && (i + 2 >= index || arr[i + 2] != tok!"{")) + { + parenCount = pc; + continue; + } if (currentIsNonWrapTemp && (arr[i + 1] == tok!"switch" || arr[i + 1] == tok!"{" || arr[i + 1] == tok!")")) { diff --git a/tests/allman/issue0220.d.ref b/tests/allman/issue0220.d.ref new file mode 100644 index 0000000..a6972d4 --- /dev/null +++ b/tests/allman/issue0220.d.ref @@ -0,0 +1,68 @@ +static if (someCondition) + void doStuff() + { + } +else + void doStuff() + { + } + +static if (someCondition) + void doStuff() + { + } +else static if (otherCondition) + void doStuff() + { + } + +static if (someCondition) + void doStuff() + { + } +else static if (otherCondition) + void doStuff() + { + } +else + void doStuff() + { + } + +static if (condition) + int a; +else + int b; + +static if (condition) + int a; +else static if (otherCondition) + int c; +else + int b; + +void doStuff(); + +static if (stuff) + int a; +else + class C + { + public: + void aFunction(); + private: + int a; + int b; + } + +static if (condition) + int a; +else + int b; + +static if (condition) + int a; +else static if (otherCondition) + int c; +else + int b; diff --git a/tests/issue0220.d b/tests/issue0220.d new file mode 100644 index 0000000..5534ea4 --- /dev/null +++ b/tests/issue0220.d @@ -0,0 +1,68 @@ +static if (someCondition) + void doStuff() + { + } +else + void doStuff() + { + } + +static if (someCondition) + void doStuff() + { + } +else static if (otherCondition) + void doStuff() + { + } + +static if (someCondition) + void doStuff() + { + } +else static if (otherCondition) + void doStuff() + { + } +else + void doStuff() + { + } + +static if (condition) + int a; +else + int b; + +static if (condition) + int a; +else static if (otherCondition) + int c; +else + int b; + +void doStuff(); + +static if (stuff) + int a; +else + class C + { + public: + void aFunction(); + private: + int a; + int b; + } + +static if (condition) + int a; +else + int b; + +static if (condition) + int a; +else static if (otherCondition) + int c; +else + int b; diff --git a/tests/otbs/issue0220.d.ref b/tests/otbs/issue0220.d.ref new file mode 100644 index 0000000..c8f09ef --- /dev/null +++ b/tests/otbs/issue0220.d.ref @@ -0,0 +1,60 @@ +static if (someCondition) + void doStuff() { + } +else + void doStuff() { + } + +static if (someCondition) + void doStuff() { + } +else static if (otherCondition) + void doStuff() { + } + +static if (someCondition) + void doStuff() { + } +else static if (otherCondition) + void doStuff() { + } +else + void doStuff() { + } + +static if (condition) + int a; +else + int b; + +static if (condition) + int a; +else static if (otherCondition) + int c; +else + int b; + +void doStuff(); + +static if (stuff) + int a; +else + class C { + public: + void aFunction(); + private: + int a; + int b; + } + +static if (condition) + int a; +else + int b; + +static if (condition) + int a; +else static if (otherCondition) + int c; +else + int b;