From 9f95ac3be458fed0a7dbdbe9c1b7121a22c14931 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Thu, 12 Dec 2024 13:50:29 -0800 Subject: [PATCH] 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. --- example_usage/lib/library_source.g.dart | 1 - example_usage/lib/library_source.info.dart | 2 +- source_gen/CHANGELOG.md | 3 +- source_gen/lib/builder.dart | 1 - source_gen/lib/src/builder.dart | 20 ++++++----- source_gen/test/builder_test.dart | 34 ++++++++----------- .../test/generator_for_annotation_test.dart | 8 ++--- 7 files changed, 34 insertions(+), 35 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 f21265b3..25df1300 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 files generated by a + `LibraryBuilder` or `PartBuilder` and 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..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, @@ -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) { @@ -239,7 +236,7 @@ class SharedPartBuilder extends _Builder { SharedPartBuilder( super.generators, String partId, { - super.formatOutput, + super.formatOutput = _defaultFormatOutput, super.additionalOutputExtensions, super.allowSyntaxErrors, super.writeDescriptions, @@ -301,7 +298,7 @@ class PartBuilder extends _Builder { PartBuilder( super.generators, String generatedExtension, { - super.formatOutput, + super.formatOutput = _defaultFormatUnit, super.additionalOutputExtensions, super.writeDescriptions, super.header, @@ -328,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 @@ -344,7 +342,7 @@ class LibraryBuilder extends _Builder { /// libraries. LibraryBuilder( Generator generator, { - super.formatOutput, + super.formatOutput = _defaultFormatUnit, super.generatedExtension, super.additionalOutputExtensions, super.writeDescriptions, @@ -414,6 +412,12 @@ const defaultFileHeader = '// GENERATED CODE - DO NOT MODIFY BY HAND'; 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 _defaultFormatOutput(code, version); +} + final _headerLine = '// '.padRight(77, '*'); const partIdRegExpLiteral = r'[A-Za-z_\d-]+'; 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