Skip to content
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

Format fields and methods. #1313

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 26 additions & 16 deletions lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,12 @@ class AstNodeVisitor extends ThrowingAstVisitor<void>

@override
void visitFieldDeclaration(FieldDeclaration node) {
throw UnimplementedError();
modifier(node.externalKeyword);
modifier(node.staticKeyword);
munificent marked this conversation as resolved.
Show resolved Hide resolved
modifier(node.abstractKeyword);
modifier(node.covariantKeyword);
visit(node.fields);
token(node.semicolon);
}

@override
Expand Down Expand Up @@ -556,19 +561,14 @@ class AstNodeVisitor extends ThrowingAstVisitor<void>

@override
void visitFunctionDeclaration(FunctionDeclaration node) {
modifier(node.externalKeyword);

Piece? returnType;
if (node.returnType case var returnTypeNode?) {
visit(returnTypeNode);
returnType = pieces.split();
}

// TODO(tall): Get or set keywords for getters and setters.
if (node.propertyKeyword != null) throw UnimplementedError();
token(node.name);

finishFunction(returnType, node.functionExpression);
createFunction(
externalKeyword: node.externalKeyword,
returnType: node.returnType,
propertyKeyword: node.propertyKeyword,
name: node.name,
typeParameters: node.functionExpression.typeParameters,
parameters: node.functionExpression.parameters,
body: node.functionExpression.body);
}

@override
Expand All @@ -578,7 +578,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<void>

@override
void visitFunctionExpression(FunctionExpression node) {
finishFunction(null, node);
finishFunction(null, node.typeParameters, node.parameters, node.body);
}

@override
Expand Down Expand Up @@ -786,7 +786,16 @@ class AstNodeVisitor extends ThrowingAstVisitor<void>

@override
void visitMethodDeclaration(MethodDeclaration node) {
throw UnimplementedError();
createFunction(
externalKeyword: node.externalKeyword,
modifierKeyword: node.modifierKeyword,
returnType: node.returnType,
operatorKeyword: node.operatorKeyword,
propertyKeyword: node.propertyKeyword,
name: node.name,
typeParameters: node.typeParameters,
parameters: node.parameters,
body: node.body);
}

@override
Expand Down Expand Up @@ -1209,6 +1218,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<void>

@override
void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
modifier(node.externalKeyword);
visit(node.variables);
token(node.semicolon);
}
Expand Down
61 changes: 47 additions & 14 deletions lib/src/front_end/piece_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,39 @@ mixin PieceFactory implements CommentWriter {
}
}

/// Creates a function, method, getter, or setter declaration.
///
/// If [modifierKeyword] is given, it should be the `static` or `abstract`
/// modifier on a method declaration. If [operatorKeyword] is given, it
/// should be the `operator` keyword on an operator declaration. If
/// [propertyKeyword] is given, it should be the `get` or `set` keyword on a
/// getter or setter declaration.
void createFunction(
{Token? externalKeyword,
Token? modifierKeyword,
AstNode? returnType,
Token? operatorKeyword,
Token? propertyKeyword,
required Token name,
TypeParameterList? typeParameters,
FormalParameterList? parameters,
required FunctionBody body}) {
modifier(externalKeyword);
modifier(modifierKeyword);

Piece? returnTypePiece;
if (returnType != null) {
visit(returnType);
returnTypePiece = pieces.split();
}

modifier(operatorKeyword);
modifier(propertyKeyword);
token(name);

finishFunction(returnTypePiece, typeParameters, parameters, body);
}

/// Creates a function type or function-typed formal.
void createFunctionType(
TypeAnnotation? returnType,
Expand Down Expand Up @@ -424,7 +457,6 @@ mixin PieceFactory implements CommentWriter {
List<AstNode> members,
Token rightBracket) {
if (metadata.isNotEmpty) throw UnimplementedError('Type metadata.');
if (members.isNotEmpty) throw UnimplementedError('Type members.');

modifiers.forEach(modifier);
token(keyword);
Expand Down Expand Up @@ -483,7 +515,7 @@ mixin PieceFactory implements CommentWriter {
leftBracket: leftBracket,
elements,
rightBracket: rightBracket,
style: const ListStyle(commas: Commas.nonTrailing, splitCost: 2));
style: const ListStyle(commas: Commas.nonTrailing, splitCost: 3));
munificent marked this conversation as resolved.
Show resolved Hide resolved
}

/// Writes the parts of a formal parameter shared by all formal parameter
Expand All @@ -492,7 +524,7 @@ mixin PieceFactory implements CommentWriter {
if (parameter.metadata.isNotEmpty) throw UnimplementedError();

modifier(parameter.requiredKeyword);
if (parameter.covariantKeyword != null) throw UnimplementedError();
modifier(parameter.covariantKeyword);
}

/// Handles the `async`, `sync*`, or `async*` modifiers on a function body.
Expand Down Expand Up @@ -565,24 +597,25 @@ mixin PieceFactory implements CommentWriter {
/// Finishes writing a named function declaration or anonymous function
/// expression after the return type (if any) and name (if any) has been
/// written.
void finishFunction(Piece? returnType, FunctionExpression function) {
visit(function.typeParameters);
visit(function.parameters);
void finishFunction(Piece? returnType, TypeParameterList? typeParameters,
FormalParameterList? parameters, FunctionBody body) {
visit(typeParameters);
visit(parameters);

Piece parameters;
Piece? body;
if (function.body case EmptyFunctionBody body) {
Piece parametersPiece;
Piece? bodyPiece;
if (body is EmptyFunctionBody) {
// If the body is just `;`, then don't allow a space or split before the
// semicolon by making it part of the parameters piece.
token(body.semicolon);
parameters = pieces.split();
parametersPiece = pieces.split();
} else {
parameters = pieces.split();
visit(function.body);
body = pieces.take();
parametersPiece = pieces.split();
visit(body);
bodyPiece = pieces.take();
}

pieces.give(FunctionPiece(returnType, parameters, body));
pieces.give(FunctionPiece(returnType, parametersPiece, bodyPiece));
}

/// Writes an optional modifier that precedes other code.
Expand Down
9 changes: 6 additions & 3 deletions lib/src/piece/function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'piece.dart';
///
/// Handles splitting between the return type and the rest of the function.
class FunctionPiece extends Piece {
static const _splitAfterReturnType = State(1, cost: 2);

/// The return type annotation, if any.
final Piece? _returnType;

Expand All @@ -22,20 +24,21 @@ class FunctionPiece extends Piece {
FunctionPiece(this._returnType, this._signature, [this._body]);

@override
List<State> get additionalStates => [if (_returnType != null) State.split];
List<State> get additionalStates =>
[if (_returnType != null) _splitAfterReturnType];

@override
void format(CodeWriter writer, State state) {
if (_returnType case var returnType?) {
// A split inside the return type forces splitting after the return type.
writer.setAllowNewlines(state == State.split);
writer.setAllowNewlines(state == _splitAfterReturnType);

writer.format(returnType);

// A split in the type parameters or parameters does not force splitting
// after the return type.
writer.setAllowNewlines(true);
writer.splitIf(state == State.split);
writer.splitIf(state == _splitAfterReturnType);
}

writer.format(_signature);
Expand Down
6 changes: 4 additions & 2 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ These tests are all run by `short_format_test.dart`.
The newer tall style tests are:

```
declaration/ - Typedef, class, enum, and extension declarations.
declaration/ - Typedef, class, enum, extension, mixin, and member declarations.
Includes constructors, getters, setters, methods, and fields,
but not functions and variables, which are in their own
directories below.
expression/ - Expressions.
invocation/ - Function and member invocations.
member/ - Constructor, method, field, getter, and setter declarations.
selection/ - Test preserving selection information.
statement/ - Statements.
top_level/ - Top-level directives.
Expand Down
84 changes: 84 additions & 0 deletions test/declaration/external.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
40 columns |
>>> Top-level variable.
external final a , b ;
external final Set < int > a , b ;
external var a ;
external List < int > a ;
<<<
external final a, b;
external final Set<int> a, b;
external var a;
external List<int> a;
>>> Static field.
class C {
external static final a , b ;
external static final Set < int > a , b ;
external static var a ;
external static List < int > a ;
}
<<<
class C {
external static final a, b;
external static final Set<int> a, b;
external static var a;
external static List<int> a;
}
>>> Instance field.
class C {
external final a , b ;
external final Set < String > a , b ;
external var a ;
external List < int > a ;
}
<<<
class C {
external final a, b;
external final Set<String> a, b;
external var a;
external List<int> a;
}
>>> Top-level function.
external int function();
external int get getter;
external void set setter(int value);
<<<
external int function();
external int get getter;
external void set setter(int value);
>>> Instance member.
class A {
external int function();
external int get getter;
external void set setter(int value);
}
<<<
class A {
external int function();
external int get getter;
external void set setter(int value);
}
>>> Static member.
class A {
external static int function();
external static int get getter;
external static void set setter(int value);
}
<<<
class A {
external static int function();
external static int get getter;
external static void set setter(
int value,
);
}
>>> Don't split after `external`.
class Foo {
external var soMuchSoVeryLongFieldNameHere;
external SuperLongTypeAnnotation field;
}
<<<
class Foo {
external var soMuchSoVeryLongFieldNameHere;
external SuperLongTypeAnnotation
field;
}
67 changes: 67 additions & 0 deletions test/declaration/field.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
40 columns |
>>> Covariant.
class Foo {
covariant var bar;
covariant int baz;
}
<<<
class Foo {
covariant var bar;
covariant int baz;
}
>>> Late.
class Foo {
static late final int i;
static late int i;
static late var i;
covariant late var i;
covariant late int i;
late final int i;
late int i;
late var i;
}
<<<
class Foo {
static late final int i;
static late int i;
static late var i;
covariant late var i;
covariant late int i;
late final int i;
late int i;
late var i;
}
>>> Abstract.
class Foo {
abstract covariant var a , b ;
abstract final int c;
abstract int i;
}
<<<
class Foo {
abstract covariant var a, b;
abstract final int c;
abstract int i;
}
>>> Don't split after `covariant`.
class Foo {
covariant var soMuchSoVeryLongFieldNameHere;
covariant VeryLongTypeAnnotation field;
}
<<<
class Foo {
covariant var soMuchSoVeryLongFieldNameHere;
covariant VeryLongTypeAnnotation
field;
}
>>> Don't split after `abstract`.
class Foo {
abstract var soMuchSoVeryLongFieldNameHere;
abstract SuperLongTypeAnnotation field;
}
<<<
class Foo {
abstract var soMuchSoVeryLongFieldNameHere;
abstract SuperLongTypeAnnotation
field;
}
Loading