Skip to content

Commit f8f5d25

Browse files
committed
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.
1 parent 2a6be7e commit f8f5d25

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

source_gen/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.1-WIP
2+
3+
- Keep `// GENERATED FILE` comments on the first line.
4+
15
## 2.0.0
26

37
- **Breaking Change**: Change `formatOutput` function to accept a language

source_gen/lib/src/builder.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,13 @@ String _defaultFormatOutput(String code, Version version) =>
414414

415415
/// Prefixes a dart format width and formats [code].
416416
String _defaultFormatUnit(String code, Version version) {
417-
code = '$dartFormatWidth\n$code';
417+
if (code.startsWith('$defaultFileHeader\n')) {
418+
code = '$defaultFileHeader\n'
419+
'$dartFormatWidth\n'
420+
'${code.substring(defaultFileHeader.length)}';
421+
} else {
422+
code = '$dartFormatWidth\n$code';
423+
}
418424
return _defaultFormatOutput(code, version);
419425
}
420426

source_gen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 2.0.0
2+
version: 2.0.1-WIP
33
description: >-
44
Source code generation builders and utilities for the Dart build system
55
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen

source_gen/test/builder_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,8 @@ final int foo = 42
10671067
''';
10681068

10691069
const _testGenPartContent = '''
1070-
$dartFormatWidth
10711070
// GENERATED CODE - DO NOT MODIFY BY HAND
1071+
$dartFormatWidth
10721072
10731073
part of 'test_lib.dart';
10741074
@@ -1081,8 +1081,8 @@ part of 'test_lib.dart';
10811081
''';
10821082

10831083
const _testGenPartContentForLibrary = '''
1084-
$dartFormatWidth
10851084
// GENERATED CODE - DO NOT MODIFY BY HAND
1085+
$dartFormatWidth
10861086
10871087
part of 'test_lib.dart';
10881088
@@ -1094,8 +1094,8 @@ part of 'test_lib.dart';
10941094
''';
10951095

10961096
const _testGenStandaloneContent = '''
1097-
$dartFormatWidth
10981097
// GENERATED CODE - DO NOT MODIFY BY HAND
1098+
$dartFormatWidth
10991099
11001100
// **************************************************************************
11011101
// CommentGenerator
@@ -1106,8 +1106,8 @@ $dartFormatWidth
11061106
''';
11071107

11081108
const _testGenPartContentForClassesAndLibrary = '''
1109-
$dartFormatWidth
11101109
// GENERATED CODE - DO NOT MODIFY BY HAND
1110+
$dartFormatWidth
11111111
11121112
part of 'test_lib.dart';
11131113
@@ -1121,8 +1121,8 @@ part of 'test_lib.dart';
11211121
''';
11221122

11231123
const _testGenNoLibrary = '''
1124-
$dartFormatWidth
11251124
// GENERATED CODE - DO NOT MODIFY BY HAND
1125+
$dartFormatWidth
11261126
11271127
part of 'test_lib.dart';
11281128

source_gen/test/generator_for_annotation_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void main() {
4444
_inputMap,
4545
outputs: {
4646
'a|lib/file.g.dart': '''
47-
$dartFormatWidth
4847
// GENERATED CODE - DO NOT MODIFY BY HAND
48+
$dartFormatWidth
4949
5050
// **************************************************************************
5151
// Generator: Repeating
@@ -129,8 +129,8 @@ $dartFormatWidth
129129
},
130130
outputs: {
131131
'a|lib/file.g.dart': '''
132-
$dartFormatWidth
133132
// GENERATED CODE - DO NOT MODIFY BY HAND
133+
$dartFormatWidth
134134
135135
// **************************************************************************
136136
// Generator: Deprecated
@@ -166,8 +166,8 @@ $dartFormatWidth
166166
},
167167
outputs: {
168168
'a|lib/file.g.dart': '''
169-
$dartFormatWidth
170169
// GENERATED CODE - DO NOT MODIFY BY HAND
170+
$dartFormatWidth
171171
172172
// **************************************************************************
173173
// Generator: Deprecated

0 commit comments

Comments
 (0)