Skip to content

606 enum #608

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 2 commits into from
Mar 24, 2017
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 @@
# 1.0.1

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

# 1.0.0

Expand Down
2 changes: 1 addition & 1 deletion bin/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:dart_style/src/io.dart';
import 'package:dart_style/src/source_code.dart';

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

void main(List<String> args) {
var parser = new ArgParser(allowTrailingOptions: true);
Expand Down
12 changes: 5 additions & 7 deletions lib/src/argument_list_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,16 @@ class ArgumentListVisitor {

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

return _isBlockFunction(invocation.argumentList.arguments.single);
return _isBlockFunction(expression.argumentList.arguments.single);
}

if (expression is InstanceCreationExpression) {
var creation = expression as InstanceCreationExpression;
if (creation.argumentList.arguments.length != 1) return false;
if (expression.argumentList.arguments.length != 1) return false;

return _isBlockFunction(creation.argumentList.arguments.single);
return _isBlockFunction(expression.argumentList.arguments.single);
}

// Allow immediately-invoked functions like "() { ... }()".
Expand Down
11 changes: 11 additions & 0 deletions lib/src/chunk_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class ChunkBuilder {
/// token pair.
bool get needsToPreserveNewlines =>
_pendingWhitespace == Whitespace.oneOrTwoNewlines ||
_pendingWhitespace == Whitespace.splitOrTwoNewlines ||
_pendingWhitespace == Whitespace.splitOrNewline;

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

case Whitespace.splitOrTwoNewlines:
if (numLines > 1) {
_pendingWhitespace = Whitespace.twoNewlines;
} else {
_pendingWhitespace = Whitespace.none;
split(space: true);
}
break;

case Whitespace.oneOrTwoNewlines:
if (numLines > 1) {
_pendingWhitespace = Whitespace.twoNewlines;
Expand Down Expand Up @@ -604,6 +614,7 @@ class ChunkBuilder {
break;

case Whitespace.splitOrNewline:
case Whitespace.splitOrTwoNewlines:
case Whitespace.oneOrTwoNewlines:
// We should have pinned these down before getting here.
assert(false);
Expand Down
9 changes: 8 additions & 1 deletion lib/src/source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ class SourceVisitor extends ThrowingAstVisitor {
space();

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

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

/// Allow either a single split or newline to be emitted before the next
/// non-whitespace token based on whether a newline exists in the source
/// between the last token and the next one.
void splitOrTwoNewlines() {
builder.writeWhitespace(Whitespace.splitOrTwoNewlines);
}

/// Allow either one or two newlines to be emitted before the next
/// non-whitespace token based on whether more than one newline exists in the
/// source between the last token and the next one.
Expand Down
10 changes: 10 additions & 0 deletions lib/src/whitespace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class Whitespace {
/// less prescriptive over the user's whitespace.
static const splitOrNewline = const Whitespace._("splitOrNewline");

/// A split or blank line (two newlines) should be output based on whether
/// the current token is on the same line as the previous one or not.
///
/// This is used between enum cases, which will collapse if possible but
/// also allow a blank line to be preserved between cases.
///
/// In general, we like to avoid using this because it makes the formatter
/// less prescriptive over the user's whitespace.
static const splitOrTwoNewlines = const Whitespace._("splitOrTwoNewlines");

/// One or two newlines should be output based on how many newlines are
/// present between the next token and the previous one.
///
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_style
# Note: See tool/grind.dart for how to bump the version.
version: 1.0.1-dev
version: 1.0.1
author: Dart Team <[email protected]>
description: Opinionated, automatic Dart source code formatter.
homepage: https://github.com/dart-lang/dart_style
Expand Down
118 changes: 118 additions & 0 deletions test/regression/0600/0606.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
>>>
enum ErrorKind {
AbstractNotSync,
AsciiControlCharacter,
AsyncAsIdentifier,
AwaitAsIdentifier,
AwaitForNotAsync,
AwaitNotAsync,
BuiltInIdentifierAsType,
BuiltInIdentifierInDeclaration,
EmptyNamedParameterList,
EmptyOptionalParameterList,
Encoding,
ExpectedBlockToSkip,
ExpectedBody,
ExpectedButGot,
ExpectedClassBody,

/// This error code can be used to support non-compliant (with respect to
/// Dart Language Specification) Dart VM native clauses. See
/// [dart_vm_native.dart].
ExpectedClassBodyToSkip,

ExpectedDeclaration,
ExpectedExpression,
ExpectedFunctionBody,
ExpectedHexDigit,
ExpectedIdentifier,
ExpectedOpenParens,
ExpectedString,
ExpectedType,
ExtraneousModifier,
ExtraneousModifierReplace,
FactoryNotSync,
GeneratorReturnsValue,
InvalidAwaitFor,
InvalidInlineFunctionType,
InvalidSyncModifier,
InvalidVoid,
MissingExponent,
NonAsciiIdentifier,
NonAsciiWhitespace,
OnlyTry,
PositionalParameterWithEquals,
RequiredParameterWithDefault,
SetterNotSync,
StackOverflow,
UnexpectedDollarInString,
UnexpectedToken,
UnmatchedToken,
UnsupportedPrefixPlus,
UnterminatedComment,
UnterminatedString,
UnterminatedToken,
YieldAsIdentifier,
YieldNotGenerator,

Unspecified,
}
<<<
enum ErrorKind {
AbstractNotSync,
AsciiControlCharacter,
AsyncAsIdentifier,
AwaitAsIdentifier,
AwaitForNotAsync,
AwaitNotAsync,
BuiltInIdentifierAsType,
BuiltInIdentifierInDeclaration,
EmptyNamedParameterList,
EmptyOptionalParameterList,
Encoding,
ExpectedBlockToSkip,
ExpectedBody,
ExpectedButGot,
ExpectedClassBody,

/// This error code can be used to support non-compliant (with respect to
/// Dart Language Specification) Dart VM native clauses. See
/// [dart_vm_native.dart].
ExpectedClassBodyToSkip,

ExpectedDeclaration,
ExpectedExpression,
ExpectedFunctionBody,
ExpectedHexDigit,
ExpectedIdentifier,
ExpectedOpenParens,
ExpectedString,
ExpectedType,
ExtraneousModifier,
ExtraneousModifierReplace,
FactoryNotSync,
GeneratorReturnsValue,
InvalidAwaitFor,
InvalidInlineFunctionType,
InvalidSyncModifier,
InvalidVoid,
MissingExponent,
NonAsciiIdentifier,
NonAsciiWhitespace,
OnlyTry,
PositionalParameterWithEquals,
RequiredParameterWithDefault,
SetterNotSync,
StackOverflow,
UnexpectedDollarInString,
UnexpectedToken,
UnmatchedToken,
UnsupportedPrefixPlus,
UnterminatedComment,
UnterminatedString,
UnterminatedToken,
YieldAsIdentifier,
YieldNotGenerator,

Unspecified,
}
48 changes: 39 additions & 9 deletions test/whitespace/enums.unit
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
40 columns |
>>> single
enum Unity {ONE}
enum Unity {one}
<<<
enum Unity { ONE }
enum Unity { one }
>>> single line
enum Primate{BONOBO,CHIMP,GORILLA}
enum Primate{bonobo,chimp,gorilla}
<<<
enum Primate { BONOBO, CHIMP, GORILLA }
enum Primate { bonobo, chimp, gorilla }
>>> trailing comma always splits
enum Primate{BONOBO,CHIMP,}
enum Primate{bonobo,chimp,}
<<<
enum Primate {
BONOBO,
CHIMP,
bonobo,
chimp,
}
>>> metadata
@Awesome @Fierce("really")
enum Primate{BONOBO,CHIMP,GORILLA}
enum Primate{bonobo,chimp,gorilla}
<<<
@Awesome
@Fierce("really")
enum Primate { BONOBO, CHIMP, GORILLA }
enum Primate { bonobo, chimp, gorilla }
>>> preserve one blank line between enums
enum Primate {


bonobo,


chimp,



gorilla

}
<<<
enum Primate {
bonobo,

chimp,

gorilla
}
>>> do not preserve single newline
enum Primate {
bonobo,
chimp,
gorilla
}
<<<
enum Primate { bonobo, chimp, gorilla }