-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[pigeon] add support for top level consts #12032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,11 @@ | |
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| static NSString *const PGNaStringConstant = @"stringConstantValue"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consts should not be inlined into headers in C. These should be |
||
| static const NSInteger PGNanIntConstant = 42; | ||
| static const double PGNaDoubleConstant = 3.14; | ||
| static const BOOL PGNaBoolConstant = YES; | ||
|
|
||
| typedef NS_ENUM(NSUInteger, PGNCode) { | ||
| PGNCodeOne = 0, | ||
| PGNCodeTwo = 1, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,6 +186,72 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> { | |
| indent.writeln("import 'package:meta/meta.dart' show immutable, protected, visibleForTesting;"); | ||
| } | ||
|
|
||
| @override | ||
| void writeConstants( | ||
| InternalDartOptions generatorOptions, | ||
| Root root, | ||
| Indent indent, { | ||
| required String dartPackageName, | ||
| }) { | ||
| if (root.constants.isEmpty) { | ||
| return; | ||
| } | ||
| indent.newln(); | ||
| for (final Constant constant in root.constants) { | ||
| addDocumentationComments(indent, constant.documentationComments, docCommentSpec); | ||
| final String formattedValue = _formatValue(constant.type.baseName, constant.value); | ||
| indent.writeln('const ${constant.type.baseName} ${constant.name} = $formattedValue;'); | ||
| } | ||
| } | ||
|
|
||
| String _formatValue(String type, Object value) { | ||
| if (type == 'String') { | ||
| return _makeDartStringLiteral(value.toString()); | ||
| } else { | ||
| return value.toString(); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Consider a single |
||
| } | ||
|
|
||
| String _makeDartStringLiteral(String valStr) { | ||
| final bool hasSpecial = | ||
| // ignore: use_raw_strings | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use a raw string for this one? |
||
| valStr.contains('\\') || | ||
| valStr.contains(r'$') || | ||
| // ignore: use_raw_strings | ||
| valStr.contains('\n') || | ||
| // ignore: use_raw_strings | ||
| valStr.contains('\r'); | ||
|
|
||
| if (!hasSpecial) { | ||
| if (!valStr.contains("'")) { | ||
| return "'$valStr'"; | ||
| } | ||
| if (!valStr.contains('"')) { | ||
| return '"$valStr"'; | ||
| } | ||
| return "r'''$valStr'''"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the string contains |
||
| } | ||
|
|
||
| if (!valStr.contains('\n') && !valStr.contains('\r')) { | ||
| if (!valStr.contains("'")) { | ||
| return "r'$valStr'"; | ||
| } | ||
| if (!valStr.contains('"')) { | ||
| return 'r"$valStr"'; | ||
| } | ||
| } | ||
|
|
||
| if (!valStr.contains("'''")) { | ||
| return "r'''$valStr'''"; | ||
| } | ||
| if (!valStr.contains('"""')) { | ||
| return 'r"""$valStr"""'; | ||
| } | ||
|
|
||
| final String escaped = escapeStringSingleQuotes(valStr); | ||
| return "'$escaped'"; | ||
| } | ||
|
|
||
| @override | ||
| void writeEnum( | ||
| InternalDartOptions generatorOptions, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ import 'generator.dart'; | |
| /// The current version of pigeon. | ||
| /// | ||
| /// This must match the version in pubspec.yaml. | ||
| const String pigeonVersion = '27.1.0'; | ||
| const String pigeonVersion = '27.2.0'; | ||
|
|
||
| /// Default plugin package name. | ||
| const String defaultPluginPackageName = 'dev.flutter.pigeon'; | ||
|
|
@@ -871,3 +871,22 @@ bool isCollectionType(TypeDeclaration type) { | |
| !type.isProxyApi && | ||
| (type.baseName.contains('List') || type.baseName == 'Map'); | ||
| } | ||
|
|
||
| /// Escapes special characters in a string for use in double-quoted string literals. | ||
| String escapeStringDoubleQuotes(String value) { | ||
| return value | ||
| .replaceAll('\\', r'\\') // ignore: use_raw_strings | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and below, why not just use raw strings? |
||
| .replaceAll('"', r'\"') | ||
| .replaceAll('\n', r'\n') | ||
| .replaceAll('\r', r'\r'); | ||
| } | ||
|
tarrinneal marked this conversation as resolved.
|
||
|
|
||
| /// Escapes special characters in a string for use in single-quoted string literals. | ||
| String escapeStringSingleQuotes(String value) { | ||
| return value | ||
| .replaceAll('\\', r'\\') // ignore: use_raw_strings | ||
| .replaceAll("'", r"\'") | ||
| .replaceAll('\n', r'\n') | ||
| .replaceAll('\r', r'\r') | ||
| .replaceAll(r'$', r'\$'); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the prefixing, the variable name needs to be converted to UpperCamelCase. E.g.,
PGNAStringConstant.