Skip to content

Commit b268371

Browse files
authored
Format generic function references and constructor tear-offs. (#1047)
* Format generic function references and constructor tear-offs. Fix #1028. * Bump the minor version.
1 parent 06bfd19 commit b268371

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.0-dev
2+
3+
* Support generic function references and constructor tear-offs (#1028).
4+
15
# 2.0.3
26

37
* Fix hang when reading from stdin (https://github.com/dart-lang/sdk/issues/46600).

lib/src/dart_formatter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class DartFormatter {
8989
var featureSet = FeatureSet.fromEnableFlags2(
9090
sdkLanguageVersion: Version(2, 13, 0),
9191
flags: [
92+
'constructor-tearoffs',
9293
'generic-metadata',
9394
'nonfunction-type-aliases',
9495
'triple-shift'

lib/src/source_visitor.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,12 @@ class SourceVisitor extends ThrowingAstVisitor {
18051805
builder.endSpan();
18061806
}
18071807

1808+
@override
1809+
void visitFunctionReference(FunctionReference node) {
1810+
visit(node.function);
1811+
visit(node.typeArguments);
1812+
}
1813+
18081814
@override
18091815
void visitFunctionTypeAlias(FunctionTypeAlias node) {
18101816
visitMetadata(node.metadata);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dart_style
22
# Note: See tool/grind.dart for how to bump the version.
3-
version: 2.0.3
3+
version: 2.1.0-dev
44
description: >-
55
Opinionated, automatic Dart source code formatter.
66
Provides an API and a CLI tool.

test/splitting/mixed.stmt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,12 @@ var longVariableName = identifierSoLongItWraps is SomeClassName;
222222
<<<
223223
var longVariableName =
224224
identifierSoLongItWraps
225-
is SomeClassName;
225+
is SomeClassName;
226+
>>> generic function reference nested inside expression
227+
veryLongFunction(argument, ConstructorTearOff<First, Second, Third, Fourth>, argument);
228+
<<<
229+
veryLongFunction(
230+
argument,
231+
ConstructorTearOff<First, Second,
232+
Third, Fourth>,
233+
argument);

test/splitting/type_arguments.stmt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,43 @@ new SomeClass<
9494
fifth,
9595
sixth,
9696
seventh,
97-
eighth);
97+
eighth);
98+
>>> generic instantiation all fit on one line
99+
Foo<A,B,C,D>;
100+
<<<
101+
Foo<A, B, C, D>;
102+
>>> generic instantiation split between args
103+
LongClassName<First, Second, Third, Fourth>;
104+
<<<
105+
LongClassName<First, Second, Third,
106+
Fourth>;
107+
>>> generic instantiation split before first if needed
108+
LongClassName<FirstTypeArgumentIsTooLong, Second>;
109+
<<<
110+
LongClassName<
111+
FirstTypeArgumentIsTooLong, Second>;
112+
>>> generic instantiation split in middle if fit in two lines
113+
LongClassName<First, Second, Third, Fourth, Fifth, Sixth, Seventh>;
114+
<<<
115+
LongClassName<First, Second, Third,
116+
Fourth, Fifth, Sixth, Seventh>;
117+
>>> generic instantiation split one per line if they don't fit in two lines
118+
LongClassName<First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth>;
119+
<<<
120+
LongClassName<
121+
First,
122+
Second,
123+
Third,
124+
Fourth,
125+
Fifth,
126+
Sixth,
127+
Seventh,
128+
Eighth>;
129+
>>> generic instantiation indent nested type arguments
130+
LongClassName<First, Inner<Second, Third, Fourth, Fifth, Sixth, Seventh>, Eighth>;
131+
<<<
132+
LongClassName<
133+
First,
134+
Inner<Second, Third, Fourth, Fifth,
135+
Sixth, Seventh>,
136+
Eighth>;

test/whitespace/expressions.stmt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,20 @@ obj?[foo];
170170
>>> generic function expression
171171
var generic = < T,S >(){};
172172
<<<
173-
var generic = <T, S>() {};
173+
var generic = <T, S>() {};
174+
>>> generic method instantiation
175+
void main() => id < int > ;
176+
<<<
177+
void main() => id<int>;
178+
>>> generic method instantiation
179+
void main() => id < int , String , bool > ;
180+
<<<
181+
void main() => id<int, String, bool>;
182+
>>> generic constructor tear-off
183+
var x = Class < int >;
184+
<<<
185+
var x = Class<int>;
186+
>>> generic name constructor tear-off
187+
var x = Class < int > . named;
188+
<<<
189+
var x = Class<int>.named;

0 commit comments

Comments
 (0)