Skip to content

Commit 9f95ac3

Browse files
authored
Only add dart format width comment on format (#733)
Closes #724 A use case with a custom `formatCode` callback can reasonably use a different line width than the default, so only add the width comment when this package is responsible for performing a format. Uses with a custom header will no longer slot the width comment in following the header. Some outputs will no longer have the comment. This is not breaking since there has been no published since the comment was introduced. Files using the shared part builder will need to resolve format width conflicts using some other mechanism. A shared part file can have outputs contributed from different code generators that are unrelated, and may have different behavior around formatting, so it is not possible to write a single width comment that applies to the combined file.
1 parent b7b0c7d commit 9f95ac3

File tree

7 files changed

+34
-35
lines changed

7 files changed

+34
-35
lines changed

example_usage/lib/library_source.g.dart

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_usage/lib/library_source.info.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// GENERATED CODE - DO NOT MODIFY BY HAND
21
// dart format width=80
2+
// GENERATED CODE - DO NOT MODIFY BY HAND
33

44
// **************************************************************************
55
// MemberCountLibraryGenerator

source_gen/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
- `LibraryBuilder`, `PartBuilder`, and `SharedPartBuilder` now take an optional
1414
`writeDescriptions` boolean. When set to `false`, headers and generator
1515
descriptions for the files will not be included in the builder output.
16-
- Include `//dart format width=80` comments in generated Dart unit files.
16+
- Include `//dart format width=80` comments in files generated by a
17+
`LibraryBuilder` or `PartBuilder` and formatted with the default callback.
1718

1819
## 1.5.0
1920

source_gen/lib/builder.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ class CombiningBuilder implements Builder {
143143

144144
final output = '''
145145
$defaultFileHeader
146-
$dartFormatWidth
147146
${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble
148147
part of '$partOfUri';
149148

source_gen/lib/src/builder.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class _Builder extends Builder {
5050
/// [options] to allow output files to be generated into a different directory
5151
_Builder(
5252
this._generators, {
53-
this.formatOutput = _defaultFormatOutput,
53+
required this.formatOutput,
5454
String generatedExtension = '.g.dart',
5555
List<String> additionalOutputExtensions = const [],
5656
String? header,
@@ -134,7 +134,6 @@ class _Builder extends Builder {
134134

135135
if (this is PartBuilder) {
136136
contentBuffer
137-
..writeln(dartFormatWidth)
138137
..write(languageOverrideForLibrary(library))
139138
..writeln('part of \'$partOfUri\';');
140139
final part = computePartUrl(buildStep.inputId, outputId);
@@ -156,8 +155,6 @@ class _Builder extends Builder {
156155
// For shared-part builders, `part` statements will be checked by the
157156
// combining build step.
158157
}
159-
} else {
160-
contentBuffer.writeln(dartFormatWidth);
161158
}
162159

163160
for (var item in generatedOutputs) {
@@ -239,7 +236,7 @@ class SharedPartBuilder extends _Builder {
239236
SharedPartBuilder(
240237
super.generators,
241238
String partId, {
242-
super.formatOutput,
239+
super.formatOutput = _defaultFormatOutput,
243240
super.additionalOutputExtensions,
244241
super.allowSyntaxErrors,
245242
super.writeDescriptions,
@@ -301,7 +298,7 @@ class PartBuilder extends _Builder {
301298
PartBuilder(
302299
super.generators,
303300
String generatedExtension, {
304-
super.formatOutput,
301+
super.formatOutput = _defaultFormatUnit,
305302
super.additionalOutputExtensions,
306303
super.writeDescriptions,
307304
super.header,
@@ -328,7 +325,8 @@ class LibraryBuilder extends _Builder {
328325
/// should be indicated in [additionalOutputExtensions].
329326
///
330327
/// [formatOutput] is called to format the generated code. Defaults to
331-
/// using the standard [DartFormatter].
328+
/// using the standard [DartFormatter] and writing a comment specifying the
329+
/// default format width of 80..
332330
///
333331
/// [writeDescriptions] adds comments to the output used to separate the
334332
/// sections of the file generated from different generators, and reveals
@@ -344,7 +342,7 @@ class LibraryBuilder extends _Builder {
344342
/// libraries.
345343
LibraryBuilder(
346344
Generator generator, {
347-
super.formatOutput,
345+
super.formatOutput = _defaultFormatUnit,
348346
super.generatedExtension,
349347
super.additionalOutputExtensions,
350348
super.writeDescriptions,
@@ -414,6 +412,12 @@ const defaultFileHeader = '// GENERATED CODE - DO NOT MODIFY BY HAND';
414412
String _defaultFormatOutput(String code, Version version) =>
415413
DartFormatter(languageVersion: version).format(code);
416414

415+
/// Prefixes a dart format width and formats [code].
416+
String _defaultFormatUnit(String code, Version version) {
417+
code = '$dartFormatWidth\n$code';
418+
return _defaultFormatOutput(code, version);
419+
}
420+
417421
final _headerLine = '// '.padRight(77, '*');
418422

419423
const partIdRegExpLiteral = r'[A-Za-z_\d-]+';

source_gen/test/builder_test.dart

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void main() {
6767
generateFor: {'$_pkgName|lib/test_lib.dart'},
6868
outputs: {
6969
'$_pkgName|lib/test_lib.g.dart': decodedMatches(
70-
startsWith('$_customHeader\n$dartFormatWidth\n\n// ***'),
70+
startsWith('$dartFormatWidth\n$_customHeader\n\n// ***'),
7171
),
7272
},
7373
);
@@ -317,7 +317,7 @@ $dartFormatWidth
317317
generateFor: {'$_pkgName|lib/a.dart'},
318318
outputs: {
319319
'$_pkgName|lib/a.foo.dart': decodedMatches(
320-
startsWith('$_customHeader\n\n$dartFormatWidth\npart of'),
320+
startsWith('$dartFormatWidth\n$_customHeader\n\npart of'),
321321
),
322322
},
323323
);
@@ -334,7 +334,7 @@ $dartFormatWidth
334334
generateFor: {'$_pkgName|lib/a.dart'},
335335
outputs: {
336336
'$_pkgName|lib/a.foo.dart':
337-
decodedMatches(startsWith('$dartFormatWidth\npart of')),
337+
decodedMatches(startsWith('$dartFormatWidth\n\npart of')),
338338
},
339339
);
340340
});
@@ -348,14 +348,13 @@ $dartFormatWidth
348348
),
349349
{
350350
'$_pkgName|lib/a.dart': '''
351-
$dartFormatWidth
352351
// @dart=2.12
353352
part "a.foo.dart";''',
354353
},
355354
generateFor: {'$_pkgName|lib/a.dart'},
356355
outputs: {
357356
'$_pkgName|lib/a.foo.dart': decodedMatches(
358-
startsWith('$dartFormatWidth\n// @dart=2.12\n'),
357+
startsWith('$dartFormatWidth\n\n// @dart=2.12\n'),
359358
),
360359
},
361360
);
@@ -422,7 +421,7 @@ part "a.foo.dart";''',
422421
generateFor: {'$_pkgName|lib/a.dart'},
423422
outputs: {
424423
'$_pkgName|lib/generated/a.foo.dart': decodedMatches(
425-
startsWith("$dartFormatWidth\npart of '../a.dart';"),
424+
startsWith("$dartFormatWidth\n\npart of '../a.dart';"),
426425
),
427426
},
428427
);
@@ -570,7 +569,6 @@ part "a.foo.dart";''',
570569
const CombiningBuilder(),
571570
{
572571
'$_pkgName|lib/a.dart': '''
573-
$dartFormatWidth
574572
// @dart=2.12
575573
library a;
576574
part "a.g.dart";
@@ -582,7 +580,6 @@ part "a.g.dart";
582580
'$_pkgName|lib/a.g.dart': decodedMatches(
583581
'''
584582
// GENERATED CODE - DO NOT MODIFY BY HAND
585-
$dartFormatWidth
586583
// @dart=2.12
587584
588585
part of 'a.dart';
@@ -1069,10 +1066,10 @@ const _testLibContentSyntaxError = r'''
10691066
final int foo = 42
10701067
''';
10711068

1072-
const _testGenPartContent = r'''
1069+
const _testGenPartContent = '''
1070+
$dartFormatWidth
10731071
// GENERATED CODE - DO NOT MODIFY BY HAND
10741072
1075-
// dart format width=80
10761073
part of 'test_lib.dart';
10771074
10781075
// **************************************************************************
@@ -1083,10 +1080,10 @@ part of 'test_lib.dart';
10831080
// Code for "class Customer"
10841081
''';
10851082

1086-
const _testGenPartContentForLibrary = r'''
1083+
const _testGenPartContentForLibrary = '''
1084+
$dartFormatWidth
10871085
// GENERATED CODE - DO NOT MODIFY BY HAND
10881086
1089-
// dart format width=80
10901087
part of 'test_lib.dart';
10911088
10921089
// **************************************************************************
@@ -1096,9 +1093,9 @@ part of 'test_lib.dart';
10961093
// Code for "test_lib"
10971094
''';
10981095

1099-
const _testGenStandaloneContent = r'''
1096+
const _testGenStandaloneContent = '''
1097+
$dartFormatWidth
11001098
// GENERATED CODE - DO NOT MODIFY BY HAND
1101-
// dart format width=80
11021099
11031100
// **************************************************************************
11041101
// CommentGenerator
@@ -1108,10 +1105,10 @@ const _testGenStandaloneContent = r'''
11081105
// Code for "class Customer"
11091106
''';
11101107

1111-
const _testGenPartContentForClassesAndLibrary = r'''
1108+
const _testGenPartContentForClassesAndLibrary = '''
1109+
$dartFormatWidth
11121110
// GENERATED CODE - DO NOT MODIFY BY HAND
11131111
1114-
// dart format width=80
11151112
part of 'test_lib.dart';
11161113
11171114
// **************************************************************************
@@ -1123,10 +1120,10 @@ part of 'test_lib.dart';
11231120
// Code for "class Customer"
11241121
''';
11251122

1126-
const _testGenNoLibrary = r'''
1123+
const _testGenNoLibrary = '''
1124+
$dartFormatWidth
11271125
// GENERATED CODE - DO NOT MODIFY BY HAND
11281126
1129-
// dart format width=80
11301127
part of 'test_lib.dart';
11311128
11321129
// **************************************************************************
@@ -1140,7 +1137,6 @@ part of 'test_lib.dart';
11401137
const _whitespaceTrimmed = r'''
11411138
// GENERATED CODE - DO NOT MODIFY BY HAND
11421139
1143-
// dart format width=80
11441140
part of 'test_lib.dart';
11451141
11461142
// **************************************************************************

source_gen/test/generator_for_annotation_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void main() {
4343
builder,
4444
_inputMap,
4545
outputs: {
46-
'a|lib/file.g.dart': r'''
46+
'a|lib/file.g.dart': '''
47+
$dartFormatWidth
4748
// GENERATED CODE - DO NOT MODIFY BY HAND
48-
// dart format width=80
4949
5050
// **************************************************************************
5151
// Generator: Repeating
@@ -129,8 +129,8 @@ void main() {
129129
},
130130
outputs: {
131131
'a|lib/file.g.dart': '''
132-
// GENERATED CODE - DO NOT MODIFY BY HAND
133132
$dartFormatWidth
133+
// GENERATED CODE - DO NOT MODIFY BY HAND
134134
135135
// **************************************************************************
136136
// Generator: Deprecated
@@ -166,8 +166,8 @@ $dartFormatWidth
166166
},
167167
outputs: {
168168
'a|lib/file.g.dart': '''
169-
// GENERATED CODE - DO NOT MODIFY BY HAND
170169
$dartFormatWidth
170+
// GENERATED CODE - DO NOT MODIFY BY HAND
171171
172172
// **************************************************************************
173173
// Generator: Deprecated

0 commit comments

Comments
 (0)