Skip to content

Commit

Permalink
Generate doc comments for enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Dec 29, 2023
1 parent 1784bf8 commit 4550cbd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions protoc_plugin/lib/protoc.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';

import 'package:protobuf/protobuf.dart';

Expand Down
22 changes: 13 additions & 9 deletions protoc_plugin/lib/src/enum_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ class EnumGenerator extends ProtobufContainer {
List<int>? _fieldPath;
final List<int> _fieldPathSegment;

/// See [[ProtobufContainer]
@override
List<int>? get fieldPath =>
List<int> get fieldPath =>
_fieldPath ??= List.from(parent!.fieldPath!)..addAll(_fieldPathSegment);

EnumGenerator._(EnumDescriptorProto descriptor, this.parent,
Expand Down Expand Up @@ -103,17 +102,15 @@ 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);
}
out.addAnnotatedBlock(
'class $classname extends $protobufImportPrefix.ProtobufEnum {',
'}\n', [
NamedLocation(
name: classname!,
fieldPathSegment: fieldPath!,
start: 'class '.length)
name: classname!, fieldPathSegment: fieldPath, start: 'class '.length)
], () {
// -----------------------------------------------------------------
// Define enum types.
Expand All @@ -124,14 +121,21 @@ class EnumGenerator extends ProtobufContainer {
out.addSuffix(
omitEnumNames.constFieldName, omitEnumNames.constDefinition);
final conditionalValName = omitEnumNames.createTernary(val.name);
final fieldPathSegment = List<int>.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)
]);
}
Expand All @@ -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)
]);
Expand Down
1 change: 0 additions & 1 deletion protoc_plugin/lib/src/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class MessageGenerator extends ProtobufContainer {

final List<int> _fieldPathSegment;

/// See [[ProtobufContainer]
@override
late final List<int> fieldPath = List.from(parent!.fieldPath!)
..addAll(_fieldPathSegment);
Expand Down

0 comments on commit 4550cbd

Please sign in to comment.