Skip to content

Commit 8116290

Browse files
authored
Add option to do custom code formatting (#147)
1 parent 8539dce commit 8116290

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.5.8
2+
3+
* Add `formatOutput` optional parameter to the `GeneratorBuilder` constructor.
4+
This is a lamda of the form `String formatOutput(String originalCode)` which
5+
allows you do do custom formatting.
6+
17
## 0.5.7
28

39
* Support for package:analyzer 0.30.0

lib/src/builder.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ import 'generated_output.dart';
1111
import 'generator.dart';
1212
import 'utils.dart';
1313

14+
typedef String OutputFormatter(String generatedCode);
15+
1416
class GeneratorBuilder extends Builder {
17+
final OutputFormatter formatOutput;
1518
final List<Generator> generators;
1619
final String generatedExtension;
1720
final bool isStandalone;
1821

1922
GeneratorBuilder(this.generators,
20-
{this.generatedExtension: '.g.dart', this.isStandalone: false}) {
23+
{OutputFormatter formatOutput,
24+
this.generatedExtension: '.g.dart',
25+
this.isStandalone: false})
26+
: formatOutput = formatOutput ?? _formatter.format {
2127
// TODO: validate that generatedExtension starts with a `.'
2228
// not null, empty, etc
2329
if (this.isStandalone && this.generators.length > 1) {
@@ -72,9 +78,8 @@ class GeneratorBuilder extends Builder {
7278

7379
var genPartContent = contentBuffer.toString();
7480

75-
var formatter = new DartFormatter();
7681
try {
77-
genPartContent = formatter.format(genPartContent);
82+
genPartContent = formatOutput(genPartContent);
7883
} catch (e, stack) {
7984
log.severe(
8085
'Error formatting generated source code for ${library.identifier}'
@@ -124,6 +129,8 @@ Stream<GeneratedOutput> _processUnitMember(
124129
}
125130
}
126131

132+
final _formatter = new DartFormatter();
133+
127134
const _topHeader = '''// GENERATED CODE - DO NOT MODIFY BY HAND
128135
129136
''';

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: 0.5.7
2+
version: 0.5.8
33
author: Dart Team <[email protected]>
44
description: Automatic sourcecode generation for Dart
55
homepage: https://github.com/dart-lang/source_gen

test/builder_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:source_gen/source_gen.dart';
1111
import 'package:test/test.dart';
1212

1313
import 'src/comment_generator.dart';
14+
import 'src/unformatted_code_generator.dart';
1415

1516
void main() {
1617
test('Simple Generator test', _simpleTest);
@@ -93,6 +94,41 @@ void main() {
9394
'$pkgName|lib/test_lib.g.dart': _testGenPartContentError,
9495
});
9596
});
97+
98+
test('defaults to formatting generated code with the DartFormatter',
99+
() async {
100+
await testBuilder(new GeneratorBuilder([new UnformattedCodeGenerator()]),
101+
{'$pkgName|lib/a.dart': 'library a; part "a.part.dart";'},
102+
generateFor: new Set.from(['$pkgName|lib/a.dart']),
103+
outputs: {
104+
'$pkgName|lib/a.g.dart':
105+
contains(UnformattedCodeGenerator.formattedCode),
106+
});
107+
});
108+
109+
test('can skip formatting with a trivial lambda', () async {
110+
await testBuilder(
111+
new GeneratorBuilder([new UnformattedCodeGenerator()],
112+
formatOutput: (s) => s),
113+
{'$pkgName|lib/a.dart': 'library a; part "a.part.dart";'},
114+
generateFor: new Set.from(['$pkgName|lib/a.dart']),
115+
outputs: {
116+
'$pkgName|lib/a.g.dart':
117+
contains(UnformattedCodeGenerator.unformattedCode),
118+
});
119+
});
120+
121+
test('can pass a custom formatter with formatOutput', () async {
122+
var customOutput = 'final String hello = "hello";';
123+
await testBuilder(
124+
new GeneratorBuilder([new UnformattedCodeGenerator()],
125+
formatOutput: (_) => customOutput),
126+
{'$pkgName|lib/a.dart': 'library a; part "a.part.dart";'},
127+
generateFor: new Set.from(['$pkgName|lib/a.dart']),
128+
outputs: {
129+
'$pkgName|lib/a.g.dart': contains(customOutput),
130+
});
131+
});
96132
}
97133

98134
Future _simpleTest() => _generateTest(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:source_gen/source_gen.dart';
9+
10+
/// Generates a single-line of unformatted code.
11+
class UnformattedCodeGenerator extends Generator {
12+
const UnformattedCodeGenerator();
13+
14+
@override
15+
Future<String> generate(Element element, _) async {
16+
return unformattedCode;
17+
}
18+
19+
static const formattedCode = '''
20+
void hello() => print('hello');
21+
''';
22+
23+
static const unformattedCode = '''
24+
void hello ()=> print('hello');
25+
''';
26+
}

0 commit comments

Comments
 (0)