Skip to content

Commit 2b7c60a

Browse files
authored
[pigeon] Change generator formatting to closer match formatter output (#3072)
* dart and some java * java * objc header * objc source * newln * loopable new lines * better counting * cpp and redo previous changes * changelog * nest and nits * addScoped * rewrite description of newln * regex tests
1 parent a4bc6ac commit 2b7c60a

File tree

29 files changed

+642
-569
lines changed

29 files changed

+642
-569
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 7.0.1
2+
3+
* [generator_tools] adds `newln` method for adding empty lines and ending lines.
4+
* Updates generators to more closely match Flutter formatter tool output.
5+
16
## 7.0.0
27

38
* [java] **BREAKING CHANGE**: Makes data classes final.

packages/pigeon/lib/cpp_generator.dart

Lines changed: 65 additions & 65 deletions
Large diffs are not rendered by default.

packages/pigeon/lib/dart_generator.dart

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
8686
indent.writeln(
8787
'// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import',
8888
);
89-
indent.addln('');
89+
indent.newln();
9090
}
9191

9292
@override
@@ -96,7 +96,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
9696
indent.writeln(
9797
"import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;",
9898
);
99-
indent.addln('');
99+
indent.newln();
100100
indent.writeln(
101101
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;");
102102
indent.writeln("import 'package:flutter/services.dart';");
@@ -105,11 +105,11 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
105105
@override
106106
void writeEnum(
107107
DartOptions generatorOptions, Root root, Indent indent, Enum anEnum) {
108-
indent.writeln('');
108+
indent.newln();
109109
addDocumentationComments(
110110
indent, anEnum.documentationComments, _docCommentSpec);
111111
indent.write('enum ${anEnum.name} ');
112-
indent.scoped('{', '}', () {
112+
indent.addScoped('{', '}', () {
113113
for (final EnumMember member in anEnum.members) {
114114
addDocumentationComments(
115115
indent, member.documentationComments, _docCommentSpec);
@@ -126,33 +126,33 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
126126
final Set<String> customEnumNames =
127127
root.enums.map((Enum x) => x.name).toSet();
128128

129-
indent.writeln('');
129+
indent.newln();
130130
addDocumentationComments(
131131
indent, klass.documentationComments, _docCommentSpec);
132132

133133
indent.write('class ${klass.name} ');
134-
indent.scoped('{', '}', () {
134+
indent.addScoped('{', '}', () {
135135
_writeConstructor(indent, klass);
136-
indent.addln('');
136+
indent.newln();
137137
for (final NamedType field in getFieldsInSerializationOrder(klass)) {
138138
addDocumentationComments(
139139
indent, field.documentationComments, _docCommentSpec);
140140

141141
final String datatype = _addGenericTypesNullable(field.type);
142142
indent.writeln('$datatype ${field.name};');
143-
indent.writeln('');
143+
indent.newln();
144144
}
145145
writeClassEncode(generatorOptions, root, indent, klass, customClassNames,
146146
customEnumNames);
147-
indent.writeln('');
147+
indent.newln();
148148
writeClassDecode(generatorOptions, root, indent, klass, customClassNames,
149149
customEnumNames);
150150
});
151151
}
152152

153153
void _writeConstructor(Indent indent, Class klass) {
154154
indent.write(klass.name);
155-
indent.scoped('({', '});', () {
155+
indent.addScoped('({', '});', () {
156156
for (final NamedType field in getFieldsInSerializationOrder(klass)) {
157157
final String required = field.type.isNullable ? '' : 'required ';
158158
indent.writeln('${required}this.${field.name},');
@@ -170,11 +170,11 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
170170
Set<String> customEnumNames,
171171
) {
172172
indent.write('Object encode() ');
173-
indent.scoped('{', '}', () {
173+
indent.addScoped('{', '}', () {
174174
indent.write(
175175
'return <Object?>',
176176
);
177-
indent.scoped('[', '];', () {
177+
indent.addScoped('[', '];', () {
178178
for (final NamedType field in getFieldsInSerializationOrder(klass)) {
179179
final String conditional = field.type.isNullable ? '?' : '';
180180
if (customClassNames.contains(field.type.baseName)) {
@@ -207,27 +207,25 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
207207
if (customClassNames.contains(field.type.baseName)) {
208208
final String nonNullValue =
209209
'${field.type.baseName}.decode($resultAt! as List<Object?>)';
210-
indent.format(
211-
field.type.isNullable
212-
? '''
210+
if (field.type.isNullable) {
211+
indent.format('''
213212
$resultAt != null
214213
\t\t? $nonNullValue
215-
\t\t: null'''
216-
: nonNullValue,
217-
leadingSpace: false,
218-
trailingNewline: false);
214+
\t\t: null''', leadingSpace: false, trailingNewline: false);
215+
} else {
216+
indent.add(nonNullValue);
217+
}
219218
} else if (customEnumNames.contains(field.type.baseName)) {
220219
final String nonNullValue =
221220
'${field.type.baseName}.values[$resultAt! as int]';
222-
indent.format(
223-
field.type.isNullable
224-
? '''
221+
if (field.type.isNullable) {
222+
indent.format('''
225223
$resultAt != null
226224
\t\t? $nonNullValue
227-
\t\t: null'''
228-
: nonNullValue,
229-
leadingSpace: false,
230-
trailingNewline: false);
225+
\t\t: null''', leadingSpace: false, trailingNewline: false);
226+
} else {
227+
indent.add(nonNullValue);
228+
}
231229
} else if (field.type.typeArguments.isNotEmpty) {
232230
final String genericType = _makeGenericTypeArguments(field.type);
233231
final String castCall = _makeGenericCastCall(field.type);
@@ -252,10 +250,10 @@ $resultAt != null
252250
indent.write(
253251
'static ${klass.name} decode(Object result) ',
254252
);
255-
indent.scoped('{', '}', () {
253+
indent.addScoped('{', '}', () {
256254
indent.writeln('result as List<Object?>;');
257255
indent.write('return ${klass.name}');
258-
indent.scoped('(', ');', () {
256+
indent.addScoped('(', ');', () {
259257
enumerate(getFieldsInSerializationOrder(klass),
260258
(int index, final NamedType field) {
261259
indent.write('${field.name}: ');
@@ -292,15 +290,15 @@ $resultAt != null
292290
codecName = _getCodecName(api);
293291
_writeCodec(indent, codecName, api, root);
294292
}
295-
indent.addln('');
293+
indent.newln();
296294
addDocumentationComments(
297295
indent, api.documentationComments, _docCommentSpec);
298296

299297
indent.write('abstract class ${api.name} ');
300-
indent.scoped('{', '}', () {
298+
indent.addScoped('{', '}', () {
301299
indent
302300
.writeln('static const MessageCodec<Object?> codec = $codecName();');
303-
indent.addln('');
301+
indent.newln();
304302
for (final Method func in api.methods) {
305303
addDocumentationComments(
306304
indent, func.documentationComments, _docCommentSpec);
@@ -314,14 +312,14 @@ $resultAt != null
314312
_getArgumentName,
315313
);
316314
indent.writeln('$returnType ${func.name}($argSignature);');
317-
indent.writeln('');
315+
indent.newln();
318316
}
319317
indent.write(
320318
'static void setup(${api.name}? api, {BinaryMessenger? binaryMessenger}) ');
321-
indent.scoped('{', '}', () {
319+
indent.addScoped('{', '}', () {
322320
for (final Method func in api.methods) {
323321
indent.write('');
324-
indent.scoped('{', '}', () {
322+
indent.addScoped('{', '}', () {
325323
indent.writeln(
326324
'final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(',
327325
);
@@ -337,15 +335,15 @@ $resultAt != null
337335
final String messageHandlerSetter =
338336
isMockHandler ? 'setMockMessageHandler' : 'setMessageHandler';
339337
indent.write('if (api == null) ');
340-
indent.scoped('{', '}', () {
338+
indent.addScoped('{', '}', () {
341339
indent.writeln('channel.$messageHandlerSetter(null);');
342340
}, addTrailingNewline: false);
343341
indent.add(' else ');
344-
indent.scoped('{', '}', () {
342+
indent.addScoped('{', '}', () {
345343
indent.write(
346344
'channel.$messageHandlerSetter((Object? message) async ',
347345
);
348-
indent.scoped('{', '});', () {
346+
indent.addScoped('{', '});', () {
349347
final String returnType =
350348
_addGenericTypesNullable(func.returnType);
351349
final bool isAsync = func.isAsynchronous;
@@ -382,8 +380,9 @@ $resultAt != null
382380
'$leftHandSide = ($argsArray[$count] as $genericArgType?)${castCall.isEmpty ? '' : '?$castCall'};');
383381
}
384382
if (!arg.type.isNullable) {
383+
indent.writeln('assert($argName != null,');
385384
indent.writeln(
386-
"assert($argName != null, 'Argument for $channelName was null, expected non-null $argType.');");
385+
" 'Argument for $channelName was null, expected non-null $argType.');");
387386
}
388387
});
389388
final Iterable<String> argNames =
@@ -446,12 +445,12 @@ $resultAt != null
446445
codecName = _getCodecName(api);
447446
_writeCodec(indent, codecName, api, root);
448447
}
449-
indent.addln('');
448+
indent.newln();
450449
bool first = true;
451450
addDocumentationComments(
452451
indent, api.documentationComments, _docCommentSpec);
453452
indent.write('class ${api.name} ');
454-
indent.scoped('{', '}', () {
453+
indent.addScoped('{', '}', () {
455454
indent.format('''
456455
/// Constructor for [${api.name}]. The [binaryMessenger] named argument is
457456
/// available for dependency injection. If it is left null, the default
@@ -463,10 +462,10 @@ final BinaryMessenger? _binaryMessenger;
463462

464463
indent
465464
.writeln('static const MessageCodec<Object?> codec = $codecName();');
466-
indent.addln('');
465+
indent.newln();
467466
for (final Method func in api.methods) {
468467
if (!first) {
469-
indent.writeln('');
468+
indent.newln();
470469
} else {
471470
first = false;
472471
}
@@ -494,7 +493,7 @@ final BinaryMessenger? _binaryMessenger;
494493
indent.write(
495494
'Future<${_addGenericTypesNullable(func.returnType)}> ${func.name}($argSignature) async ',
496495
);
497-
indent.scoped('{', '}', () {
496+
indent.addScoped('{', '}', () {
498497
final String channelName = makeChannelName(api, func);
499498
indent.writeln(
500499
'final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(');
@@ -585,7 +584,6 @@ if (replyList == null) {
585584
dartHostTestHandler: api.dartHostTestHandler,
586585
documentationComments: api.documentationComments,
587586
);
588-
indent.writeln('');
589587
writeFlutterApi(
590588
generatorOptions,
591589
root,
@@ -621,7 +619,7 @@ if (replyList == null) {
621619
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;");
622620
indent.writeln("import 'package:flutter/services.dart';");
623621
indent.writeln("import 'package:flutter_test/flutter_test.dart';");
624-
indent.writeln('');
622+
indent.newln();
625623
}
626624
}
627625

@@ -642,33 +640,34 @@ String _getCodecName(Api api) => '_${api.name}Codec';
642640
void _writeCodec(Indent indent, String codecName, Api api, Root root) {
643641
assert(getCodecClasses(api, root).isNotEmpty);
644642
final Iterable<EnumeratedClass> codecClasses = getCodecClasses(api, root);
643+
indent.newln();
645644
indent.write('class $codecName extends $_standardMessageCodec');
646-
indent.scoped(' {', '}', () {
645+
indent.addScoped(' {', '}', () {
647646
indent.writeln('const $codecName();');
648647
indent.writeln('@override');
649648
indent.write('void writeValue(WriteBuffer buffer, Object? value) ');
650-
indent.scoped('{', '}', () {
649+
indent.addScoped('{', '}', () {
651650
enumerate(codecClasses, (int index, final EnumeratedClass customClass) {
652651
final String ifValue = 'if (value is ${customClass.name}) ';
653652
if (index == 0) {
654653
indent.write('');
655654
}
656655
indent.add(ifValue);
657-
indent.scoped('{', '} else ', () {
656+
indent.addScoped('{', '} else ', () {
658657
indent.writeln('buffer.putUint8(${customClass.enumeration});');
659658
indent.writeln('writeValue(buffer, value.encode());');
660659
}, addTrailingNewline: false);
661660
});
662-
indent.scoped('{', '}', () {
661+
indent.addScoped('{', '}', () {
663662
indent.writeln('super.writeValue(buffer, value);');
664663
});
665664
});
666-
indent.writeln('');
665+
indent.newln();
667666
indent.writeln('@override');
668667
indent.write('Object? readValueOfType(int type, ReadBuffer buffer) ');
669-
indent.scoped('{', '}', () {
668+
indent.addScoped('{', '}', () {
670669
indent.write('switch (type) ');
671-
indent.scoped('{', '}', () {
670+
indent.addScoped('{', '}', () {
672671
for (final EnumeratedClass customClass in codecClasses) {
673672
indent.write('case ${customClass.enumeration}: ');
674673
indent.writeScoped('', '', () {
@@ -677,7 +676,7 @@ void _writeCodec(Indent indent, String codecName, Api api, Root root) {
677676
});
678677
}
679678
indent.writeln('default:');
680-
indent.scoped('', '', () {
679+
indent.nest(1, () {
681680
indent.writeln('return super.readValueOfType(type, buffer);');
682681
});
683682
});

packages/pigeon/lib/generator_tools.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'dart:mirrors';
99
import 'ast.dart';
1010

1111
/// The current version of pigeon. This must match the version in pubspec.yaml.
12-
const String pigeonVersion = '7.0.0';
12+
const String pigeonVersion = '7.0.1';
1313

1414
/// Read all the content from [stdin] to a String.
1515
String readStdin() {
@@ -83,7 +83,7 @@ class Indent {
8383

8484
/// Scoped increase of the ident level. For the execution of [func] the
8585
/// indentation will be incremented.
86-
void scoped(
86+
void addScoped(
8787
String? begin,
8888
String? end,
8989
Function func, {
@@ -102,14 +102,14 @@ class Indent {
102102
}
103103
}
104104

105-
/// Like `scoped` but writes the current indentation level.
105+
/// Like `addScoped` but writes the current indentation level.
106106
void writeScoped(
107107
String? begin,
108108
String end,
109109
Function func, {
110110
bool addTrailingNewline = true,
111111
}) {
112-
scoped(str() + (begin ?? ''), end, func,
112+
addScoped(str() + (begin ?? ''), end, func,
113113
addTrailingNewline: addTrailingNewline);
114114
}
115115

@@ -144,6 +144,13 @@ class Indent {
144144
void add(String text) {
145145
_sink.write(text);
146146
}
147+
148+
/// Adds [lines] number of newlines.
149+
void newln([int lines = 1]) {
150+
for (; lines > 0; lines--) {
151+
_sink.write(newline);
152+
}
153+
}
147154
}
148155

149156
/// Create the generated channel name for a [func] on a [api].

0 commit comments

Comments
 (0)