diff --git a/src/dfmt.d b/src/dfmt.d index 7569ec7..70b6209 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -189,8 +189,12 @@ private: write(" "); } writeToken(); + auto j = justAddedExtraNewline; if (tokens[index - 1].text[0 .. 2] == "//") + { newline(); + justAddedExtraNewline = j; + } else if (index < tokens.length) { if (tokens[index - 1].line == tokens[index].line) @@ -231,10 +235,18 @@ private: break; } if (current.type == tok!"comment" && current.line == peekBack().line) + { + justAddedExtraNewline = true; break; - if (!(t == tok!"import" && current.type == tok!"import")) + } + else if ((t == tok!"import" && current.type != tok!"import")) + { write("\n"); - newline(); + justAddedExtraNewline = true; + newline(); + } + else + newline(); break; } else if (current.type == tok!",") @@ -431,8 +443,6 @@ private: if (index >= tokens.length || current.type != tok!"comment" || current.line != tokens[index - 1].line) newline(); - if (peekImplementation(tok!"class",0)) - newline(); break; case tok!"{": writeBraces(); @@ -877,8 +887,15 @@ private: { import std.range:assumeSorted; output.put("\n"); + immutable bool hasCurrent = index + 1 < tokens.length; + if (index > 0 && hasCurrent && tokens[index - 1].type != tok!"}" + && tokens[index].line - tokens[index - 1].line > 1 && !justAddedExtraNewline) + { + output.put("\n"); + } + justAddedExtraNewline = false; currentLineLength = 0; - if (index < tokens.length) + if (hasCurrent) { if (current.type == tok!"}") indentLevel--; @@ -950,6 +967,10 @@ private: /// Configuration FormatterConfig* config; + + /// Keep track of whether or not an extra newline was just added because of + /// an import statement. + bool justAddedExtraNewline; } /// The only good brace styles diff --git a/tests/DeclSpacing.d.ref b/tests/DeclSpacing.d.ref index 7383c47..3c7ed00 100644 --- a/tests/DeclSpacing.d.ref +++ b/tests/DeclSpacing.d.ref @@ -12,7 +12,6 @@ void main() } const baz = 11; - class Foo2 : Foo { } diff --git a/tests/guessnumber.d.ref b/tests/guessnumber.d.ref index e863781..b40b2e9 100644 --- a/tests/guessnumber.d.ref +++ b/tests/guessnumber.d.ref @@ -6,10 +6,12 @@ void main() writefln("Guess my target number that is between " ~ "%d and %d (inclusive).\n", interval[]); immutable target = uniform!"[]"(interval[]); + foreach (immutable i; sequence!q{n}) { writef("Your guess #%d: ", i + 1); immutable txt = stdin.readln.strip; + Nullable!int answer; try { diff --git a/tests/swap.d.ref b/tests/swap.d.ref index a5176e3..660e05a 100644 --- a/tests/swap.d.ref +++ b/tests/swap.d.ref @@ -14,10 +14,12 @@ void main() int[] a = [10, 20]; writeln(a); + // The std.algorithm standard library module // contains a generic swap: swap(a[0], a[1]); writeln(a); + // Using mySwap: mySwap(a[0], a[1]); writeln(a);