From 4550cbdc272ecbd11144d526040e09c6e993db95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Fri, 29 Dec 2023 11:10:16 +0100 Subject: [PATCH] Generate doc comments for enum values --- protoc_plugin/lib/protoc.dart | 1 + protoc_plugin/lib/src/enum_generator.dart | 22 ++++++++++++-------- protoc_plugin/lib/src/message_generator.dart | 1 - 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/protoc_plugin/lib/protoc.dart b/protoc_plugin/lib/protoc.dart index e6d175bd7..1786c2295 100644 --- a/protoc_plugin/lib/protoc.dart +++ b/protoc_plugin/lib/protoc.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:io'; import 'package:protobuf/protobuf.dart'; diff --git a/protoc_plugin/lib/src/enum_generator.dart b/protoc_plugin/lib/src/enum_generator.dart index 8126b9ec0..a9f7536ba 100644 --- a/protoc_plugin/lib/src/enum_generator.dart +++ b/protoc_plugin/lib/src/enum_generator.dart @@ -32,9 +32,8 @@ class EnumGenerator extends ProtobufContainer { List? _fieldPath; final List _fieldPathSegment; - /// See [[ProtobufContainer] @override - List? get fieldPath => + List get fieldPath => _fieldPath ??= List.from(parent!.fieldPath!)..addAll(_fieldPathSegment); EnumGenerator._(EnumDescriptorProto descriptor, this.parent, @@ -103,7 +102,7 @@ class EnumGenerator extends ProtobufContainer { static const int _enumValueTag = 2; void generate(IndentingWriter out) { - final commentBlock = fileGen?.commentBlock(fieldPath!); + final commentBlock = fileGen?.commentBlock(fieldPath); if (commentBlock != null) { out.println(commentBlock); } @@ -111,9 +110,7 @@ class EnumGenerator extends ProtobufContainer { 'class $classname extends $protobufImportPrefix.ProtobufEnum {', '}\n', [ NamedLocation( - name: classname!, - fieldPathSegment: fieldPath!, - start: 'class '.length) + name: classname!, fieldPathSegment: fieldPath, start: 'class '.length) ], () { // ----------------------------------------------------------------- // Define enum types. @@ -124,14 +121,21 @@ class EnumGenerator extends ProtobufContainer { out.addSuffix( omitEnumNames.constFieldName, omitEnumNames.constDefinition); final conditionalValName = omitEnumNames.createTernary(val.name); + final fieldPathSegment = List.from(fieldPath) + ..addAll([_enumValueTag, _originalCanonicalIndices[i]]); + + final commentBlock = fileGen?.commentBlock(fieldPathSegment); + if (commentBlock != null) { + out.println(commentBlock); + } + out.printlnAnnotated( 'static const $classname $name = ' '$classname._(${val.number}, $conditionalValName);', [ NamedLocation( name: name, - fieldPathSegment: List.from(fieldPath!) - ..addAll([_enumValueTag, _originalCanonicalIndices[i]]), + fieldPathSegment: fieldPathSegment, start: 'static const $classname '.length) ]); } @@ -146,7 +150,7 @@ class EnumGenerator extends ProtobufContainer { [ NamedLocation( name: name, - fieldPathSegment: List.from(fieldPath!) + fieldPathSegment: List.from(fieldPath) ..addAll([_enumValueTag, _originalAliasIndices[i]]), start: 'static const $classname '.length) ]); diff --git a/protoc_plugin/lib/src/message_generator.dart b/protoc_plugin/lib/src/message_generator.dart index b15f49b74..035caf7e3 100644 --- a/protoc_plugin/lib/src/message_generator.dart +++ b/protoc_plugin/lib/src/message_generator.dart @@ -67,7 +67,6 @@ class MessageGenerator extends ProtobufContainer { final List _fieldPathSegment; - /// See [[ProtobufContainer] @override late final List fieldPath = List.from(parent!.fieldPath!) ..addAll(_fieldPathSegment);