From b5db86bf6da09273a59cd045666161a1e3df6326 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Mon, 9 Dec 2024 23:42:17 +0000 Subject: [PATCH 1/2] Only add dart format width comment on format 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. --- source_gen/CHANGELOG.md | 3 +- source_gen/lib/builder.dart | 1 - source_gen/lib/src/builder.dart | 9 +++-- source_gen/test/builder_test.dart | 34 ++++++++----------- .../test/generator_for_annotation_test.dart | 8 ++--- 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/source_gen/CHANGELOG.md b/source_gen/CHANGELOG.md index f21265b3..fe33fec9 100644 --- a/source_gen/CHANGELOG.md +++ b/source_gen/CHANGELOG.md @@ -13,7 +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 generated Dart unit files. +- Include `//dart format width=80` comments in Dart unit files when they are + formatted with the default callback. ## 1.5.0 diff --git a/source_gen/lib/builder.dart b/source_gen/lib/builder.dart index e33bd4c0..9daadb0a 100644 --- a/source_gen/lib/builder.dart +++ b/source_gen/lib/builder.dart @@ -143,7 +143,6 @@ class CombiningBuilder implements Builder { final output = ''' $defaultFileHeader -$dartFormatWidth ${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble part of '$partOfUri'; diff --git a/source_gen/lib/src/builder.dart b/source_gen/lib/src/builder.dart index 8945e107..f053a3ca 100644 --- a/source_gen/lib/src/builder.dart +++ b/source_gen/lib/src/builder.dart @@ -134,7 +134,6 @@ class _Builder extends Builder { if (this is PartBuilder) { contentBuffer - ..writeln(dartFormatWidth) ..write(languageOverrideForLibrary(library)) ..writeln('part of \'$partOfUri\';'); final part = computePartUrl(buildStep.inputId, outputId); @@ -156,8 +155,6 @@ class _Builder extends Builder { // For shared-part builders, `part` statements will be checked by the // combining build step. } - } else { - contentBuffer.writeln(dartFormatWidth); } for (var item in generatedOutputs) { @@ -411,8 +408,10 @@ Future _hasAnyTopLevelAnnotations( const defaultFileHeader = '// GENERATED CODE - DO NOT MODIFY BY HAND'; -String _defaultFormatOutput(String code, Version version) => - DartFormatter(languageVersion: version).format(code); +String _defaultFormatOutput(String code, Version version) { + code = '$dartFormatWidth\n$code'; + return DartFormatter(languageVersion: version).format(code); +} final _headerLine = '// '.padRight(77, '*'); diff --git a/source_gen/test/builder_test.dart b/source_gen/test/builder_test.dart index 2d6bb8b9..87261476 100644 --- a/source_gen/test/builder_test.dart +++ b/source_gen/test/builder_test.dart @@ -67,7 +67,7 @@ void main() { generateFor: {'$_pkgName|lib/test_lib.dart'}, outputs: { '$_pkgName|lib/test_lib.g.dart': decodedMatches( - startsWith('$_customHeader\n$dartFormatWidth\n\n// ***'), + startsWith('$dartFormatWidth\n$_customHeader\n\n// ***'), ), }, ); @@ -317,7 +317,7 @@ $dartFormatWidth generateFor: {'$_pkgName|lib/a.dart'}, outputs: { '$_pkgName|lib/a.foo.dart': decodedMatches( - startsWith('$_customHeader\n\n$dartFormatWidth\npart of'), + startsWith('$dartFormatWidth\n$_customHeader\n\npart of'), ), }, ); @@ -334,7 +334,7 @@ $dartFormatWidth generateFor: {'$_pkgName|lib/a.dart'}, outputs: { '$_pkgName|lib/a.foo.dart': - decodedMatches(startsWith('$dartFormatWidth\npart of')), + decodedMatches(startsWith('$dartFormatWidth\n\npart of')), }, ); }); @@ -348,14 +348,13 @@ $dartFormatWidth ), { '$_pkgName|lib/a.dart': ''' -$dartFormatWidth // @dart=2.12 part "a.foo.dart";''', }, generateFor: {'$_pkgName|lib/a.dart'}, outputs: { '$_pkgName|lib/a.foo.dart': decodedMatches( - startsWith('$dartFormatWidth\n// @dart=2.12\n'), + startsWith('$dartFormatWidth\n\n// @dart=2.12\n'), ), }, ); @@ -422,7 +421,7 @@ part "a.foo.dart";''', generateFor: {'$_pkgName|lib/a.dart'}, outputs: { '$_pkgName|lib/generated/a.foo.dart': decodedMatches( - startsWith("$dartFormatWidth\npart of '../a.dart';"), + startsWith("$dartFormatWidth\n\npart of '../a.dart';"), ), }, ); @@ -570,7 +569,6 @@ part "a.foo.dart";''', const CombiningBuilder(), { '$_pkgName|lib/a.dart': ''' -$dartFormatWidth // @dart=2.12 library a; part "a.g.dart"; @@ -582,7 +580,6 @@ part "a.g.dart"; '$_pkgName|lib/a.g.dart': decodedMatches( ''' // GENERATED CODE - DO NOT MODIFY BY HAND -$dartFormatWidth // @dart=2.12 part of 'a.dart'; @@ -1069,10 +1066,10 @@ const _testLibContentSyntaxError = r''' final int foo = 42 '''; -const _testGenPartContent = r''' +const _testGenPartContent = ''' +$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 part of 'test_lib.dart'; // ************************************************************************** @@ -1083,10 +1080,10 @@ part of 'test_lib.dart'; // Code for "class Customer" '''; -const _testGenPartContentForLibrary = r''' +const _testGenPartContentForLibrary = ''' +$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 part of 'test_lib.dart'; // ************************************************************************** @@ -1096,9 +1093,9 @@ part of 'test_lib.dart'; // Code for "test_lib" '''; -const _testGenStandaloneContent = r''' +const _testGenStandaloneContent = ''' +$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 // ************************************************************************** // CommentGenerator @@ -1108,10 +1105,10 @@ const _testGenStandaloneContent = r''' // Code for "class Customer" '''; -const _testGenPartContentForClassesAndLibrary = r''' +const _testGenPartContentForClassesAndLibrary = ''' +$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 part of 'test_lib.dart'; // ************************************************************************** @@ -1123,10 +1120,10 @@ part of 'test_lib.dart'; // Code for "class Customer" '''; -const _testGenNoLibrary = r''' +const _testGenNoLibrary = ''' +$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 part of 'test_lib.dart'; // ************************************************************************** @@ -1140,7 +1137,6 @@ part of 'test_lib.dart'; const _whitespaceTrimmed = r''' // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 part of 'test_lib.dart'; // ************************************************************************** diff --git a/source_gen/test/generator_for_annotation_test.dart b/source_gen/test/generator_for_annotation_test.dart index abd426a1..e78f6ad4 100644 --- a/source_gen/test/generator_for_annotation_test.dart +++ b/source_gen/test/generator_for_annotation_test.dart @@ -43,9 +43,9 @@ void main() { builder, _inputMap, outputs: { - 'a|lib/file.g.dart': r''' + 'a|lib/file.g.dart': ''' +$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND -// dart format width=80 // ************************************************************************** // Generator: Repeating @@ -129,8 +129,8 @@ void main() { }, outputs: { 'a|lib/file.g.dart': ''' -// GENERATED CODE - DO NOT MODIFY BY HAND $dartFormatWidth +// GENERATED CODE - DO NOT MODIFY BY HAND // ************************************************************************** // Generator: Deprecated @@ -166,8 +166,8 @@ $dartFormatWidth }, outputs: { 'a|lib/file.g.dart': ''' -// GENERATED CODE - DO NOT MODIFY BY HAND $dartFormatWidth +// GENERATED CODE - DO NOT MODIFY BY HAND // ************************************************************************** // Generator: Deprecated From 07adb5c6a9467b77eb3d58eb29795d3d84aff5d3 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Tue, 10 Dec 2024 00:23:42 +0000 Subject: [PATCH 2/2] 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, '*');