Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 7.0.1

* [generator_tools] adds `newln` method for adding empty lines and ending lines.
* Updates generators to more closely match Flutter formatter tool output.

## 7.0.0

* [java] **BREAKING CHANGE**: Makes data classes final.
Expand Down
130 changes: 65 additions & 65 deletions packages/pigeon/lib/cpp_generator.dart

Large diffs are not rendered by default.

105 changes: 52 additions & 53 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
indent.writeln(
'// 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',
);
indent.addln('');
indent.newln();
}

@override
Expand All @@ -96,7 +96,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
indent.writeln(
"import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;",
);
indent.addln('');
indent.newln();
indent.writeln(
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;");
indent.writeln("import 'package:flutter/services.dart';");
Expand All @@ -105,11 +105,11 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
@override
void writeEnum(
DartOptions generatorOptions, Root root, Indent indent, Enum anEnum) {
indent.writeln('');
indent.newln();
addDocumentationComments(
indent, anEnum.documentationComments, _docCommentSpec);
indent.write('enum ${anEnum.name} ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
for (final EnumMember member in anEnum.members) {
addDocumentationComments(
indent, member.documentationComments, _docCommentSpec);
Expand All @@ -126,33 +126,33 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
final Set<String> customEnumNames =
root.enums.map((Enum x) => x.name).toSet();

indent.writeln('');
indent.newln();
addDocumentationComments(
indent, klass.documentationComments, _docCommentSpec);

indent.write('class ${klass.name} ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
_writeConstructor(indent, klass);
indent.addln('');
indent.newln();
for (final NamedType field in getFieldsInSerializationOrder(klass)) {
addDocumentationComments(
indent, field.documentationComments, _docCommentSpec);

final String datatype = _addGenericTypesNullable(field.type);
indent.writeln('$datatype ${field.name};');
indent.writeln('');
indent.newln();
}
writeClassEncode(generatorOptions, root, indent, klass, customClassNames,
customEnumNames);
indent.writeln('');
indent.newln();
writeClassDecode(generatorOptions, root, indent, klass, customClassNames,
customEnumNames);
});
}

void _writeConstructor(Indent indent, Class klass) {
indent.write(klass.name);
indent.scoped('({', '});', () {
indent.addScoped('({', '});', () {
for (final NamedType field in getFieldsInSerializationOrder(klass)) {
final String required = field.type.isNullable ? '' : 'required ';
indent.writeln('${required}this.${field.name},');
Expand All @@ -170,11 +170,11 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
Set<String> customEnumNames,
) {
indent.write('Object encode() ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.write(
'return <Object?>',
);
indent.scoped('[', '];', () {
indent.addScoped('[', '];', () {
for (final NamedType field in getFieldsInSerializationOrder(klass)) {
final String conditional = field.type.isNullable ? '?' : '';
if (customClassNames.contains(field.type.baseName)) {
Expand Down Expand Up @@ -207,27 +207,25 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
if (customClassNames.contains(field.type.baseName)) {
final String nonNullValue =
'${field.type.baseName}.decode($resultAt! as List<Object?>)';
indent.format(
field.type.isNullable
? '''
if (field.type.isNullable) {
indent.format('''
$resultAt != null
\t\t? $nonNullValue
\t\t: null'''
: nonNullValue,
leadingSpace: false,
trailingNewline: false);
\t\t: null''', leadingSpace: false, trailingNewline: false);
} else {
indent.add(nonNullValue);
}
} else if (customEnumNames.contains(field.type.baseName)) {
final String nonNullValue =
'${field.type.baseName}.values[$resultAt! as int]';
indent.format(
field.type.isNullable
? '''
if (field.type.isNullable) {
indent.format('''
$resultAt != null
\t\t? $nonNullValue
\t\t: null'''
: nonNullValue,
leadingSpace: false,
trailingNewline: false);
\t\t: null''', leadingSpace: false, trailingNewline: false);
} else {
indent.add(nonNullValue);
}
} else if (field.type.typeArguments.isNotEmpty) {
final String genericType = _makeGenericTypeArguments(field.type);
final String castCall = _makeGenericCastCall(field.type);
Expand All @@ -252,10 +250,10 @@ $resultAt != null
indent.write(
'static ${klass.name} decode(Object result) ',
);
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.writeln('result as List<Object?>;');
indent.write('return ${klass.name}');
indent.scoped('(', ');', () {
indent.addScoped('(', ');', () {
enumerate(getFieldsInSerializationOrder(klass),
(int index, final NamedType field) {
indent.write('${field.name}: ');
Expand Down Expand Up @@ -292,15 +290,15 @@ $resultAt != null
codecName = _getCodecName(api);
_writeCodec(indent, codecName, api, root);
}
indent.addln('');
indent.newln();
addDocumentationComments(
indent, api.documentationComments, _docCommentSpec);

indent.write('abstract class ${api.name} ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent
.writeln('static const MessageCodec<Object?> codec = $codecName();');
indent.addln('');
indent.newln();
for (final Method func in api.methods) {
addDocumentationComments(
indent, func.documentationComments, _docCommentSpec);
Expand All @@ -314,14 +312,14 @@ $resultAt != null
_getArgumentName,
);
indent.writeln('$returnType ${func.name}($argSignature);');
indent.writeln('');
indent.newln();
}
indent.write(
'static void setup(${api.name}? api, {BinaryMessenger? binaryMessenger}) ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
for (final Method func in api.methods) {
indent.write('');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.writeln(
'final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(',
);
Expand All @@ -337,15 +335,15 @@ $resultAt != null
final String messageHandlerSetter =
isMockHandler ? 'setMockMessageHandler' : 'setMessageHandler';
indent.write('if (api == null) ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.writeln('channel.$messageHandlerSetter(null);');
}, addTrailingNewline: false);
indent.add(' else ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.write(
'channel.$messageHandlerSetter((Object? message) async ',
);
indent.scoped('{', '});', () {
indent.addScoped('{', '});', () {
final String returnType =
_addGenericTypesNullable(func.returnType);
final bool isAsync = func.isAsynchronous;
Expand Down Expand Up @@ -382,8 +380,9 @@ $resultAt != null
'$leftHandSide = ($argsArray[$count] as $genericArgType?)${castCall.isEmpty ? '' : '?$castCall'};');
}
if (!arg.type.isNullable) {
indent.writeln('assert($argName != null,');
indent.writeln(
"assert($argName != null, 'Argument for $channelName was null, expected non-null $argType.');");
" 'Argument for $channelName was null, expected non-null $argType.');");
}
});
final Iterable<String> argNames =
Expand Down Expand Up @@ -446,12 +445,12 @@ $resultAt != null
codecName = _getCodecName(api);
_writeCodec(indent, codecName, api, root);
}
indent.addln('');
indent.newln();
bool first = true;
addDocumentationComments(
indent, api.documentationComments, _docCommentSpec);
indent.write('class ${api.name} ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.format('''
/// Constructor for [${api.name}]. The [binaryMessenger] named argument is
/// available for dependency injection. If it is left null, the default
Expand All @@ -463,10 +462,10 @@ final BinaryMessenger? _binaryMessenger;

indent
.writeln('static const MessageCodec<Object?> codec = $codecName();');
indent.addln('');
indent.newln();
for (final Method func in api.methods) {
if (!first) {
indent.writeln('');
indent.newln();
} else {
first = false;
}
Expand Down Expand Up @@ -494,7 +493,7 @@ final BinaryMessenger? _binaryMessenger;
indent.write(
'Future<${_addGenericTypesNullable(func.returnType)}> ${func.name}($argSignature) async ',
);
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
final String channelName = makeChannelName(api, func);
indent.writeln(
'final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(');
Expand Down Expand Up @@ -585,7 +584,6 @@ if (replyList == null) {
dartHostTestHandler: api.dartHostTestHandler,
documentationComments: api.documentationComments,
);
indent.writeln('');
writeFlutterApi(
generatorOptions,
root,
Expand Down Expand Up @@ -621,7 +619,7 @@ if (replyList == null) {
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;");
indent.writeln("import 'package:flutter/services.dart';");
indent.writeln("import 'package:flutter_test/flutter_test.dart';");
indent.writeln('');
indent.newln();
}
}

Expand All @@ -642,33 +640,34 @@ String _getCodecName(Api api) => '_${api.name}Codec';
void _writeCodec(Indent indent, String codecName, Api api, Root root) {
assert(getCodecClasses(api, root).isNotEmpty);
final Iterable<EnumeratedClass> codecClasses = getCodecClasses(api, root);
indent.newln();
indent.write('class $codecName extends $_standardMessageCodec');
indent.scoped(' {', '}', () {
indent.addScoped(' {', '}', () {
indent.writeln('const $codecName();');
indent.writeln('@override');
indent.write('void writeValue(WriteBuffer buffer, Object? value) ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
enumerate(codecClasses, (int index, final EnumeratedClass customClass) {
final String ifValue = 'if (value is ${customClass.name}) ';
if (index == 0) {
indent.write('');
}
indent.add(ifValue);
indent.scoped('{', '} else ', () {
indent.addScoped('{', '} else ', () {
indent.writeln('buffer.putUint8(${customClass.enumeration});');
indent.writeln('writeValue(buffer, value.encode());');
}, addTrailingNewline: false);
});
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.writeln('super.writeValue(buffer, value);');
});
});
indent.writeln('');
indent.newln();
indent.writeln('@override');
indent.write('Object? readValueOfType(int type, ReadBuffer buffer) ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
indent.write('switch (type) ');
indent.scoped('{', '}', () {
indent.addScoped('{', '}', () {
for (final EnumeratedClass customClass in codecClasses) {
indent.write('case ${customClass.enumeration}: ');
indent.writeScoped('', '', () {
Expand All @@ -677,7 +676,7 @@ void _writeCodec(Indent indent, String codecName, Api api, Root root) {
});
}
indent.writeln('default:');
indent.scoped('', '', () {
indent.nest(1, () {
indent.writeln('return super.readValueOfType(type, buffer);');
});
});
Expand Down
15 changes: 11 additions & 4 deletions packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'dart:mirrors';
import 'ast.dart';

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

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

/// Scoped increase of the ident level. For the execution of [func] the
/// indentation will be incremented.
void scoped(
void addScoped(
String? begin,
String? end,
Function func, {
Expand All @@ -102,14 +102,14 @@ class Indent {
}
}

/// Like `scoped` but writes the current indentation level.
/// Like `addScoped` but writes the current indentation level.
void writeScoped(
String? begin,
String end,
Function func, {
bool addTrailingNewline = true,
}) {
scoped(str() + (begin ?? ''), end, func,
addScoped(str() + (begin ?? ''), end, func,
addTrailingNewline: addTrailingNewline);
}

Expand Down Expand Up @@ -144,6 +144,13 @@ class Indent {
void add(String text) {
_sink.write(text);
}

Comment thread
tarrinneal marked this conversation as resolved.
/// Adds [lines] number of newlines.
void newln([int lines = 1]) {
Comment thread
tarrinneal marked this conversation as resolved.
for (; lines > 0; lines--) {
_sink.write(newline);
}
}
}

/// Create the generated channel name for a [func] on a [api].
Expand Down
Loading