Skip to content

Commit 92d5e1a

Browse files
veelodlang-bot
authored andcommitted
Allow comments in selective imports. (#403)
Allow comments in selective imports. merged-on-behalf-of: Brian Schott <Hackerpilot@users.noreply.github.com>
1 parent 6ad2fbb commit 92d5e1a

File tree

7 files changed

+104
-21
lines changed

7 files changed

+104
-21
lines changed

src/dfmt/formatter.d

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -523,29 +523,20 @@ private:
523523
if (config.dfmt_selective_import_space)
524524
write(" ");
525525
writeToken();
526-
write(" ");
526+
if (!currentIs(tok!"comment"))
527+
write(" ");
528+
pushWrapIndent(tok!",");
527529
}
528-
else if (currentIs(tok!","))
530+
else if (currentIs(tok!"comment"))
529531
{
530-
// compute length until next ',' or ';'
531-
int lengthOfNextChunk;
532-
for (size_t i = index + 1; i < tokens.length; i++)
533-
{
534-
if (tokens[i].type == tok!"," || tokens[i].type == tok!";")
535-
break;
536-
const len = tokenLength(tokens[i]);
537-
assert(len >= 0);
538-
lengthOfNextChunk += len;
539-
}
540-
assert(lengthOfNextChunk > 0);
541-
writeToken();
542-
if (currentLineLength + 1 + lengthOfNextChunk >= config.dfmt_soft_max_line_length)
532+
if (peekBack.line != current.line)
543533
{
544-
pushWrapIndent(tok!",");
545-
newline();
534+
// The comment appears on its own line, keep it there.
535+
if (!peekBackIs(tok!"comment"))
536+
// Comments are already properly separated.
537+
newline();
546538
}
547-
else
548-
write(" ");
539+
formatStep();
549540
}
550541
else
551542
formatStep();

tests/allman/issue0349.d.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem;
12
import super_long_import_module_name : withSuperLongSymbolNames,
2-
andAlsoLotsOfThem;
3+
andAlsoLotsOfThem, lotsAnsLots, andLots, andLotsOfThem, lineExceeds120;
34

45
private:
56
void foo();

tests/allman/issue0384.d.ref

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import std.stdio : readln, /* comment1 */ writeln;
2+
import std.stdio : readln, // comment2
3+
writeln;
4+
import std.stdio : readln,
5+
// comment3
6+
writeln;
7+
import std.stdio : readln,
8+
/* comment4 */
9+
writeln;
10+
import std.stdio : readln, readln, readln, readln, readln, readln, readln,
11+
readln, readln, readln, readln,
12+
// comment5
13+
writeln;
14+
import std.stdio : // comment6
15+
readln, readln, readln, readln, readln, readln, // comment7
16+
// comment8
17+
writeln;
18+
import std.stdio : /* comment9 */
19+
readln, readln, readln, readln, readln, readln, /* comment10 */
20+
// comment11
21+
writeln;
22+
import std.stdio : readln, // comment12
23+
readln, readln, readln, readln, readln, readln, // comment13
24+
// comment14
25+
writeln;
26+
import std.stdio : readln,
27+
// comment15
28+
readln, readln, readln, readln, readln, readln, // comment16
29+
// comment17
30+
writeln;

tests/issue0349.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem;
2+
import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem, lotsAnsLots, andLots, andLotsOfThem, lineExceeds120;
23

34
private:
45
void foo();

tests/issue0384.d

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import std.stdio : readln, /* comment1 */ writeln;
2+
import std.stdio : readln, // comment2
3+
writeln;
4+
import std.stdio : readln,
5+
// comment3
6+
writeln;
7+
import std.stdio : readln,
8+
/* comment4 */
9+
writeln;
10+
import std.stdio : readln, readln, readln, readln, readln, readln, readln, readln, readln, readln, readln,
11+
// comment5
12+
writeln;
13+
import std.stdio : // comment6
14+
readln, readln, readln, readln, readln, readln, // comment7
15+
// comment8
16+
writeln;
17+
import std.stdio : /* comment9 */
18+
readln, readln, readln, readln, readln, readln, /* comment10 */
19+
// comment11
20+
writeln;
21+
import std.stdio : readln, // comment12
22+
readln, readln, readln, readln, readln, readln, // comment13
23+
// comment14
24+
writeln;
25+
import std.stdio : readln,
26+
// comment15
27+
readln, readln, readln, readln, readln, readln, // comment16
28+
// comment17
29+
writeln;

tests/otbs/issue0349.d.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import super_long_import_module_name : withSuperLongSymbolNames, andAlsoLotsOfThem;
12
import super_long_import_module_name : withSuperLongSymbolNames,
2-
andAlsoLotsOfThem;
3+
andAlsoLotsOfThem, lotsAnsLots, andLots, andLotsOfThem, lineExceeds120;
34

45
private:
56
void foo();

tests/otbs/issue0384.d.ref

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import std.stdio : readln, /* comment1 */ writeln;
2+
import std.stdio : readln, // comment2
3+
writeln;
4+
import std.stdio : readln,
5+
// comment3
6+
writeln;
7+
import std.stdio : readln,
8+
/* comment4 */
9+
writeln;
10+
import std.stdio : readln, readln, readln, readln, readln, readln, readln,
11+
readln, readln, readln, readln,
12+
// comment5
13+
writeln;
14+
import std.stdio : // comment6
15+
readln, readln, readln, readln, readln, readln, // comment7
16+
// comment8
17+
writeln;
18+
import std.stdio : /* comment9 */
19+
readln, readln, readln, readln, readln, readln, /* comment10 */
20+
// comment11
21+
writeln;
22+
import std.stdio : readln, // comment12
23+
readln, readln, readln, readln, readln, readln, // comment13
24+
// comment14
25+
writeln;
26+
import std.stdio : readln,
27+
// comment15
28+
readln, readln, readln, readln, readln, readln, // comment16
29+
// comment17
30+
writeln;

0 commit comments

Comments
 (0)