Skip to content

Format enhanced enums. #1096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 2.2.2

* Format named arguments anywhere (#1072).
* Format enhanced enums (#1075).

# 2.2.1

Expand Down
86 changes: 71 additions & 15 deletions lib/src/source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ class SourceVisitor extends ThrowingAstVisitor {
token(node.equals);
space();

visit(node.superclass2);
visit(node.superclass);

builder.startRule(CombinatorRule());
visit(node.withClause);
Expand Down Expand Up @@ -834,11 +834,12 @@ class SourceVisitor extends ThrowingAstVisitor {

var needsDouble = true;
for (var declaration in node.declarations) {
var hasClassBody = declaration is ClassDeclaration ||
var hasBody = declaration is ClassDeclaration ||
declaration is EnumDeclaration ||
declaration is ExtensionDeclaration;

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

if (needsDouble) {
twoNewlines();
Expand All @@ -851,8 +852,8 @@ class SourceVisitor extends ThrowingAstVisitor {
visit(declaration);

needsDouble = false;
if (hasClassBody) {
// Add a blank line after declarations with class-like bodies.
if (hasBody) {
// Add a blank line after types declarations with bodies.
needsDouble = true;
} else if (declaration is FunctionDeclaration) {
// Add a blank line after non-empty block functions.
Expand Down Expand Up @@ -1068,7 +1069,7 @@ class SourceVisitor extends ThrowingAstVisitor {

@override
void visitConstructorName(ConstructorName node) {
visit(node.type2);
visit(node.type);
token(node.period);
visit(node.name);
}
Expand Down Expand Up @@ -1165,25 +1166,80 @@ class SourceVisitor extends ThrowingAstVisitor {
void visitEnumConstantDeclaration(EnumConstantDeclaration node) {
visitMetadata(node.metadata);
visit(node.name);

var arguments = node.arguments;
if (arguments != null) {
builder.nestExpression();
visit(arguments.typeArguments);

var constructor = arguments.constructorSelector;
if (constructor != null) {
token(constructor.period);
visit(constructor.name);
}

visitArgumentList(arguments.argumentList, nestExpression: false);
builder.unnest();
}
}

@override
void visitEnumDeclaration(EnumDeclaration node) {
visitMetadata(node.metadata);

builder.nestExpression();
token(node.enumKeyword);
space();
visit(node.name);
visit(node.typeParameters);

builder.startRule(CombinatorRule());
visit(node.withClause);
visit(node.implementsClause);
builder.endRule();
space();

builder.unnest();

_beginBody(node.leftBracket, space: true);
visitCommaSeparatedNodes(node.constants, between: splitOrTwoNewlines);

// If there is a trailing comma, always force the constants to split.
if (hasCommaAfter(node.constants.last)) {
Token? trailingComma = _commaAfter(node.constants.last);
if (trailingComma != null) {
builder.forceRules();
}

// The ";" after the constants, which may occur after a trailing comma.
Token afterConstants = node.constants.last.endToken.next!;
Token? semicolon;
if (afterConstants.type == TokenType.SEMICOLON) {
semicolon = node.constants.last.endToken.next!;
} else if (trailingComma != null &&
trailingComma.next!.type == TokenType.SEMICOLON) {
semicolon = afterConstants.next!;
}

if (semicolon != null) {
// If there is both a trailing comma and a semicolon, move the semicolon
// to the next line. This doesn't look great but it's less bad than being
// next to the comma.
// TODO(rnystrom): If the formatter starts making non-whitespace changes
// like adding/removing trailing commas, then it should fix this too.
if (trailingComma != null) newline();

token(semicolon);

// Always split the constants if they end in ";", even if there aren't
// any members.
builder.forceRules();

// Put a blank line between the constants and members.
if (node.members.isNotEmpty) twoNewlines();
}

_visitMembers(node.members);

_endBody(node.rightBracket, space: true);
}

Expand Down Expand Up @@ -1373,7 +1429,7 @@ class SourceVisitor extends ThrowingAstVisitor {
soloSplit();
token(node.extendsKeyword);
space();
visit(node.superclass2);
visit(node.superclass);
}

@override
Expand Down Expand Up @@ -1999,7 +2055,7 @@ class SourceVisitor extends ThrowingAstVisitor {

@override
void visitImplementsClause(ImplementsClause node) {
_visitCombinator(node.implementsKeyword, node.interfaces2);
_visitCombinator(node.implementsKeyword, node.interfaces);
}

@override
Expand Down Expand Up @@ -2264,18 +2320,18 @@ class SourceVisitor extends ThrowingAstVisitor {
// If there is only a single superclass constraint, format it like an
// "extends" in a class.
var onClause = node.onClause;
if (onClause != null && onClause.superclassConstraints2.length == 1) {
if (onClause != null && onClause.superclassConstraints.length == 1) {
soloSplit();
token(onClause.onKeyword);
space();
visit(onClause.superclassConstraints2.single);
visit(onClause.superclassConstraints.single);
}

builder.startRule(CombinatorRule());

// If there are multiple superclass constraints, format them like the
// "implements" clause.
if (onClause != null && onClause.superclassConstraints2.length > 1) {
if (onClause != null && onClause.superclassConstraints.length > 1) {
visit(onClause);
}

Expand Down Expand Up @@ -2326,7 +2382,7 @@ class SourceVisitor extends ThrowingAstVisitor {

@override
void visitOnClause(OnClause node) {
_visitCombinator(node.onKeyword, node.superclassConstraints2);
_visitCombinator(node.onKeyword, node.superclassConstraints);
}

@override
Expand Down Expand Up @@ -2730,7 +2786,7 @@ class SourceVisitor extends ThrowingAstVisitor {

@override
void visitWithClause(WithClause node) {
_visitCombinator(node.withKeyword, node.mixinTypes2);
_visitCombinator(node.withKeyword, node.mixinTypes);
}

@override
Expand Down
22 changes: 11 additions & 11 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ packages:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "34.0.0"
version: "36.0.0"
analyzer:
dependency: "direct main"
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.0"
version: "3.3.1"
args:
dependency: "direct main"
description:
Expand Down Expand Up @@ -70,7 +70,7 @@ packages:
name: coverage
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
version: "1.1.0"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -112,7 +112,7 @@ packages:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
version: "3.2.0"
http_parser:
dependency: transitive
description:
Expand All @@ -133,7 +133,7 @@ packages:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
version: "0.6.4"
lints:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -189,7 +189,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
pool:
dependency: transitive
description:
Expand Down Expand Up @@ -252,7 +252,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -287,7 +287,7 @@ packages:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.20.0"
version: "1.20.1"
test_api:
dependency: transitive
description:
Expand All @@ -301,7 +301,7 @@ packages:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.10"
version: "0.4.11"
test_descriptor:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -329,7 +329,7 @@ packages:
name: vm_service
url: "https://pub.dartlang.org"
source: hosted
version: "7.5.0"
version: "8.2.0"
watcher:
dependency: transitive
description:
Expand Down Expand Up @@ -359,4 +359,4 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.14.0 <3.0.0"
dart: ">=2.16.0-100.0.dev <3.0.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
sdk: ">=2.12.0-0 <3.0.0"

dependencies:
analyzer: ">=3.2.0 <4.0.0"
analyzer: ^3.3.1
args: ">=1.0.0 <3.0.0"
path: ^1.0.0
pub_semver: ">=1.4.4 <3.0.0"
Expand Down
21 changes: 21 additions & 0 deletions test/comments/enums.unit
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,25 @@ enum A {
enum A {
// comment
B
}
>>> ensure blank line above doc comments
enum Foo {/// doc
a,/// doc
b;/// doc
var x = 1;
/// doc
void y() {}}
<<<
enum Foo {
/// doc
a,

/// doc
b;

/// doc
var x = 1;

/// doc
void y() {}
}
Loading