Skip to content

Commit 7e2374c

Browse files
committed
Experimental migration to proposed "declaring constructors".
This is just a temporary PR to get a feel for how the proposed syntax would look in a real codebase. I used a mixture of declaring, primary, and non-declaring constructors based on whatever I felt looked the best for each class.
1 parent ec0048b commit 7e2374c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+636
-891
lines changed

lib/src/analysis_options/analysis_options_file.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,9 @@ Future<AnalysisOptions> readAnalysisOptions(
147147

148148
/// Exception thrown when an analysis options file contains a "package:" URI in
149149
/// an include and resolving the URI to a file path failed.
150-
final class PackageResolutionException implements Exception {
151-
final String _message;
152-
153-
PackageResolutionException(this._message);
154-
150+
final class PackageResolutionException(
151+
final String _message,
152+
) implements Exception {
155153
@override
156154
String toString() => _message;
157155
}

lib/src/analysis_options/io_file_system.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ final class IOFileSystem implements FileSystem {
4343
/// An abstraction over a file path string, used by [IOFileSystem].
4444
///
4545
/// To create an instance of this, use [IOFileSystem.makePath()].
46-
final class IOFileSystemPath implements FileSystemPath {
46+
final class IOFileSystemPath._(
4747
/// The underlying physical file system path.
48-
final String path;
48+
final String path,
49+
) implements FileSystemPath;
4950

50-
IOFileSystemPath._(this.path);
51-
}

lib/src/back_end/code.dart

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ sealed class Code {
7070

7171
/// A [Code] object which can be written to and contain other child [Code]
7272
/// objects.
73-
final class GroupCode extends Code {
73+
final class GroupCode(
7474
/// How many spaces the first text inside this group should be indented.
75-
final int _indent;
76-
75+
final int _indent,
76+
) extends Code {
7777
/// The child [Code] objects contained in this group.
7878
final List<Code> _children = [];
7979

80-
GroupCode(this._indent);
81-
8280
/// Appends [text] to this code.
8381
void write(String text) {
8482
_children.add(_TextCode(text));
@@ -135,60 +133,59 @@ final class GroupCode extends Code {
135133

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

144-
_NewlineCode({required bool blank, required int indent})
145-
: _indent = indent,
146-
_blank = blank;
140+
/// The number of spaces of indentation after this newline.
141+
required final int _indent,
142+
});
147143
}
148144

149145
/// A [Code] object for literal source text.
150146
final class _TextCode extends Code {
151-
final String _text;
152-
153-
_TextCode(this._text);
147+
this(final String _text);
154148
}
155149

156150
/// Marks the location of the beginning or end of a selection as occurring
157151
/// [_offset] characters past the point where this marker object appears in the
158152
/// list of [Code] objects.
159153
final class _MarkerCode extends Code {
160-
/// What kind of selection endpoint is being marked.
161-
final _Marker _marker;
162-
163-
/// The number of characters into the next [Code] object where the marker
164-
/// should appear in the resulting output.
165-
final int _offset;
166-
167-
_MarkerCode(this._marker, this._offset);
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+
);
168162
}
169163

170164
final class _EnableFormattingCode extends Code {
171-
/// Whether this comment disables formatting (`format off`) or re-enables it
172-
/// (`format on`).
173-
final bool _enabled;
174-
175-
/// The number of code points from the beginning of the unformatted source
176-
/// where the unformatted code should begin or end.
177-
///
178-
/// If this piece is for `// dart format off`, then the offset is just past
179-
/// the `off`. If this piece is for `// dart format on`, it points to just
180-
/// before `//`.
181-
final int _sourceOffset;
182-
183-
_EnableFormattingCode(this._enabled, this._sourceOffset);
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+
);
184178
}
185179

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

189183
/// Traverses a [Code] tree and produces the final string of output code and
190184
/// the selection markers, if any.
191-
final class _StringBuilder {
185+
final class _StringBuilder(
186+
final SourceCode _source,
187+
final String _lineEnding,
188+
) {
192189
/// Pre-calculated whitespace strings for various common levels of
193190
/// indentation.
194191
///
@@ -227,8 +224,6 @@ final class _StringBuilder {
227224
60: ' ',
228225
};
229226

230-
final SourceCode _source;
231-
final String _lineEnding;
232227
final StringBuffer _buffer = StringBuffer();
233228

234229
/// The offset from the beginning of the source to where the selection start
@@ -248,8 +243,6 @@ final class _StringBuilder {
248243
/// Otherwise, -1 to indicate that formatting is enabled.
249244
int _disableFormattingStart = -1;
250245

251-
_StringBuilder(this._source, this._lineEnding);
252-
253246
void traverse(Code code) {
254247
switch (code) {
255248
case _NewlineCode():

lib/src/back_end/code_writer.dart

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ import 'solution_cache.dart';
2121
/// an instance of this class. It has methods that the piece can call to add
2222
/// output text to the resulting code, recursively format child pieces, insert
2323
/// whitespace, etc.
24-
final class CodeWriter {
25-
final int _pageWidth;
26-
24+
final class CodeWriter(
25+
final int _pageWidth,
26+
int leadingIndent,
27+
int subsequentIndent,
2728
/// Previously cached formatted subtrees.
28-
final SolutionCache _cache;
29+
final SolutionCache _cache,
2930

3031
/// The solution this [CodeWriter] is generating code for.
31-
final Solution _solution;
32-
32+
final Solution _solution,
33+
) {
3334
/// The code being written.
34-
final GroupCode _code;
35+
final GroupCode _code = GroupCode(leadingIndent);
3536

3637
/// What whitespace should be written before the next non-whitespace text.
3738
///
@@ -47,17 +48,25 @@ final class CodeWriter {
4748
/// The number of spaces of indentation that should be begin the next line
4849
/// when [_pendingWhitespace] is [Whitespace.newline] or
4950
/// [Whitespace.blankLine].
50-
int _pendingIndent = 0;
51+
int _pendingIndent = leadingIndent;
5152

5253
/// The number of characters in the line currently being written.
53-
int _column = 0;
54+
int _column = leadingIndent;
5455

5556
/// The stack of indentation levels.
5657
///
5758
/// Each entry in the stack is the absolute number of spaces of leading
5859
/// indentation that should be written when beginning a new line to account
5960
/// for block nesting, expression wrapping, constructor initializers, etc.
60-
final List<_IndentLevel> _indentStack = [];
61+
final List<_IndentLevel> _indentStack = [
62+
_IndentLevel(Indent.none, leadingIndent),
63+
64+
// If there is additional indentation on subsequent lines, then push that
65+
// onto the stack. When the first newline is written, [_pendingIndent] will
66+
// pick this up and use it for subsequent lines.
67+
if (subsequentIndent > leadingIndent)
68+
_IndentLevel(Indent.none, subsequentIndent)
69+
];
6170

6271
/// The stack of information for each [Piece] currently being formatted.
6372
///
@@ -102,26 +111,7 @@ final class CodeWriter {
102111
/// beginning of the first line and [subsequentIndent] is the indentation of
103112
/// each line after that, independent of indentation created by pieces being
104113
/// written.
105-
CodeWriter(
106-
this._pageWidth,
107-
int leadingIndent,
108-
int subsequentIndent,
109-
this._cache,
110-
this._solution,
111-
) : _code = GroupCode(leadingIndent) {
112-
_indentStack.add(_IndentLevel(Indent.none, leadingIndent));
113-
114-
// Track the leading indent before the first line.
115-
_pendingIndent = leadingIndent;
116-
_column = _pendingIndent;
117-
118-
// If there is additional indentation on subsequent lines, then push that
119-
// onto the stack. When the first newline is written, [_pendingIndent] will
120-
// pick this up and use it for subsequent lines.
121-
if (subsequentIndent > leadingIndent) {
122-
_indentStack.add(_IndentLevel(Indent.none, subsequentIndent));
123-
}
124-
}
114+
this;
125115

126116
/// Returns the final formatted code and the next pieces that can be expanded
127117
/// from the solution this [CodeWriter] is writing, if any.
@@ -541,7 +531,10 @@ enum Whitespace {
541531
/// is also semantic: a type describes *why* it writes that, or what kind of
542532
/// syntax its coming from. This allows us to merge or combine indentation in
543533
/// smarter ways in some contexts.
544-
enum Indent {
534+
enum const Indent(
535+
/// The number of spaces this type of indentation applies.
536+
final int spaces,
537+
) {
545538
// No indentation.
546539
none(0),
547540

@@ -575,29 +568,22 @@ enum Indent {
575568

576569
/// Constructor initializer when the parameter list does have optional or
577570
/// named parameters.
578-
initializerWithOptionalParameter(3);
579-
580-
/// The number of spaces this type of indentation applies.
581-
final int spaces;
582-
583-
const Indent(this.spaces);
571+
initializerWithOptionalParameter(3),
584572
}
585573

586574
/// Information for each piece currently being formatted while [CodeWriter]
587575
/// traverses the piece tree.
588-
class _FormatState {
576+
class _FormatState(
589577
/// The piece being formatted.
590-
final Piece piece;
591-
578+
final Piece piece,
579+
) {
592580
/// The piece's shape.
593581
///
594582
/// This changes based on the newlines the piece writes.
595583
Shape shape = Shape.inline;
596584

597585
/// How a newline affects the shape of this piece.
598586
ShapeMode mode = ShapeMode.merge;
599-
600-
_FormatState(this.piece);
601587
}
602588

603589
/// Determines how a newline inside a piece or a child piece affects the shape
@@ -626,23 +612,23 @@ enum ShapeMode {
626612
}
627613

628614
/// A level of indentation in the indentation stack.
629-
final class _IndentLevel {
615+
final class _IndentLevel(
630616
/// The reason this indentation was added.
631617
///
632618
/// Not used for 3.7 style.
633-
final Indent type;
619+
final Indent type,
634620

635621
/// The total number of spaces of indentation.
636-
final int spaces;
637-
622+
final int spaces,
623+
) {
638624
/// How many spaces of [spaces] can be collapsed with further indentation.
639625
///
640626
/// Only used for 3.7 style.
641627
final int collapsible;
642628

643629
_IndentLevel.v3Dot7(this.spaces, this.collapsible) : type = Indent.none;
644630

645-
_IndentLevel(this.type, this.spaces) : collapsible = 0;
631+
this : collapsible = 0;
646632

647633
@override
648634
String toString() => '${type.name}:$spaces';

lib/src/back_end/solution.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ final class Solution implements Comparable<Solution> {
113113
/// Creates a new [Solution] with no pieces set to any state (which
114114
/// implicitly means they have state [State.unsplit] unless they're pinned to
115115
/// another state).
116-
factory Solution(
116+
factory(
117117
SolutionCache cache,
118118
Piece root, {
119119
required int pageWidth,
@@ -126,7 +126,7 @@ final class Solution implements Comparable<Solution> {
126126
return solution;
127127
}
128128

129-
Solution._(
129+
new _(
130130
Piece root,
131131
this._cost,
132132
this._pieceStates,

lib/src/back_end/solution_cache.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ import 'solver.dart';
2222
/// the same child Piece and wanting to format it separately with the same
2323
/// indentation. When that happens, sharing this cache allows us to reuse that
2424
/// cached subtree Solution.
25-
final class SolutionCache {
25+
final class SolutionCache({
2626
/// Whether this cache and all solutions in it use the 3.7 style solver.
27-
final bool is3Dot7;
28-
27+
required final bool is3Dot7,
28+
}) {
2929
final _cache = <_Key, Solution>{};
3030

31-
SolutionCache({required this.is3Dot7});
32-
3331
/// Returns a previously cached solution for formatting [root] with leading
3432
/// [indent] (and [subsequentIndent] for lines after the first) or produces a
3533
/// new solution, caches it, and returns it.

lib/src/back_end/solver.dart

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ const _maxAttempts = 10000;
4343
/// conquer the Piece tree and solve portions separately, while also
4444
/// reusing work across different solutions.
4545
final class Solver {
46-
final SolutionCache _cache;
47-
48-
final int _pageWidth;
49-
50-
/// The number of spaces of indentation on the first line.
51-
final int _leadingIndent;
52-
5346
/// The number of spaces of indentation on all lines after the first.
5447
final int _subsequentIndent;
5548

@@ -60,14 +53,14 @@ final class Solver {
6053
/// The first line is indented by [leadingIndent] spaces and all lines after
6154
/// that are indented by [subsequentIndent]. If [subsequentIndent] is omitted,
6255
/// defaults to [leadingIndent].
63-
Solver(
64-
this._cache, {
65-
required int pageWidth,
66-
int leadingIndent = 0,
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,
6762
int? subsequentIndent,
68-
}) : _pageWidth = pageWidth,
69-
_leadingIndent = leadingIndent,
70-
_subsequentIndent = subsequentIndent ?? leadingIndent;
63+
}) : _subsequentIndent = subsequentIndent ?? leadingIndent;
7164

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

0 commit comments

Comments
 (0)