From f8f5d256df3199d610db7a379abde8debd04b19e Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Tue, 17 Jun 2025 21:47:23 +0000 Subject: [PATCH] Keep generated file comments on the first line Let linquist continue to find the generated file marker on the first line, the dart format width comment is allowed to be on any line. Look for the generated file line exactly and when it starts the file insert the dart format comment following it. --- source_gen/CHANGELOG.md | 4 ++++ source_gen/lib/src/builder.dart | 8 +++++++- source_gen/pubspec.yaml | 2 +- source_gen/test/builder_test.dart | 10 +++++----- source_gen/test/generator_for_annotation_test.dart | 6 +++--- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/source_gen/CHANGELOG.md b/source_gen/CHANGELOG.md index f299d63a..ff2875ec 100644 --- a/source_gen/CHANGELOG.md +++ b/source_gen/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.1-WIP + +- Keep `// GENERATED FILE` comments on the first line. + ## 2.0.0 - **Breaking Change**: Change `formatOutput` function to accept a language diff --git a/source_gen/lib/src/builder.dart b/source_gen/lib/src/builder.dart index 53b8c2f7..c17d87d7 100644 --- a/source_gen/lib/src/builder.dart +++ b/source_gen/lib/src/builder.dart @@ -414,7 +414,13 @@ String _defaultFormatOutput(String code, Version version) => /// Prefixes a dart format width and formats [code]. String _defaultFormatUnit(String code, Version version) { - code = '$dartFormatWidth\n$code'; + if (code.startsWith('$defaultFileHeader\n')) { + code = '$defaultFileHeader\n' + '$dartFormatWidth\n' + '${code.substring(defaultFileHeader.length)}'; + } else { + code = '$dartFormatWidth\n$code'; + } return _defaultFormatOutput(code, version); } diff --git a/source_gen/pubspec.yaml b/source_gen/pubspec.yaml index d650bb5c..3554ba73 100644 --- a/source_gen/pubspec.yaml +++ b/source_gen/pubspec.yaml @@ -1,5 +1,5 @@ name: source_gen -version: 2.0.0 +version: 2.0.1-WIP description: >- Source code generation builders and utilities for the Dart build system repository: https://github.com/dart-lang/source_gen/tree/master/source_gen diff --git a/source_gen/test/builder_test.dart b/source_gen/test/builder_test.dart index 87261476..791bbcb7 100644 --- a/source_gen/test/builder_test.dart +++ b/source_gen/test/builder_test.dart @@ -1067,8 +1067,8 @@ final int foo = 42 '''; const _testGenPartContent = ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth part of 'test_lib.dart'; @@ -1081,8 +1081,8 @@ part of 'test_lib.dart'; '''; const _testGenPartContentForLibrary = ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth part of 'test_lib.dart'; @@ -1094,8 +1094,8 @@ part of 'test_lib.dart'; '''; const _testGenStandaloneContent = ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth // ************************************************************************** // CommentGenerator @@ -1106,8 +1106,8 @@ $dartFormatWidth '''; const _testGenPartContentForClassesAndLibrary = ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth part of 'test_lib.dart'; @@ -1121,8 +1121,8 @@ part of 'test_lib.dart'; '''; const _testGenNoLibrary = ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth 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 e78f6ad4..1ba7512d 100644 --- a/source_gen/test/generator_for_annotation_test.dart +++ b/source_gen/test/generator_for_annotation_test.dart @@ -44,8 +44,8 @@ void main() { _inputMap, outputs: { 'a|lib/file.g.dart': ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth // ************************************************************************** // Generator: Repeating @@ -129,8 +129,8 @@ $dartFormatWidth }, outputs: { 'a|lib/file.g.dart': ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth // ************************************************************************** // Generator: Deprecated @@ -166,8 +166,8 @@ $dartFormatWidth }, outputs: { 'a|lib/file.g.dart': ''' -$dartFormatWidth // GENERATED CODE - DO NOT MODIFY BY HAND +$dartFormatWidth // ************************************************************************** // Generator: Deprecated