Skip to content

Only add dart format width comment on format #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion source_gen/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ class CombiningBuilder implements Builder {

final output = '''
$defaultFileHeader
$dartFormatWidth
${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble
part of '$partOfUri';

Expand Down
20 changes: 12 additions & 8 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 @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -239,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 @@ -301,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 @@ -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
Expand All @@ -344,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 @@ -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-]+';
Expand Down
34 changes: 15 additions & 19 deletions source_gen/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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// ***'),
),
},
);
Expand Down Expand Up @@ -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'),
),
},
);
Expand All @@ -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')),
},
);
});
Expand All @@ -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'),
),
},
);
Expand Down Expand Up @@ -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';"),
),
},
);
Expand Down Expand Up @@ -570,7 +569,6 @@ part "a.foo.dart";''',
const CombiningBuilder(),
{
'$_pkgName|lib/a.dart': '''
$dartFormatWidth
// @dart=2.12
library a;
part "a.g.dart";
Expand All @@ -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';
Expand Down Expand Up @@ -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';

// **************************************************************************
Expand All @@ -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';

// **************************************************************************
Expand All @@ -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
Expand All @@ -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';

// **************************************************************************
Expand All @@ -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';

// **************************************************************************
Expand All @@ -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';

// **************************************************************************
Expand Down
8 changes: 4 additions & 4 deletions source_gen/test/generator_for_annotation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading