Format enums. Fix #120.
BUG=https://github.com/dart-lang/dart_style/issues/120
R=pquitslund@google.com
Review URL: https://chromiumcodereview.appspot.com//844653002
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8febba9..cdd3b36 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1 +1,3 @@
-*No changes yet!*
+# 0.1.1-dev
+
+* Support formatting enums.
\ No newline at end of file
diff --git a/lib/src/dart_formatter.dart b/lib/src/dart_formatter.dart
index 5b59faa..e7c5b5e 100644
--- a/lib/src/dart_formatter.dart
+++ b/lib/src/dart_formatter.dart
@@ -147,6 +147,7 @@
// Parse it.
var parser = new Parser(stringSource, errorListener);
parser.parseAsync = true;
+ parser.parseEnum = true;
var node;
if (source.isCompilationUnit) {
diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart
index 7906fe4..540735d 100644
--- a/lib/src/source_visitor.dart
+++ b/lib/src/source_visitor.dart
@@ -523,11 +523,34 @@
}
visitEnumConstantDeclaration(EnumConstantDeclaration node) {
- throw new UnimplementedError("Enum formatting is not implemented yet.");
+ visit(node.name);
}
visitEnumDeclaration(EnumDeclaration node) {
- throw new UnimplementedError("Enum formatting is not implemented yet.");
+ visitDeclarationMetadata(node.metadata);
+
+ token(node.keyword);
+ space();
+ visit(node.name);
+ space();
+ token(node.leftBracket);
+
+ _writer.indent();
+ _writer.startMultisplit();
+ _writer.multisplit(space: true);
+
+ visitCommaSeparatedNodes(node.constants, between: () {
+ _writer.multisplit(space: true);
+ });
+
+ // Trailing comma.
+ if (node.rightBracket.previous.lexeme == ",") {
+ token(node.rightBracket.previous);
+ }
+
+ _writer.unindent();
+ _writer.multisplit(space: true);
+ token(node.rightBracket);
}
visitExportDirective(ExportDirective node) {
diff --git a/pubspec.yaml b/pubspec.yaml
index 277d533..f9bbc40 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: dart_style
-version: 0.1.0
+version: 0.1.1-dev
author: Dart Team <misc@dartlang.org>
description: Opinionated, automatic Dart source code formatter.
homepage: https://github.com/dart-lang/dart_style
diff --git a/test/splitting/enums.unit b/test/splitting/enums.unit
new file mode 100644
index 0000000..d326e40
--- /dev/null
+++ b/test/splitting/enums.unit
@@ -0,0 +1,19 @@
+40 columns |
+>>> multiple lines
+enum Primate{BONOBO,CHIMP,GORILLA,ORANGUTAN}
+<<<
+enum Primate {
+ BONOBO,
+ CHIMP,
+ GORILLA,
+ ORANGUTAN
+}
+>>> multiple lines trailing comma
+enum Primate{BONOBO,CHIMP,GORILLA,ORANGUTAN,}
+<<<
+enum Primate {
+ BONOBO,
+ CHIMP,
+ GORILLA,
+ ORANGUTAN,
+}
\ No newline at end of file
diff --git a/test/whitespace/enums.unit b/test/whitespace/enums.unit
new file mode 100644
index 0000000..0d3cb7a
--- /dev/null
+++ b/test/whitespace/enums.unit
@@ -0,0 +1,20 @@
+40 columns |
+>>> single
+enum Unity {ONE}
+<<<
+enum Unity { ONE }
+>>> single line
+enum Primate{BONOBO,CHIMP,GORILLA}
+<<<
+enum Primate { BONOBO, CHIMP, GORILLA }
+>>> single line trailing comma
+enum Primate{BONOBO,CHIMP,}
+<<<
+enum Primate { BONOBO, CHIMP, }
+>>> metadata
+@Awesome @Fierce("really")
+enum Primate{BONOBO,CHIMP,GORILLA}
+<<<
+@Awesome
+@Fierce("really")
+enum Primate { BONOBO, CHIMP, GORILLA }
\ No newline at end of file