From 8983dc8b155c60c1fc6d95e2263ce585dd0fb6e9 Mon Sep 17 00:00:00 2001 From: Andreas Zwinkau Date: Wed, 14 Jan 2015 22:39:45 +0100 Subject: [PATCH] tuning whitespace around comments --- src/dfmt.d | 22 ++++++++++++++++++++-- tests/comments.d | 6 ++++++ tests/comments.d.ref | 7 +++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/comments.d create mode 100644 tests/comments.d.ref diff --git a/src/dfmt.d b/src/dfmt.d index c2e8f81..341c91e 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -115,8 +115,25 @@ private: assert (index < tokens.length); if (current.type == tok!"comment") { + const i = index; + if (i > 0) + { + if (tokens[i-1].line < tokens[i].line) + { + if (tokens[i-1].type != tok!"comment" + && tokens[i-1].type != tok!"{") + newline(); + } + else + write(" "); + } writeToken(); - newline(); + if (i >= tokens.length-1) + newline(); + else if (tokens[i+1].line > tokens[i].line) + newline(); + else if (tokens[i+1].type != tok!"{") + write(" "); } else if (isStringLiteral(current.type) || isNumberLiteral(current.type) || current.type == tok!"characterLiteral") @@ -245,7 +262,8 @@ private: case tok!";": tempIndent = 0; writeToken(); - newline(); + if (current.type != tok!"comment") + newline(); break; case tok!"{": writeBraces(); diff --git a/tests/comments.d b/tests/comments.d new file mode 100644 index 0000000..80f5313 --- /dev/null +++ b/tests/comments.d @@ -0,0 +1,6 @@ +int /*sneaky*/ foo( /*comments*/ ) /*everywhere*/ { + // comment on its own line + foo() // comment on same line + .bar(); // also on same line + /* again */ // same line +} diff --git a/tests/comments.d.ref b/tests/comments.d.ref new file mode 100644 index 0000000..3b0fb36 --- /dev/null +++ b/tests/comments.d.ref @@ -0,0 +1,7 @@ +int /*sneaky*/ foo( /*comments*/ ) /*everywhere*/ +{ + // comment on its own line + foo() // comment on same line + .bar(); // also on same line + /* again */ // same line +}