Skip to content

Commit 07adb5c

Browse files
committed
Only write width to library files
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 b5db86b commit 07adb5c

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
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
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source_gen/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +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 Dart unit files when they are
17-
formatted with the default callback.
16+
- Include `//dart format width=80` comments in files generated by a
17+
`LibraryBuilder` or `PartBuilder` and formatted with the default callback.
1818

1919
## 1.5.0
2020

source_gen/lib/src/builder.dart

Lines changed: 12 additions & 7 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,
@@ -236,7 +236,7 @@ class SharedPartBuilder extends _Builder {
236236
SharedPartBuilder(
237237
super.generators,
238238
String partId, {
239-
super.formatOutput,
239+
super.formatOutput = _defaultFormatOutput,
240240
super.additionalOutputExtensions,
241241
super.allowSyntaxErrors,
242242
super.writeDescriptions,
@@ -298,7 +298,7 @@ class PartBuilder extends _Builder {
298298
PartBuilder(
299299
super.generators,
300300
String generatedExtension, {
301-
super.formatOutput,
301+
super.formatOutput = _defaultFormatUnit,
302302
super.additionalOutputExtensions,
303303
super.writeDescriptions,
304304
super.header,
@@ -325,7 +325,8 @@ class LibraryBuilder extends _Builder {
325325
/// should be indicated in [additionalOutputExtensions].
326326
///
327327
/// [formatOutput] is called to format the generated code. Defaults to
328-
/// using the standard [DartFormatter].
328+
/// using the standard [DartFormatter] and writing a comment specifying the
329+
/// default format width of 80..
329330
///
330331
/// [writeDescriptions] adds comments to the output used to separate the
331332
/// sections of the file generated from different generators, and reveals
@@ -341,7 +342,7 @@ class LibraryBuilder extends _Builder {
341342
/// libraries.
342343
LibraryBuilder(
343344
Generator generator, {
344-
super.formatOutput,
345+
super.formatOutput = _defaultFormatUnit,
345346
super.generatedExtension,
346347
super.additionalOutputExtensions,
347348
super.writeDescriptions,
@@ -408,9 +409,13 @@ Future<bool> _hasAnyTopLevelAnnotations(
408409

409410
const defaultFileHeader = '// GENERATED CODE - DO NOT MODIFY BY HAND';
410411

411-
String _defaultFormatOutput(String code, Version version) {
412+
String _defaultFormatOutput(String code, Version version) =>
413+
DartFormatter(languageVersion: version).format(code);
414+
415+
/// Prefixes a dart format width and formats [code].
416+
String _defaultFormatUnit(String code, Version version) {
412417
code = '$dartFormatWidth\n$code';
413-
return DartFormatter(languageVersion: version).format(code);
418+
return _defaultFormatOutput(code, version);
414419
}
415420

416421
final _headerLine = '// '.padRight(77, '*');

0 commit comments

Comments
 (0)