From 07adb5c6a9467b77eb3d58eb29795d3d84aff5d3 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Tue, 10 Dec 2024 00:23:42 +0000 Subject: [PATCH] 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. --- example_usage/lib/library_source.g.dart | 1 - example_usage/lib/library_source.info.dart | 2 +- source_gen/CHANGELOG.md | 4 ++-- source_gen/lib/src/builder.dart | 19 ++++++++++++------- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/example_usage/lib/library_source.g.dart b/example_usage/lib/library_source.g.dart index 26bcdcad..482ec38e 100644 --- a/example_usage/lib/library_source.g.dart +++ b/example_usage/lib/library_source.g.dart @@ -1,5 +1,4 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 // ignore_for_file: lint_a, lint_b diff --git a/example_usage/lib/library_source.info.dart b/example_usage/lib/library_source.info.dart index 99b7457e..47ff6ea3 100644 --- a/example_usage/lib/library_source.info.dart +++ b/example_usage/lib/library_source.info.dart @@ -1,5 +1,5 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND // dart format width=80 +// GENERATED CODE - DO NOT MODIFY BY HAND // ************************************************************************** // MemberCountLibraryGenerator diff --git a/source_gen/CHANGELOG.md b/source_gen/CHANGELOG.md index fe33fec9..25df1300 100644 --- a/source_gen/CHANGELOG.md +++ b/source_gen/CHANGELOG.md @@ -13,8 +13,8 @@ - `LibraryBuilder`, `PartBuilder`, and `SharedPartBuilder` now take an optional `writeDescriptions` boolean. When set to `false`, headers and generator descriptions for the files will not be included in the builder output. -- Include `//dart format width=80` comments in Dart unit files when they are - formatted with the default callback. +- Include `//dart format width=80` comments in files generated by a + `LibraryBuilder` or `PartBuilder` and formatted with the default callback. ## 1.5.0 diff --git a/source_gen/lib/src/builder.dart b/source_gen/lib/src/builder.dart index f053a3ca..53b8c2f7 100644 --- a/source_gen/lib/src/builder.dart +++ b/source_gen/lib/src/builder.dart @@ -50,7 +50,7 @@ class _Builder extends Builder { /// [options] to allow output files to be generated into a different directory _Builder( this._generators, { - this.formatOutput = _defaultFormatOutput, + required this.formatOutput, String generatedExtension = '.g.dart', List additionalOutputExtensions = const [], String? header, @@ -236,7 +236,7 @@ class SharedPartBuilder extends _Builder { SharedPartBuilder( super.generators, String partId, { - super.formatOutput, + super.formatOutput = _defaultFormatOutput, super.additionalOutputExtensions, super.allowSyntaxErrors, super.writeDescriptions, @@ -298,7 +298,7 @@ class PartBuilder extends _Builder { PartBuilder( super.generators, String generatedExtension, { - super.formatOutput, + super.formatOutput = _defaultFormatUnit, super.additionalOutputExtensions, super.writeDescriptions, super.header, @@ -325,7 +325,8 @@ class LibraryBuilder extends _Builder { /// should be indicated in [additionalOutputExtensions]. /// /// [formatOutput] is called to format the generated code. Defaults to - /// using the standard [DartFormatter]. + /// using the standard [DartFormatter] and writing a comment specifying the + /// default format width of 80.. /// /// [writeDescriptions] adds comments to the output used to separate the /// sections of the file generated from different generators, and reveals @@ -341,7 +342,7 @@ class LibraryBuilder extends _Builder { /// libraries. LibraryBuilder( Generator generator, { - super.formatOutput, + super.formatOutput = _defaultFormatUnit, super.generatedExtension, super.additionalOutputExtensions, super.writeDescriptions, @@ -408,9 +409,13 @@ Future _hasAnyTopLevelAnnotations( const defaultFileHeader = '// GENERATED CODE - DO NOT MODIFY BY HAND'; -String _defaultFormatOutput(String code, Version version) { +String _defaultFormatOutput(String code, Version version) => + DartFormatter(languageVersion: version).format(code); + +/// Prefixes a dart format width and formats [code]. +String _defaultFormatUnit(String code, Version version) { code = '$dartFormatWidth\n$code'; - return DartFormatter(languageVersion: version).format(code); + return _defaultFormatOutput(code, version); } final _headerLine = '// '.padRight(77, '*');