Skip to content

Commit 12d6907

Browse files
committed
Format enhanced enums.
Fix #1075.
1 parent 4de83d3 commit 12d6907

File tree

7 files changed

+345
-19
lines changed

7 files changed

+345
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 2.2.2
22

33
* Format named arguments anywhere (#1072).
4+
* Format enhanced enums (#1075).
45

56
# 2.2.1
67

lib/src/source_visitor.dart

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,11 +834,12 @@ class SourceVisitor extends ThrowingAstVisitor {
834834

835835
var needsDouble = true;
836836
for (var declaration in node.declarations) {
837-
var hasClassBody = declaration is ClassDeclaration ||
837+
var hasBody = declaration is ClassDeclaration ||
838+
declaration is EnumDeclaration ||
838839
declaration is ExtensionDeclaration;
839840

840-
// Add a blank line before declarations with class-like bodies.
841-
if (hasClassBody) needsDouble = true;
841+
// Add a blank line before types with bodies.
842+
if (hasBody) needsDouble = true;
842843

843844
if (needsDouble) {
844845
twoNewlines();
@@ -851,8 +852,8 @@ class SourceVisitor extends ThrowingAstVisitor {
851852
visit(declaration);
852853

853854
needsDouble = false;
854-
if (hasClassBody) {
855-
// Add a blank line after declarations with class-like bodies.
855+
if (hasBody) {
856+
// Add a blank line after types declarations with bodies.
856857
needsDouble = true;
857858
} else if (declaration is FunctionDeclaration) {
858859
// Add a blank line after non-empty block functions.
@@ -1165,25 +1166,73 @@ class SourceVisitor extends ThrowingAstVisitor {
11651166
void visitEnumConstantDeclaration(EnumConstantDeclaration node) {
11661167
visitMetadata(node.metadata);
11671168
visit(node.name);
1169+
1170+
var arguments = node.arguments;
1171+
if (arguments != null) {
1172+
builder.nestExpression();
1173+
visit(arguments.typeArguments);
1174+
visitArgumentList(arguments.argumentList, nestExpression: false);
1175+
builder.unnest();
1176+
}
11681177
}
11691178

11701179
@override
11711180
void visitEnumDeclaration(EnumDeclaration node) {
11721181
visitMetadata(node.metadata);
11731182

1183+
builder.nestExpression();
11741184
token(node.enumKeyword);
11751185
space();
11761186
visit(node.name);
1187+
visit(node.typeParameters);
1188+
1189+
builder.startRule(CombinatorRule());
1190+
visit(node.withClause);
1191+
visit(node.implementsClause);
1192+
builder.endRule();
11771193
space();
11781194

1195+
builder.unnest();
1196+
11791197
_beginBody(node.leftBracket, space: true);
11801198
visitCommaSeparatedNodes(node.constants, between: splitOrTwoNewlines);
11811199

11821200
// If there is a trailing comma, always force the constants to split.
1183-
if (hasCommaAfter(node.constants.last)) {
1201+
Token? trailingComma = _commaAfter(node.constants.last);
1202+
if (trailingComma != null) {
11841203
builder.forceRules();
11851204
}
11861205

1206+
// The ";" after the constants, which may occur after a trailing comma.
1207+
Token afterConstants = node.constants.last.endToken.next!;
1208+
Token? semicolon;
1209+
if (afterConstants.type == TokenType.SEMICOLON) {
1210+
semicolon = node.constants.last.endToken.next!;
1211+
} else if (trailingComma != null &&
1212+
trailingComma.next!.type == TokenType.SEMICOLON) {
1213+
semicolon = afterConstants.next!;
1214+
}
1215+
1216+
if (semicolon != null) {
1217+
// If there is both a trailing comma and a semicolon, move the semicolon
1218+
// to the next line. This doesn't look great but it's less bad than being
1219+
// next to the comma.
1220+
// TODO(rnystrom): If the formatter starts making non-whitespace changes
1221+
// like adding/removing trailing commas, then it should fix this too.
1222+
if (trailingComma != null) newline();
1223+
1224+
token(semicolon);
1225+
1226+
// Always split the constants if they end in ";", even if there aren't
1227+
// any members.
1228+
builder.forceRules();
1229+
1230+
// Put a blank line between the constants and members.
1231+
if (node.members.isNotEmpty) twoNewlines();
1232+
}
1233+
1234+
_visitMembers(node.members);
1235+
11871236
_endBody(node.rightBracket, space: true);
11881237
}
11891238

pubspec.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ packages:
77
name: _fe_analyzer_shared
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "34.0.0"
10+
version: "36.0.0"
1111
analyzer:
1212
dependency: "direct main"
1313
description:
1414
name: analyzer
1515
url: "https://pub.dartlang.org"
1616
source: hosted
17-
version: "3.2.0"
17+
version: "3.3.1"
1818
args:
1919
dependency: "direct main"
2020
description:
@@ -70,7 +70,7 @@ packages:
7070
name: coverage
7171
url: "https://pub.dartlang.org"
7272
source: hosted
73-
version: "1.0.3"
73+
version: "1.1.0"
7474
crypto:
7575
dependency: transitive
7676
description:
@@ -112,7 +112,7 @@ packages:
112112
name: http_multi_server
113113
url: "https://pub.dartlang.org"
114114
source: hosted
115-
version: "3.0.1"
115+
version: "3.2.0"
116116
http_parser:
117117
dependency: transitive
118118
description:
@@ -133,7 +133,7 @@ packages:
133133
name: js
134134
url: "https://pub.dartlang.org"
135135
source: hosted
136-
version: "0.6.3"
136+
version: "0.6.4"
137137
lints:
138138
dependency: "direct dev"
139139
description:
@@ -189,7 +189,7 @@ packages:
189189
name: path
190190
url: "https://pub.dartlang.org"
191191
source: hosted
192-
version: "1.8.0"
192+
version: "1.8.1"
193193
pool:
194194
dependency: transitive
195195
description:
@@ -252,7 +252,7 @@ packages:
252252
name: source_span
253253
url: "https://pub.dartlang.org"
254254
source: hosted
255-
version: "1.8.1"
255+
version: "1.8.2"
256256
stack_trace:
257257
dependency: transitive
258258
description:
@@ -287,7 +287,7 @@ packages:
287287
name: test
288288
url: "https://pub.dartlang.org"
289289
source: hosted
290-
version: "1.20.0"
290+
version: "1.20.1"
291291
test_api:
292292
dependency: transitive
293293
description:
@@ -301,7 +301,7 @@ packages:
301301
name: test_core
302302
url: "https://pub.dartlang.org"
303303
source: hosted
304-
version: "0.4.10"
304+
version: "0.4.11"
305305
test_descriptor:
306306
dependency: "direct dev"
307307
description:
@@ -329,7 +329,7 @@ packages:
329329
name: vm_service
330330
url: "https://pub.dartlang.org"
331331
source: hosted
332-
version: "7.5.0"
332+
version: "8.2.0"
333333
watcher:
334334
dependency: transitive
335335
description:
@@ -359,4 +359,4 @@ packages:
359359
source: hosted
360360
version: "3.1.0"
361361
sdks:
362-
dart: ">=2.14.0 <3.0.0"
362+
dart: ">=2.16.0-100.0.dev <3.0.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ environment:
99
sdk: ">=2.12.0-0 <3.0.0"
1010

1111
dependencies:
12-
analyzer: ">=3.2.0 <4.0.0"
12+
analyzer: ^3.3.1
1313
args: ">=1.0.0 <3.0.0"
1414
path: ^1.0.0
1515
pub_semver: ">=1.4.4 <3.0.0"

test/comments/enums.unit

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ enum A {
5151
enum A {
5252
// comment
5353
B
54+
}
55+
>>> ensure blank line above doc comments
56+
enum Foo {/// doc
57+
a,/// doc
58+
b;/// doc
59+
var x = 1;
60+
/// doc
61+
void y() {}}
62+
<<<
63+
enum Foo {
64+
/// doc
65+
a,
66+
67+
/// doc
68+
b;
69+
70+
/// doc
71+
var x = 1;
72+
73+
/// doc
74+
void y() {}
5475
}

test/splitting/enums.unit

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,30 @@ enum Primate {
1616
CHIMP,
1717
GORILLA,
1818
ORANGUTAN,
19+
}
20+
>>> wrapped argument lists in values
21+
enum Args {
22+
firstEnumValue(longArgument,anotherArgument),
23+
secondEnumValue(longArgument,anotherArgument,aThirdArgument,theLastOne),
24+
thirdGenericOne<FirstTypeArgument,
25+
SecondTypeArgument<NestedTypeArgument,AnotherNestedTypeArgument>,
26+
LastTypeArgument>(namedArgument: firstValue,anotherNamed:argumentValue)
27+
}
28+
<<<
29+
enum Args {
30+
firstEnumValue(
31+
longArgument, anotherArgument),
32+
secondEnumValue(
33+
longArgument,
34+
anotherArgument,
35+
aThirdArgument,
36+
theLastOne),
37+
thirdGenericOne<
38+
FirstTypeArgument,
39+
SecondTypeArgument<
40+
NestedTypeArgument,
41+
AnotherNestedTypeArgument>,
42+
LastTypeArgument>(
43+
namedArgument: firstValue,
44+
anotherNamed: argumentValue)
1945
}

0 commit comments

Comments
 (0)