From cb9a4015e75d7ee5dc0be74fbf228bc82459fb90 Mon Sep 17 00:00:00 2001 From: Andreas Zwinkau Date: Sat, 17 Jan 2015 00:19:51 +0100 Subject: [PATCH] Import with comment behind must not output newlines --- src/dfmt.d | 4 +++- tests/swap.d | 24 ++++++++++++++++++++++++ tests/swap.d.ref | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/swap.d create mode 100644 tests/swap.d.ref diff --git a/src/dfmt.d b/src/dfmt.d index 83bd5dc..f89db25 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -159,7 +159,7 @@ private: const i = index; if (i > 0) { - if (tokens[i-1].line < tokens[i].line) + if (tokens[i-1].line < current.line) { if (tokens[i-1].type != tok!"comment" && tokens[i-1].type != tok!"{") @@ -192,6 +192,8 @@ private: { writeToken(); tempIndent = 0; + if (current.type == tok!"comment") + break; if (!(t == tok!"import" && current.type == tok!"import")) write("\n"); newline(); diff --git a/tests/swap.d b/tests/swap.d new file mode 100644 index 0000000..d5ee7d3 --- /dev/null +++ b/tests/swap.d @@ -0,0 +1,24 @@ +import std.algorithm: swap; // from Phobos standard library + +// The D solution uses templates and it's similar to the C++ one: +void mySwap(T)(ref T left, ref T right) { + auto temp = left; + left = right; + right = temp; +} + +void main() { + import std.stdio; + + 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); +} diff --git a/tests/swap.d.ref b/tests/swap.d.ref new file mode 100644 index 0000000..a5176e3 --- /dev/null +++ b/tests/swap.d.ref @@ -0,0 +1,24 @@ +import std.algorithm : swap; // from Phobos standard library + +// The D solution uses templates and it's similar to the C++ one: +void mySwap(T)(ref T left, ref T right) +{ + auto temp = left; + left = right; + right = temp; +} + +void main() +{ + import std.stdio; + + 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); +}