Skip to content

Commit 898ec56

Browse files
committed
nits and nats
1 parent d765ce7 commit 898ec56

21 files changed

Lines changed: 462 additions & 335 deletions

packages/pigeon/example/app/lib/src/event_channel_messages.g.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import 'dart:async';
1010
import 'dart:typed_data' show Float64List, Int32List, Int64List;
11+
1112
import 'package:flutter/services.dart';
1213
import 'package:meta/meta.dart' show immutable, protected, visibleForTesting;
1314

packages/pigeon/example/app/lib/src/messages.g.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import 'dart:async';
1010
import 'dart:typed_data' show Float64List, Int32List, Int64List;
11+
1112
import 'package:flutter/services.dart';
1213
import 'package:meta/meta.dart' show immutable, protected, visibleForTesting;
1314

packages/pigeon/lib/src/ast.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,20 +528,23 @@ class TypeDeclaration {
528528

529529
/// Returns the full annotated name of the type.
530530
String getFullName({bool withNullable = true}) {
531-
if (baseName == 'List' || baseName == 'Map') {
532-
return '$baseName<$typeArgumentsString>${isNullable && withNullable ? '?' : ''}';
533-
}
534-
return '$baseName${isNullable && withNullable ? '?' : ''}';
531+
return '$baseName$typeArgumentsString${isNullable && withNullable ? '?' : ''}';
535532
}
536533

537534
/// Returns the Type Arguments in annotation form.
538535
String get typeArgumentsString {
536+
var typeArgumentString = '<';
539537
if (baseName == 'List') {
540-
return typeArguments.firstOrNull?.getFullName() ?? 'Object?';
538+
typeArgumentString +=
539+
typeArguments.firstOrNull?.getFullName() ?? 'Object?';
541540
} else if (baseName == 'Map') {
542-
return '${typeArguments.firstOrNull?.getFullName() ?? 'Object?'}, ${typeArguments.lastOrNull?.getFullName() ?? 'Object?'}';
541+
typeArgumentString +=
542+
'${typeArguments.firstOrNull?.getFullName() ?? 'Object?'}, ${typeArguments.lastOrNull?.getFullName() ?? 'Object?'}';
543+
} else {
544+
return '';
543545
}
544-
return '';
546+
typeArgumentString += '>';
547+
return typeArgumentString;
545548
}
546549

547550
@override

packages/pigeon/lib/src/dart/dart_generator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
768768
indent.writeln(
769769
"import 'dart:typed_data' show ${typedDataClasses.join(', ')};",
770770
);
771+
indent.newln();
771772
if (generatorOptions.useFfi) {
772773
indent.writeln("import 'package:ffi/ffi.dart';");
773774
}

packages/pigeon/lib/src/generator_tools.dart

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -913,16 +913,40 @@ String wrapConditionally(String toWrap, String start, String end, bool wrap) {
913913
return wrap ? '$start$toWrap$end' : toWrap;
914914
}
915915

916-
/// Sorts collections by how generic they are.
917-
int sortByObjectCount(TypeDeclaration a, TypeDeclaration b) {
918-
var aTotal = 0;
919-
var bTotal = 0;
916+
/// Compares [TypeDeclaration]s by how generic they are.
917+
///
918+
/// Generic-ness is approximated by counting the number of "Objects" and "?" in the
919+
/// type name, with "Object" being strongly weighted and "?" less so.
920+
int compareTypeDeclarationGenericness(TypeDeclaration a, TypeDeclaration b) {
921+
return _calculateGenericScore(a, 0).compareTo(_calculateGenericScore(b, 0));
922+
}
923+
924+
int _calculateGenericScore(TypeDeclaration type, int depth) {
925+
var score = 0;
920926

921-
aTotal += a.getFullName(withNullable: false).split('?').length;
922-
bTotal += b.getFullName(withNullable: false).split('?').length;
927+
if (type.baseName == 'Object') {
928+
score += 10000 >> depth;
929+
}
930+
if (type.isNullable) {
931+
score += 1000 >> depth;
932+
}
923933

924-
aTotal += a.getFullName(withNullable: false).split('Object').length * 100;
925-
bTotal += b.getFullName(withNullable: false).split('Object').length * 100;
934+
// Handle untyped collections by scoring their implicit 'Object?' arguments
935+
if (type.typeArguments.isEmpty) {
936+
if (type.baseName == 'List') {
937+
score += 11000 >> (depth + 1);
938+
}
939+
if (type.baseName == 'Map') {
940+
score += 22000 >> (depth + 1);
941+
}
942+
}
926943

927-
return aTotal < bTotal ? -1 : 1;
944+
for (final TypeDeclaration arg in type.typeArguments) {
945+
score += _calculateGenericScore(arg, depth + 1);
946+
}
947+
return score;
928948
}
949+
950+
/// Alias for [compareTypeDeclarationGenericness] to maintain compatibility.
951+
const int Function(TypeDeclaration, TypeDeclaration) sortByObjectCount =
952+
compareTypeDeclarationGenericness;

0 commit comments

Comments
 (0)