Skip to content

Commit 9475e65

Browse files
authored
Keep generated file comments on the first line (#760)
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 e69abb4 commit 9475e65

File tree

6 files changed

+22
-11
lines changed

6 files changed

+22
-11
lines changed

example_usage/lib/library_source.info.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source_gen/CHANGELOG.md

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

37
- Prepare to stop using `dart:mirrors`: deprecate `TypeChecker.fromRuntime`.

source_gen/lib/src/builder.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,14 @@ String _defaultFormatOutput(String code, Version version) =>
433433

434434
/// Prefixes a dart format width and formats [code].
435435
String _defaultFormatUnit(String code, Version version) {
436-
code = '$dartFormatWidth\n$code';
436+
if (code.startsWith('$defaultFileHeader\n')) {
437+
code =
438+
'$defaultFileHeader\n'
439+
'$dartFormatWidth\n'
440+
'${code.substring(defaultFileHeader.length)}';
441+
} else {
442+
code = '$dartFormatWidth\n$code';
443+
}
437444
return _defaultFormatOutput(code, version);
438445
}
439446

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: 3.1.0
2+
version: 3.1.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
@@ -1058,8 +1058,8 @@ final int foo = 42
10581058
''';
10591059

10601060
const _testGenPartContent = '''
1061-
$dartFormatWidth
10621061
// GENERATED CODE - DO NOT MODIFY BY HAND
1062+
$dartFormatWidth
10631063
10641064
part of 'test_lib.dart';
10651065
@@ -1072,8 +1072,8 @@ part of 'test_lib.dart';
10721072
''';
10731073

10741074
const _testGenPartContentForLibrary = '''
1075-
$dartFormatWidth
10761075
// GENERATED CODE - DO NOT MODIFY BY HAND
1076+
$dartFormatWidth
10771077
10781078
part of 'test_lib.dart';
10791079
@@ -1085,8 +1085,8 @@ part of 'test_lib.dart';
10851085
''';
10861086

10871087
const _testGenStandaloneContent = '''
1088-
$dartFormatWidth
10891088
// GENERATED CODE - DO NOT MODIFY BY HAND
1089+
$dartFormatWidth
10901090
10911091
// **************************************************************************
10921092
// CommentGenerator
@@ -1097,8 +1097,8 @@ $dartFormatWidth
10971097
''';
10981098

10991099
const _testGenPartContentForClassesAndLibrary = '''
1100-
$dartFormatWidth
11011100
// GENERATED CODE - DO NOT MODIFY BY HAND
1101+
$dartFormatWidth
11021102
11031103
part of 'test_lib.dart';
11041104
@@ -1112,8 +1112,8 @@ part of 'test_lib.dart';
11121112
''';
11131113

11141114
const _testGenNoLibrary = '''
1115-
$dartFormatWidth
11161115
// GENERATED CODE - DO NOT MODIFY BY HAND
1116+
$dartFormatWidth
11171117
11181118
part of 'test_lib.dart';
11191119

source_gen/test/generator_for_annotation_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void main() {
5252
_inputMap,
5353
outputs: {
5454
'a|lib/file.g.dart': '''
55-
$dartFormatWidth
5655
// GENERATED CODE - DO NOT MODIFY BY HAND
56+
$dartFormatWidth
5757
5858
// **************************************************************************
5959
// Generator: Repeating
@@ -150,8 +150,8 @@ main() {}''',
150150
},
151151
outputs: {
152152
'a|lib/file.g.dart': '''
153-
$dartFormatWidth
154153
// GENERATED CODE - DO NOT MODIFY BY HAND
154+
$dartFormatWidth
155155
156156
// **************************************************************************
157157
// Generator: Deprecated
@@ -195,8 +195,8 @@ $dartFormatWidth
195195
},
196196
outputs: {
197197
'a|lib/file.g.dart': '''
198-
$dartFormatWidth
199198
// GENERATED CODE - DO NOT MODIFY BY HAND
199+
$dartFormatWidth
200200
201201
// **************************************************************************
202202
// Generator: Deprecated

0 commit comments

Comments
 (0)