From 92d5e1ac8cef71be6538d4b8dc262970cf762f0a Mon Sep 17 00:00:00 2001 From: Bastiaan Veelo Date: Fri, 19 Oct 2018 01:45:52 +0200 Subject: [PATCH] Allow comments in selective imports. (#403) Allow comments in selective imports. merged-on-behalf-of: Brian Schott --- src/dfmt/formatter.d | 35 +++++++++++++---------------------- tests/allman/issue0349.d.ref | 3 ++- tests/allman/issue0384.d.ref | 30 ++++++++++++++++++++++++++++++ tests/issue0349.d | 1 + tests/issue0384.d | 29 +++++++++++++++++++++++++++++ tests/otbs/issue0349.d.ref | 3 ++- tests/otbs/issue0384.d.ref | 30 ++++++++++++++++++++++++++++++ 7 files changed, 107 insertions(+), 24 deletions(-) create mode 100644 tests/allman/issue0384.d.ref create mode 100644 tests/issue0384.d create mode 100644 tests/otbs/issue0384.d.ref diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index ca39597..19b5312 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -523,29 +523,20 @@ private: if (config.dfmt_selective_import_space) write(" "); writeToken(); - write(" "); - } - else if (currentIs(tok!",")) - { - // compute length until next ',' or ';' - int lengthOfNextChunk; - for (size_t i = index + 1; i < tokens.length; i++) - { - if (tokens[i].type == tok!"," || tokens[i].type == tok!";") - break; - const len = tokenLength(tokens[i]); - assert(len >= 0); - lengthOfNextChunk += len; - } - assert(lengthOfNextChunk > 0); - writeToken(); - if (currentLineLength + 1 + lengthOfNextChunk >= config.dfmt_soft_max_line_length) - { - pushWrapIndent(tok!","); - newline(); - } - else + if (!currentIs(tok!"comment")) write(" "); + pushWrapIndent(tok!","); + } + else if (currentIs(tok!"comment")) + { + if (peekBack.line != current.line) + { + // The comment appears on its own line, keep it there. + if (!peekBackIs(tok!"comment")) + // Comments are already properly separated. + newline(); + } + formatStep(); } else formatStep(); diff --git a/tests/allman/issue0349.d.ref b/tests/allman/issue0349.d.ref index abc6a82..c64f3e8 100644 --- a/tests/allman/issue0349.d.ref +++ b/tests/allman/issue0349.d.ref @@ -1,5 +1,6 @@ +import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem; import super_long_import_module_name : withSuperLongSymbolNames, - andAlsoLotsOfThem; + andAlsoLotsOfThem, lotsAnsLots, andLots, andLotsOfThem, lineExceeds120; private: void foo(); diff --git a/tests/allman/issue0384.d.ref b/tests/allman/issue0384.d.ref new file mode 100644 index 0000000..cccd08b --- /dev/null +++ b/tests/allman/issue0384.d.ref @@ -0,0 +1,30 @@ +import std.stdio : readln, /* comment1 */ writeln; +import std.stdio : readln, // comment2 + writeln; +import std.stdio : readln, + // comment3 + writeln; +import std.stdio : readln, + /* comment4 */ + writeln; +import std.stdio : readln, readln, readln, readln, readln, readln, readln, + readln, readln, readln, readln, + // comment5 + writeln; +import std.stdio : // comment6 + readln, readln, readln, readln, readln, readln, // comment7 + // comment8 + writeln; +import std.stdio : /* comment9 */ + readln, readln, readln, readln, readln, readln, /* comment10 */ + // comment11 + writeln; +import std.stdio : readln, // comment12 + readln, readln, readln, readln, readln, readln, // comment13 + // comment14 + writeln; +import std.stdio : readln, + // comment15 + readln, readln, readln, readln, readln, readln, // comment16 + // comment17 + writeln; diff --git a/tests/issue0349.d b/tests/issue0349.d index 613cceb..b280c60 100644 --- a/tests/issue0349.d +++ b/tests/issue0349.d @@ -1,4 +1,5 @@ import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem; +import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem, lotsAnsLots, andLots, andLotsOfThem, lineExceeds120; private: void foo(); diff --git a/tests/issue0384.d b/tests/issue0384.d new file mode 100644 index 0000000..4ea9426 --- /dev/null +++ b/tests/issue0384.d @@ -0,0 +1,29 @@ +import std.stdio : readln, /* comment1 */ writeln; +import std.stdio : readln, // comment2 + writeln; +import std.stdio : readln, + // comment3 + writeln; +import std.stdio : readln, + /* comment4 */ + writeln; +import std.stdio : readln, readln, readln, readln, readln, readln, readln, readln, readln, readln, readln, + // comment5 + writeln; +import std.stdio : // comment6 + readln, readln, readln, readln, readln, readln, // comment7 + // comment8 + writeln; +import std.stdio : /* comment9 */ + readln, readln, readln, readln, readln, readln, /* comment10 */ + // comment11 + writeln; +import std.stdio : readln, // comment12 + readln, readln, readln, readln, readln, readln, // comment13 + // comment14 + writeln; +import std.stdio : readln, + // comment15 + readln, readln, readln, readln, readln, readln, // comment16 + // comment17 + writeln; diff --git a/tests/otbs/issue0349.d.ref b/tests/otbs/issue0349.d.ref index abc6a82..c64f3e8 100644 --- a/tests/otbs/issue0349.d.ref +++ b/tests/otbs/issue0349.d.ref @@ -1,5 +1,6 @@ +import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem; import super_long_import_module_name : withSuperLongSymbolNames, - andAlsoLotsOfThem; + andAlsoLotsOfThem, lotsAnsLots, andLots, andLotsOfThem, lineExceeds120; private: void foo(); diff --git a/tests/otbs/issue0384.d.ref b/tests/otbs/issue0384.d.ref new file mode 100644 index 0000000..cccd08b --- /dev/null +++ b/tests/otbs/issue0384.d.ref @@ -0,0 +1,30 @@ +import std.stdio : readln, /* comment1 */ writeln; +import std.stdio : readln, // comment2 + writeln; +import std.stdio : readln, + // comment3 + writeln; +import std.stdio : readln, + /* comment4 */ + writeln; +import std.stdio : readln, readln, readln, readln, readln, readln, readln, + readln, readln, readln, readln, + // comment5 + writeln; +import std.stdio : // comment6 + readln, readln, readln, readln, readln, readln, // comment7 + // comment8 + writeln; +import std.stdio : /* comment9 */ + readln, readln, readln, readln, readln, readln, /* comment10 */ + // comment11 + writeln; +import std.stdio : readln, // comment12 + readln, readln, readln, readln, readln, readln, // comment13 + // comment14 + writeln; +import std.stdio : readln, + // comment15 + readln, readln, readln, readln, readln, readln, // comment16 + // comment17 + writeln;