Skip to content

Commit

Permalink
Only write width to library files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
natebosch committed Dec 10, 2024
1 parent b5db86b commit 07adb5c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 0 additions & 1 deletion example_usage/lib/library_source.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example_usage/lib/library_source.info.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// MemberCountLibraryGenerator
Expand Down
4 changes: 2 additions & 2 deletions source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 12 additions & 7 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> additionalOutputExtensions = const [],
String? header,
Expand Down Expand Up @@ -236,7 +236,7 @@ class SharedPartBuilder extends _Builder {
SharedPartBuilder(
super.generators,
String partId, {
super.formatOutput,
super.formatOutput = _defaultFormatOutput,
super.additionalOutputExtensions,
super.allowSyntaxErrors,
super.writeDescriptions,
Expand Down Expand Up @@ -298,7 +298,7 @@ class PartBuilder extends _Builder {
PartBuilder(
super.generators,
String generatedExtension, {
super.formatOutput,
super.formatOutput = _defaultFormatUnit,
super.additionalOutputExtensions,
super.writeDescriptions,
super.header,
Expand All @@ -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
Expand All @@ -341,7 +342,7 @@ class LibraryBuilder extends _Builder {
/// libraries.
LibraryBuilder(
Generator generator, {
super.formatOutput,
super.formatOutput = _defaultFormatUnit,
super.generatedExtension,
super.additionalOutputExtensions,
super.writeDescriptions,
Expand Down Expand Up @@ -408,9 +409,13 @@ Future<bool> _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, '*');
Expand Down

0 comments on commit 07adb5c

Please sign in to comment.