From 238407c212d64527298e1aa4594809fa412b491c Mon Sep 17 00:00:00 2001 From: Robert Nystrom Date: Mon, 26 Jul 2021 15:17:06 -0700 Subject: [PATCH 1/2] Format generic function references and constructor tear-offs. Fix #1028. --- CHANGELOG.md | 4 +++ lib/src/dart_formatter.dart | 1 + lib/src/source_visitor.dart | 6 +++++ pubspec.yaml | 2 +- test/splitting/mixed.stmt | 10 +++++++- test/splitting/type_arguments.stmt | 41 +++++++++++++++++++++++++++++- test/whitespace/expressions.stmt | 18 ++++++++++++- 7 files changed, 78 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1baf149..66d272ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.0.4-dev + +* Support generic function references and constructor tear-offs (#1028). + # 2.0.3 * Fix hang when reading from stdin (https://github.com/dart-lang/sdk/issues/46600). diff --git a/lib/src/dart_formatter.dart b/lib/src/dart_formatter.dart index 7f437a95..2510acec 100644 --- a/lib/src/dart_formatter.dart +++ b/lib/src/dart_formatter.dart @@ -89,6 +89,7 @@ class DartFormatter { var featureSet = FeatureSet.fromEnableFlags2( sdkLanguageVersion: Version(2, 13, 0), flags: [ + 'constructor-tearoffs', 'generic-metadata', 'nonfunction-type-aliases', 'triple-shift' diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart index d08f18fd..4a53ae60 100644 --- a/lib/src/source_visitor.dart +++ b/lib/src/source_visitor.dart @@ -1805,6 +1805,12 @@ class SourceVisitor extends ThrowingAstVisitor { builder.endSpan(); } + @override + void visitFunctionReference(FunctionReference node) { + visit(node.function); + visit(node.typeArguments); + } + @override void visitFunctionTypeAlias(FunctionTypeAlias node) { visitMetadata(node.metadata); diff --git a/pubspec.yaml b/pubspec.yaml index ab6ef206..2617db7a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: dart_style # Note: See tool/grind.dart for how to bump the version. -version: 2.0.3 +version: 2.0.4-dev description: >- Opinionated, automatic Dart source code formatter. Provides an API and a CLI tool. diff --git a/test/splitting/mixed.stmt b/test/splitting/mixed.stmt index 7d8dfff1..f14c4ef9 100644 --- a/test/splitting/mixed.stmt +++ b/test/splitting/mixed.stmt @@ -222,4 +222,12 @@ var longVariableName = identifierSoLongItWraps is SomeClassName; <<< var longVariableName = identifierSoLongItWraps - is SomeClassName; \ No newline at end of file + is SomeClassName; +>>> generic function reference nested inside expression +veryLongFunction(argument, ConstructorTearOff, argument); +<<< +veryLongFunction( + argument, + ConstructorTearOff, + argument); \ No newline at end of file diff --git a/test/splitting/type_arguments.stmt b/test/splitting/type_arguments.stmt index 96751533..77a2eb3a 100644 --- a/test/splitting/type_arguments.stmt +++ b/test/splitting/type_arguments.stmt @@ -94,4 +94,43 @@ new SomeClass< fifth, sixth, seventh, - eighth); \ No newline at end of file + eighth); +>>> generic instantiation all fit on one line +Foo; +<<< +Foo; +>>> generic instantiation split between args +LongClassName; +<<< +LongClassName; +>>> generic instantiation split before first if needed +LongClassName; +<<< +LongClassName< + FirstTypeArgumentIsTooLong, Second>; +>>> generic instantiation split in middle if fit in two lines +LongClassName; +<<< +LongClassName; +>>> generic instantiation split one per line if they don't fit in two lines +LongClassName; +<<< +LongClassName< + First, + Second, + Third, + Fourth, + Fifth, + Sixth, + Seventh, + Eighth>; +>>> generic instantiation indent nested type arguments +LongClassName, Eighth>; +<<< +LongClassName< + First, + Inner, + Eighth>; \ No newline at end of file diff --git a/test/whitespace/expressions.stmt b/test/whitespace/expressions.stmt index 1a138dcf..23502abb 100644 --- a/test/whitespace/expressions.stmt +++ b/test/whitespace/expressions.stmt @@ -170,4 +170,20 @@ obj?[foo]; >>> generic function expression var generic = < T,S >(){}; <<< -var generic = () {}; \ No newline at end of file +var generic = () {}; +>>> generic method instantiation +void main() => id < int > ; +<<< +void main() => id; +>>> generic method instantiation +void main() => id < int , String , bool > ; +<<< +void main() => id; +>>> generic constructor tear-off +var x = Class < int >; +<<< +var x = Class; +>>> generic name constructor tear-off +var x = Class < int > . named; +<<< +var x = Class.named; \ No newline at end of file From f5dd5bbb3064a414e4ce46b70d6bf23ad732cf2c Mon Sep 17 00:00:00 2001 From: Robert Nystrom Date: Mon, 26 Jul 2021 16:26:59 -0700 Subject: [PATCH 2/2] Bump the minor version. --- CHANGELOG.md | 2 +- pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d272ea..a0372471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 2.0.4-dev +# 2.1.0-dev * Support generic function references and constructor tear-offs (#1028). diff --git a/pubspec.yaml b/pubspec.yaml index 2617db7a..c796bc26 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: dart_style # Note: See tool/grind.dart for how to bump the version. -version: 2.0.4-dev +version: 2.1.0-dev description: >- Opinionated, automatic Dart source code formatter. Provides an API and a CLI tool.