Skip to content

Commit e79b035

Browse files
committed
Move every in-body declaring constructor to a primary.
1 parent 7e2374c commit e79b035

File tree

14 files changed

+275
-299
lines changed

14 files changed

+275
-299
lines changed

lib/src/back_end/code.dart

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -132,50 +132,42 @@ final class GroupCode(
132132
}
133133

134134
/// A [Code] object for a newline followed by any leading indentation.
135-
final class _NewlineCode extends Code {
136-
this({
137-
/// Whether a blank line (two newlines) should be written.
138-
required final bool _blank,
139-
140-
/// The number of spaces of indentation after this newline.
141-
required final int _indent,
142-
});
143-
}
135+
final class _NewlineCode({
136+
/// Whether a blank line (two newlines) should be written.
137+
required final bool _blank,
138+
139+
/// The number of spaces of indentation after this newline.
140+
required final int _indent,
141+
}) extends Code;
144142

145143
/// A [Code] object for literal source text.
146-
final class _TextCode extends Code {
147-
this(final String _text);
148-
}
144+
final class _TextCode(final String _text) extends Code;
149145

150146
/// Marks the location of the beginning or end of a selection as occurring
151147
/// [_offset] characters past the point where this marker object appears in the
152148
/// list of [Code] objects.
153-
final class _MarkerCode extends Code {
154-
this(
155-
/// What kind of selection endpoint is being marked.
156-
final _Marker _marker,
157-
158-
/// The number of characters into the next [Code] object where the marker
159-
/// should appear in the resulting output.
160-
final int _offset,
161-
);
162-
}
163-
164-
final class _EnableFormattingCode extends Code {
165-
this(
166-
/// Whether this comment disables formatting (`format off`) or re-enables it
167-
/// (`format on`).
168-
final bool _enabled,
169-
170-
/// The number of code points from the beginning of the unformatted source
171-
/// where the unformatted code should begin or end.
172-
///
173-
/// If this piece is for `// dart format off`, then the offset is just past
174-
/// the `off`. If this piece is for `// dart format on`, it points to just
175-
/// before `//`.
176-
final int _sourceOffset,
177-
);
178-
}
149+
final class _MarkerCode(
150+
/// What kind of selection endpoint is being marked.
151+
final _Marker _marker,
152+
153+
/// The number of characters into the next [Code] object where the marker
154+
/// should appear in the resulting output.
155+
final int _offset,
156+
) extends Code;
157+
158+
final class _EnableFormattingCode(
159+
/// Whether this comment disables formatting (`format off`) or re-enables it
160+
/// (`format on`).
161+
final bool _enabled,
162+
163+
/// The number of code points from the beginning of the unformatted source
164+
/// where the unformatted code should begin or end.
165+
///
166+
/// If this piece is for `// dart format off`, then the offset is just past
167+
/// the `off`. If this piece is for `// dart format on`, it points to just
168+
/// before `//`.
169+
final int _sourceOffset,
170+
) extends Code;
179171

180172
/// Which selection marker is pointed to by a [_MarkerCode].
181173
enum _Marker { start, end }

lib/src/back_end/solver.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ const _maxAttempts = 10000;
4242
/// use it across different Solutions. This enables us to both divide and
4343
/// conquer the Piece tree and solve portions separately, while also
4444
/// reusing work across different solutions.
45-
final class Solver {
45+
final class Solver(
46+
final SolutionCache _cache, {
47+
required final int _pageWidth,
48+
49+
/// The number of spaces of indentation on the first line.
50+
int final int _leadingIndent = 0,
51+
int? subsequentIndent,
52+
}) {
4653
/// The number of spaces of indentation on all lines after the first.
47-
final int _subsequentIndent;
54+
final int _subsequentIndent = subsequentIndent ?? _leadingIndent;
4855

4956
final PriorityQueue<Solution> _queue = PriorityQueue();
5057

@@ -53,14 +60,7 @@ final class Solver {
5360
/// The first line is indented by [leadingIndent] spaces and all lines after
5461
/// that are indented by [subsequentIndent]. If [subsequentIndent] is omitted,
5562
/// defaults to [leadingIndent].
56-
this(
57-
final SolutionCache _cache, {
58-
required final int _pageWidth,
59-
60-
/// The number of spaces of indentation on the first line.
61-
int final int _leadingIndent = 0,
62-
int? subsequentIndent,
63-
}) : _subsequentIndent = subsequentIndent ?? leadingIndent;
63+
this;
6464

6565
/// Finds the best set of line splits for [root] piece and returns the
6666
/// resulting formatted code.

lib/src/cli/format_command.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ final class FormatCommand extends Command<int> {
1818
@override
1919
String get name => 'format';
2020

21+
@override
22+
final String category;
23+
2124
@override
2225
String get description => 'Idiomatically format Dart source code.';
2326

2427
@override
2528
String get invocation =>
2629
'${runner!.executableName} $name [options...] <files or directories...>';
2730

28-
this({
29-
bool verbose = false,
30-
@override
31-
final String category = '',
32-
}) {
31+
new({bool verbose = false, this.category = ''}) {
3332
argParser.addFlag(
3433
'verbose',
3534
abbr: 'v',

lib/src/front_end/chain_builder.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ import 'piece_factory.dart';
4242
///
4343
/// This lets us create a single [ChainPiece] for the entire series of dotted
4444
/// operations, so that we can control splitting them or not as a unit.
45-
final class ChainBuilder {
45+
final class ChainBuilder(
46+
final PieceFactory _visitor,
47+
48+
/// The outermost expression being converted to a chain.
49+
///
50+
/// If it's a [CascadeExpression], then the chain is the cascade sections.
51+
/// Otherwise, it's some kind of method call or property access and the chain
52+
/// is the nested series of selector subexpressions.
53+
final Expression _root,
54+
) {
4655
/// The left-most target of the chain.
4756
late Piece _target;
4857

@@ -61,16 +70,7 @@ final class ChainBuilder {
6170
/// The dotted property accesses and method calls following the target.
6271
final List<ChainCall> _calls = [];
6372

64-
this(
65-
final PieceFactory _visitor,
66-
67-
/// The outermost expression being converted to a chain.
68-
///
69-
/// If it's a [CascadeExpression], then the chain is the cascade sections.
70-
/// Otherwise, it's some kind of method call or property access and the chain
71-
/// is the nested series of selector subexpressions.
72-
final Expression _root,
73-
) {
73+
this {
7474
if (_root case CascadeExpression cascade) {
7575
_visitTarget(cascade.target, cascadeTarget: true);
7676

lib/src/front_end/delimited_list_builder.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import 'piece_factory.dart';
1818
/// delimiter token. Then call [add()] for each [AstNode] that is inside the
1919
/// delimiters. The [rightBracket()] with the closing delimiter and finally
2020
/// [build()] to get the resulting [ListPiece].
21-
final class DelimitedListBuilder {
21+
final class DelimitedListBuilder(
22+
final PieceFactory _visitor, [
23+
final ListStyle _style = const ListStyle(),
24+
]) {
2225
/// The opening bracket before the elements, if any.
2326
Piece? _leftBracket;
2427

@@ -43,10 +46,7 @@ final class DelimitedListBuilder {
4346

4447
/// Creates a new [DelimitedListBuilder] for an argument list, collection
4548
/// literal, etc.
46-
this(
47-
final PieceFactory _visitor, [
48-
final ListStyle _style = const ListStyle(),
49-
]);
49+
this;
5050

5151
/// Creates the final [ListPiece] out of the added brackets, delimiters,
5252
/// elements, and style.

lib/src/piece/assign.dart

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,39 +61,37 @@ import 'piece.dart';
6161
/// var [unsplitBlock] =
6262
/// longOperand +
6363
/// anotherOperand;
64-
final class AssignPiece extends Piece {
64+
final class AssignPiece(
65+
/// The left-hand side of the operation and the operator itself.
66+
final Piece _left,
67+
68+
/// The right-hand side of the operation.
69+
final Piece _right, {
70+
71+
/// Whether the piece should have a cost for splitting at the operator.
72+
///
73+
/// Usually true because it's generally better to block split inside the
74+
/// operands when possible. But false for `=>` when the expression has a form
75+
/// where we'd rather keep the expression itself unsplit as in:
76+
///
77+
/// // Don't avoid split:
78+
/// makeStuff() => [
79+
/// element,
80+
/// element,
81+
/// ];
82+
///
83+
/// // Avoid split:
84+
/// doThing() =>
85+
/// thingToDo(argument, argument);
86+
final bool _avoidSplit = true,
87+
}) extends Piece {
6588
/// Allow the right-hand side to block split.
6689
static const State _blockOrHeadlineSplitRight = State(1, cost: 0);
6790

6891
/// Force the left-hand side to block split and allow the right-hand side to
6992
/// split.
7093
static const State _blockSplitLeft = State(2);
7194

72-
this(
73-
/// The left-hand side of the operation and the operator itself.
74-
final Piece _left,
75-
76-
/// The right-hand side of the operation.
77-
final Piece _right, {
78-
79-
/// Whether the piece should have a cost for splitting at the operator.
80-
///
81-
/// Usually true because it's generally better to block split inside the
82-
/// operands when possible. But false for `=>` when the expression has a form
83-
/// where we'd rather keep the expression itself unsplit as in:
84-
///
85-
/// // Don't avoid split:
86-
/// makeStuff() => [
87-
/// element,
88-
/// element,
89-
/// ];
90-
///
91-
/// // Avoid split:
92-
/// doThing() =>
93-
/// thingToDo(argument, argument);
94-
final bool _avoidSplit = true
95-
});
96-
9795
@override
9896
List<State> get additionalStates => [
9997
_blockOrHeadlineSplitRight,

lib/src/piece/assign_3_dot_7.dart

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,57 @@ import 'piece.dart';
6868
/// var [unsplitBlock] =
6969
/// longOperand +
7070
/// anotherOperand;
71-
final class AssignPiece3Dot7 extends Piece {
71+
final class AssignPiece3Dot7(
72+
/// The `=` or other operator.
73+
final Piece _operator,
74+
75+
// TODO(rnystrom): If it wasn't for the need to constrain [_left] to split
76+
// in [applyConstraints()], we could write the operator into the same piece
77+
// as [_left]. In the common case where the AssignPiece is for a named
78+
// argument, the name and `:` would then end up in a single atomic
79+
// [CodePiece].
80+
81+
/// The right-hand side of the operation.
82+
final Piece _right, {
83+
84+
/// The left-hand side of the operation.
85+
final Piece? _left,
86+
87+
/// If `true`, then the left side supports being block-formatted, like:
88+
///
89+
/// var [
90+
/// element1,
91+
/// element2,
92+
/// ] = value;
93+
final bool _canBlockSplitLeft = false,
94+
95+
/// If `true` then the right side supports being block-formatted, like:
96+
///
97+
/// var list = [
98+
/// element1,
99+
/// element2,
100+
/// ];
101+
final bool _canBlockSplitRight = false,
102+
103+
/// If `true` then prefer to split at the operator instead of block splitting
104+
/// the right side.
105+
///
106+
/// This is `true` for `=>` functions whose body is a function call. This
107+
/// keeps the called function next to its arguments instead having the
108+
/// function name stick to the `=>` while the arguments split. In other words,
109+
/// prefer:
110+
///
111+
/// someMethod() =>
112+
/// someFunction(argument, another);
113+
///
114+
/// Over:
115+
///
116+
/// someMethod() => someFunction(
117+
/// argument,
118+
/// another,
119+
/// );
120+
final bool _avoidBlockSplitRight = false,
121+
}) extends Piece {
72122
/// Force the block left-hand side to split and allow the right-hand side to
73123
/// split.
74124
static const State _blockSplitLeft = State(1);
@@ -79,58 +129,6 @@ final class AssignPiece3Dot7 extends Piece {
79129
/// Split at the operator.
80130
static const State _atOperator = State(3);
81131

82-
this(
83-
/// The `=` or other operator.
84-
final Piece _operator,
85-
86-
// TODO(rnystrom): If it wasn't for the need to constrain [_left] to split
87-
// in [applyConstraints()], we could write the operator into the same piece
88-
// as [_left]. In the common case where the AssignPiece is for a named
89-
// argument, the name and `:` would then end up in a single atomic
90-
// [CodePiece].
91-
92-
/// The right-hand side of the operation.
93-
final Piece _right, {
94-
95-
/// The left-hand side of the operation.
96-
final Piece? _left,
97-
98-
/// If `true`, then the left side supports being block-formatted, like:
99-
///
100-
/// var [
101-
/// element1,
102-
/// element2,
103-
/// ] = value;
104-
final bool _canBlockSplitLeft = false,
105-
106-
/// If `true` then the right side supports being block-formatted, like:
107-
///
108-
/// var list = [
109-
/// element1,
110-
/// element2,
111-
/// ];
112-
final bool _canBlockSplitRight = false,
113-
114-
/// If `true` then prefer to split at the operator instead of block splitting
115-
/// the right side.
116-
///
117-
/// This is `true` for `=>` functions whose body is a function call. This
118-
/// keeps the called function next to its arguments instead having the
119-
/// function name stick to the `=>` while the arguments split. In other words,
120-
/// prefer:
121-
///
122-
/// someMethod() =>
123-
/// someFunction(argument, another);
124-
///
125-
/// Over:
126-
///
127-
/// someMethod() => someFunction(
128-
/// argument,
129-
/// another,
130-
/// );
131-
final bool _avoidBlockSplitRight = false,
132-
});
133-
134132
@override
135133
List<State> get additionalStates => [
136134
// If at least one operand can block split, allow splitting in operands

0 commit comments

Comments
 (0)