Skip to content

Commit 2c216d8

Browse files
authored
606 enum (#608)
* Preserve a blank line between enum cases. Fix #606. * Bump to 1.0.1.
1 parent 4e45253 commit 2c216d8

File tree

9 files changed

+194
-19
lines changed

9 files changed

+194
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1.0.1
22

33
* Ensure space between `-` and `--` (#170).
4+
* Preserve a blank line between enum cases (#606).
45

56
# 1.0.0
67

bin/format.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'package:dart_style/src/io.dart';
1414
import 'package:dart_style/src/source_code.dart';
1515

1616
// Note: The following line of code is modified by tool/grind.dart.
17-
const version = "1.0.0";
17+
const version = "1.0.1";
1818

1919
void main(List<String> args) {
2020
var parser = new ArgParser(allowTrailingOptions: true);

lib/src/argument_list_visitor.dart

+5-7
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,16 @@ class ArgumentListVisitor {
236236

237237
// Allow functions wrapped in dotted method calls like "a.b.c(() { ... })".
238238
if (expression is MethodInvocation) {
239-
var invocation = expression as MethodInvocation;
240-
if (!_isValidWrappingTarget(invocation.target)) return false;
241-
if (invocation.argumentList.arguments.length != 1) return false;
239+
if (!_isValidWrappingTarget(expression.target)) return false;
240+
if (expression.argumentList.arguments.length != 1) return false;
242241

243-
return _isBlockFunction(invocation.argumentList.arguments.single);
242+
return _isBlockFunction(expression.argumentList.arguments.single);
244243
}
245244

246245
if (expression is InstanceCreationExpression) {
247-
var creation = expression as InstanceCreationExpression;
248-
if (creation.argumentList.arguments.length != 1) return false;
246+
if (expression.argumentList.arguments.length != 1) return false;
249247

250-
return _isBlockFunction(creation.argumentList.arguments.single);
248+
return _isBlockFunction(expression.argumentList.arguments.single);
251249
}
252250

253251
// Allow immediately-invoked functions like "() { ... }()".

lib/src/chunk_builder.dart

+11
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class ChunkBuilder {
101101
/// token pair.
102102
bool get needsToPreserveNewlines =>
103103
_pendingWhitespace == Whitespace.oneOrTwoNewlines ||
104+
_pendingWhitespace == Whitespace.splitOrTwoNewlines ||
104105
_pendingWhitespace == Whitespace.splitOrNewline;
105106

106107
/// The number of characters of code that can fit in a single line.
@@ -309,6 +310,15 @@ class ChunkBuilder {
309310
}
310311
break;
311312

313+
case Whitespace.splitOrTwoNewlines:
314+
if (numLines > 1) {
315+
_pendingWhitespace = Whitespace.twoNewlines;
316+
} else {
317+
_pendingWhitespace = Whitespace.none;
318+
split(space: true);
319+
}
320+
break;
321+
312322
case Whitespace.oneOrTwoNewlines:
313323
if (numLines > 1) {
314324
_pendingWhitespace = Whitespace.twoNewlines;
@@ -604,6 +614,7 @@ class ChunkBuilder {
604614
break;
605615

606616
case Whitespace.splitOrNewline:
617+
case Whitespace.splitOrTwoNewlines:
607618
case Whitespace.oneOrTwoNewlines:
608619
// We should have pinned these down before getting here.
609620
assert(false);

lib/src/source_visitor.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ class SourceVisitor extends ThrowingAstVisitor {
842842
space();
843843

844844
_beginBody(node.leftBracket, space: true);
845-
visitCommaSeparatedNodes(node.constants, between: split);
845+
visitCommaSeparatedNodes(node.constants, between: splitOrTwoNewlines);
846846

847847
// If there is a trailing comma, always force the constants to split.
848848
if (node.constants.last.endToken.next.type == TokenType.COMMA) {
@@ -2497,6 +2497,13 @@ class SourceVisitor extends ThrowingAstVisitor {
24972497
builder.writeWhitespace(Whitespace.splitOrNewline);
24982498
}
24992499

2500+
/// Allow either a single split or newline to be emitted before the next
2501+
/// non-whitespace token based on whether a newline exists in the source
2502+
/// between the last token and the next one.
2503+
void splitOrTwoNewlines() {
2504+
builder.writeWhitespace(Whitespace.splitOrTwoNewlines);
2505+
}
2506+
25002507
/// Allow either one or two newlines to be emitted before the next
25012508
/// non-whitespace token based on whether more than one newline exists in the
25022509
/// source between the last token and the next one.

lib/src/whitespace.dart

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class Whitespace {
5454
/// less prescriptive over the user's whitespace.
5555
static const splitOrNewline = const Whitespace._("splitOrNewline");
5656

57+
/// A split or blank line (two newlines) should be output based on whether
58+
/// the current token is on the same line as the previous one or not.
59+
///
60+
/// This is used between enum cases, which will collapse if possible but
61+
/// also allow a blank line to be preserved between cases.
62+
///
63+
/// In general, we like to avoid using this because it makes the formatter
64+
/// less prescriptive over the user's whitespace.
65+
static const splitOrTwoNewlines = const Whitespace._("splitOrTwoNewlines");
66+
5767
/// One or two newlines should be output based on how many newlines are
5868
/// present between the next token and the previous one.
5969
///

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dart_style
22
# Note: See tool/grind.dart for how to bump the version.
3-
version: 1.0.1-dev
3+
version: 1.0.1
44
author: Dart Team <[email protected]>
55
description: Opinionated, automatic Dart source code formatter.
66
homepage: https://github.com/dart-lang/dart_style

test/regression/0600/0606.unit

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
>>>
2+
enum ErrorKind {
3+
AbstractNotSync,
4+
AsciiControlCharacter,
5+
AsyncAsIdentifier,
6+
AwaitAsIdentifier,
7+
AwaitForNotAsync,
8+
AwaitNotAsync,
9+
BuiltInIdentifierAsType,
10+
BuiltInIdentifierInDeclaration,
11+
EmptyNamedParameterList,
12+
EmptyOptionalParameterList,
13+
Encoding,
14+
ExpectedBlockToSkip,
15+
ExpectedBody,
16+
ExpectedButGot,
17+
ExpectedClassBody,
18+
19+
/// This error code can be used to support non-compliant (with respect to
20+
/// Dart Language Specification) Dart VM native clauses. See
21+
/// [dart_vm_native.dart].
22+
ExpectedClassBodyToSkip,
23+
24+
ExpectedDeclaration,
25+
ExpectedExpression,
26+
ExpectedFunctionBody,
27+
ExpectedHexDigit,
28+
ExpectedIdentifier,
29+
ExpectedOpenParens,
30+
ExpectedString,
31+
ExpectedType,
32+
ExtraneousModifier,
33+
ExtraneousModifierReplace,
34+
FactoryNotSync,
35+
GeneratorReturnsValue,
36+
InvalidAwaitFor,
37+
InvalidInlineFunctionType,
38+
InvalidSyncModifier,
39+
InvalidVoid,
40+
MissingExponent,
41+
NonAsciiIdentifier,
42+
NonAsciiWhitespace,
43+
OnlyTry,
44+
PositionalParameterWithEquals,
45+
RequiredParameterWithDefault,
46+
SetterNotSync,
47+
StackOverflow,
48+
UnexpectedDollarInString,
49+
UnexpectedToken,
50+
UnmatchedToken,
51+
UnsupportedPrefixPlus,
52+
UnterminatedComment,
53+
UnterminatedString,
54+
UnterminatedToken,
55+
YieldAsIdentifier,
56+
YieldNotGenerator,
57+
58+
Unspecified,
59+
}
60+
<<<
61+
enum ErrorKind {
62+
AbstractNotSync,
63+
AsciiControlCharacter,
64+
AsyncAsIdentifier,
65+
AwaitAsIdentifier,
66+
AwaitForNotAsync,
67+
AwaitNotAsync,
68+
BuiltInIdentifierAsType,
69+
BuiltInIdentifierInDeclaration,
70+
EmptyNamedParameterList,
71+
EmptyOptionalParameterList,
72+
Encoding,
73+
ExpectedBlockToSkip,
74+
ExpectedBody,
75+
ExpectedButGot,
76+
ExpectedClassBody,
77+
78+
/// This error code can be used to support non-compliant (with respect to
79+
/// Dart Language Specification) Dart VM native clauses. See
80+
/// [dart_vm_native.dart].
81+
ExpectedClassBodyToSkip,
82+
83+
ExpectedDeclaration,
84+
ExpectedExpression,
85+
ExpectedFunctionBody,
86+
ExpectedHexDigit,
87+
ExpectedIdentifier,
88+
ExpectedOpenParens,
89+
ExpectedString,
90+
ExpectedType,
91+
ExtraneousModifier,
92+
ExtraneousModifierReplace,
93+
FactoryNotSync,
94+
GeneratorReturnsValue,
95+
InvalidAwaitFor,
96+
InvalidInlineFunctionType,
97+
InvalidSyncModifier,
98+
InvalidVoid,
99+
MissingExponent,
100+
NonAsciiIdentifier,
101+
NonAsciiWhitespace,
102+
OnlyTry,
103+
PositionalParameterWithEquals,
104+
RequiredParameterWithDefault,
105+
SetterNotSync,
106+
StackOverflow,
107+
UnexpectedDollarInString,
108+
UnexpectedToken,
109+
UnmatchedToken,
110+
UnsupportedPrefixPlus,
111+
UnterminatedComment,
112+
UnterminatedString,
113+
UnterminatedToken,
114+
YieldAsIdentifier,
115+
YieldNotGenerator,
116+
117+
Unspecified,
118+
}

test/whitespace/enums.unit

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
40 columns |
22
>>> single
3-
enum Unity {ONE}
3+
enum Unity {one}
44
<<<
5-
enum Unity { ONE }
5+
enum Unity { one }
66
>>> single line
7-
enum Primate{BONOBO,CHIMP,GORILLA}
7+
enum Primate{bonobo,chimp,gorilla}
88
<<<
9-
enum Primate { BONOBO, CHIMP, GORILLA }
9+
enum Primate { bonobo, chimp, gorilla }
1010
>>> trailing comma always splits
11-
enum Primate{BONOBO,CHIMP,}
11+
enum Primate{bonobo,chimp,}
1212
<<<
1313
enum Primate {
14-
BONOBO,
15-
CHIMP,
14+
bonobo,
15+
chimp,
1616
}
1717
>>> metadata
1818
@Awesome @Fierce("really")
19-
enum Primate{BONOBO,CHIMP,GORILLA}
19+
enum Primate{bonobo,chimp,gorilla}
2020
<<<
2121
@Awesome
2222
@Fierce("really")
23-
enum Primate { BONOBO, CHIMP, GORILLA }
23+
enum Primate { bonobo, chimp, gorilla }
24+
>>> preserve one blank line between enums
25+
enum Primate {
26+
27+
28+
bonobo,
29+
30+
31+
chimp,
32+
33+
34+
35+
gorilla
36+
37+
}
38+
<<<
39+
enum Primate {
40+
bonobo,
41+
42+
chimp,
43+
44+
gorilla
45+
}
46+
>>> do not preserve single newline
47+
enum Primate {
48+
bonobo,
49+
chimp,
50+
gorilla
51+
}
52+
<<<
53+
enum Primate { bonobo, chimp, gorilla }

0 commit comments

Comments
 (0)