@@ -6,6 +6,7 @@ import 'dart:convert';
6
6
import 'dart:io' ;
7
7
8
8
import '../protoc.dart' ;
9
+ import '../src/generated/descriptor.pb.dart' ;
9
10
10
11
const protobufImportPrefix = r'$pb' ;
11
12
const asyncImportPrefix = r'$async' ;
@@ -14,6 +15,20 @@ const grpcImportPrefix = r'$grpc';
14
15
const mixinImportPrefix = r'$mixin' ;
15
16
16
17
extension FileDescriptorProtoExt on FileGenerator {
18
+ bool _listEquals (List <int > a, List <int > b) {
19
+ if (a.length != b.length) {
20
+ return false ;
21
+ }
22
+ // Note: paths are much likely to share common prefixes than to share common
23
+ // suffixes, so it's probably faster to run this loop backwards ;)
24
+ for (var i = a.length - 1 ; i >= 0 ; i-- ) {
25
+ if (a[i] != b[i]) {
26
+ return false ;
27
+ }
28
+ }
29
+ return true ;
30
+ }
31
+
17
32
/// Convert leading comments of a definition at [path] to Dart doc comment
18
33
/// syntax.
19
34
///
@@ -23,18 +38,22 @@ extension FileDescriptorProtoExt on FileGenerator {
23
38
/// The output can contain multiple lines. None of the lines will have
24
39
/// trailing whitespace.
25
40
String ? commentBlock (List <int > path) {
26
- final bits = descriptor.sourceCodeInfo.location
27
- .where ((element) => element.path.toString () == path.toString ())
28
- .toList ();
29
-
30
- if (bits.length == 1 ) {
31
- final match = bits.single;
32
- return toDartComment (match.leadingComments);
41
+ SourceCodeInfo_Location ? singleMatch;
42
+ for (final location in descriptor.sourceCodeInfo.location) {
43
+ if (_listEquals (location.path, path)) {
44
+ if (singleMatch == null ) {
45
+ singleMatch = location;
46
+ } else {
47
+ // TODO: evaluate if we should just concatenate all of the matching
48
+ // entries.
49
+ stderr.writeln ('Too many source code locations. Skipping.' );
50
+ return null ;
51
+ }
52
+ }
33
53
}
34
54
35
- if (bits.length > 1 ) {
36
- // TODO: evaluate if we should just concatenate all of the entries.
37
- stderr.writeln ('Too many source code locations. Skipping.' );
55
+ if (singleMatch != null ) {
56
+ return toDartComment (singleMatch.leadingComments);
38
57
}
39
58
40
59
return null ;
0 commit comments