Fix multi-line comments for cpp enums (#5345) (#5346)

- fix CSharp comments generation
- fix Python comments generation
- fix Lua comments generation
- fix PHP comments generation
- fix Dart comments generation
- add brief description of Color enum
- add multi-line comments to the Monster:Color
This commit is contained in:
Vladimir Glavnyy
2019-06-03 02:36:49 +07:00
committed by Wouter van Oortmerssen
parent bc7ede8fb3
commit 95004218f7
28 changed files with 151 additions and 62 deletions

View File

@@ -948,7 +948,6 @@ class CppGenerator : public BaseGenerator {
void GenEnum(const EnumDef &enum_def) {
code_.SetValue("ENUM_NAME", Name(enum_def));
code_.SetValue("BASE_TYPE", GenTypeBasic(enum_def.underlying_type, false));
code_.SetValue("SEP", "");
GenComment(enum_def.doc_comment);
code_ += GenEnumDecl(enum_def) + "\\";
@@ -961,19 +960,18 @@ class CppGenerator : public BaseGenerator {
if (add_type) code_ += " : {{BASE_TYPE}}\\";
code_ += " {";
code_.SetValue("SEP", ",");
auto add_sep = false;
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
const auto &ev = **it;
if (!ev.doc_comment.empty()) {
auto prefix = code_.GetValue("SEP") + " ";
GenComment(ev.doc_comment, prefix.c_str());
code_.SetValue("SEP", "");
}
if (add_sep) code_ += "{{SEP}}";
GenComment(ev.doc_comment, " ");
code_.SetValue("KEY", GenEnumValDecl(enum_def, Name(ev)));
code_.SetValue("VALUE",
NumToStringCpp(enum_def.ToString(ev),
enum_def.underlying_type.base_type));
code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
code_.SetValue("SEP", ",\n");
code_ += " {{KEY}} = {{VALUE}}\\";
add_sep = true;
}
const EnumVal *minv = enum_def.MinValue();
const EnumVal *maxv = enum_def.MaxValue();

View File

@@ -181,7 +181,6 @@ class DartGenerator : public BaseGenerator {
}
auto &code = *code_ptr;
if (indent) code += indent;
for (auto it = dc.begin(); it != dc.end(); ++it) {
if (indent) code += indent;
@@ -495,7 +494,7 @@ class DartGenerator : public BaseGenerator {
std::string type_name = GenDartTypeName(
field.value.type, struct_def.defined_namespace, field, false);
GenDocComment(field.doc_comment, &code, "");
GenDocComment(field.doc_comment, &code, "", " ");
code += " " + type_name + " get " + field_name;
if (field.value.type.base_type == BASE_TYPE_UNION) {

View File

@@ -510,17 +510,17 @@ class GeneralGenerator : public BaseGenerator {
std::string &code = *code_ptr;
if (enum_def.generated) return;
// In C# this indicates enumeration values can be treated as bit flags.
if (lang_.language == IDLOptions::kCSharp && enum_def.attributes.Lookup("bit_flags")) {
code += "[System.FlagsAttribute]\n";
}
// Generate enum definitions of the form:
// public static (final) int name = value;
// In Java, we use ints rather than the Enum feature, because we want them
// to map directly to how they're used in C/C++ and file formats.
// That, and Java Enums are expensive, and not universally liked.
GenComment(enum_def.doc_comment, code_ptr, &lang_.comment_config);
// In C# this indicates enumeration values can be treated as bit flags.
if (lang_.language == IDLOptions::kCSharp && enum_def.attributes.Lookup("bit_flags")) {
code += "[System.FlagsAttribute]\n";
}
if (enum_def.attributes.Lookup("private")) {
// For Java, we leave the enum unmarked to indicate package-private
// For C# we mark the enum as internal
@@ -547,7 +547,8 @@ class GeneralGenerator : public BaseGenerator {
code += lang_.const_decl;
code += GenTypeBasic(enum_def.underlying_type, false);
}
code += " " + ev.name + " = ";
code += (lang_.language == IDLOptions::kJava) ? " " : " ";
code += ev.name + " = ";
code += enum_def.ToString(ev);
code += lang_.enum_separator;
}

View File

@@ -29,6 +29,7 @@ namespace flatbuffers {
namespace lua {
// Hardcode spaces per indentation.
const CommentConfig def_comment = { nullptr, "--", nullptr };
const char * Indent = " ";
const char * Comment = "-- ";
const char * End = "end\n";
@@ -472,7 +473,7 @@ namespace lua {
// Generate a struct field, conditioned on its child type(s).
void GenStructAccessor(const StructDef &struct_def,
const FieldDef &field, std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr, Comment);
GenComment(field.doc_comment, code_ptr, &def_comment);
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
GetScalarFieldOfStruct(struct_def, field, code_ptr);
@@ -535,7 +536,7 @@ namespace lua {
void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
if (struct_def.generated) return;
GenComment(struct_def.doc_comment, code_ptr, nullptr, Comment);
GenComment(struct_def.doc_comment, code_ptr, &def_comment);
BeginClass(struct_def, code_ptr);
GenerateNewObjectPrototype(struct_def, code_ptr);
@@ -571,12 +572,12 @@ namespace lua {
void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
if (enum_def.generated) return;
GenComment(enum_def.doc_comment, code_ptr, nullptr, Comment);
GenComment(enum_def.doc_comment, code_ptr, &def_comment);
BeginEnum(NormalizedName(enum_def), code_ptr);
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end();
++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, nullptr, Comment);
GenComment(ev.doc_comment, code_ptr, &def_comment, Indent);
EnumMember(enum_def, ev, code_ptr);
}
EndEnum(code_ptr);

View File

@@ -673,7 +673,7 @@ class PhpGenerator : public BaseGenerator {
// Generate a struct field, conditioned on its child type(s).
void GenStructAccessor(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr);
GenComment(field.doc_comment, code_ptr, nullptr, Indent.c_str());
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
@@ -818,7 +818,7 @@ class PhpGenerator : public BaseGenerator {
BeginEnum(enum_def.name, code_ptr);
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, nullptr);
GenComment(ev.doc_comment, code_ptr, nullptr, Indent.c_str());
EnumMember(enum_def, ev, code_ptr);
}

View File

@@ -29,6 +29,7 @@ namespace flatbuffers {
namespace python {
// Hardcode spaces per indentation.
const CommentConfig def_comment = { nullptr, "#", nullptr };
const std::string Indent = " ";
class PythonGenerator : public BaseGenerator {
@@ -497,7 +498,7 @@ class PythonGenerator : public BaseGenerator {
// Generate a struct field, conditioned on its child type(s).
void GenStructAccessor(const StructDef &struct_def,
const FieldDef &field, std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr, "# ");
GenComment(field.doc_comment, code_ptr, &def_comment, Indent.c_str());
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
GetScalarFieldOfStruct(struct_def, field, code_ptr);
@@ -557,7 +558,7 @@ class PythonGenerator : public BaseGenerator {
void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
if (struct_def.generated) return;
GenComment(struct_def.doc_comment, code_ptr, nullptr, "# ");
GenComment(struct_def.doc_comment, code_ptr, &def_comment);
BeginClass(struct_def, code_ptr);
if (!struct_def.fixed) {
// Generate a special accessor for the table that has been declared as
@@ -588,11 +589,11 @@ class PythonGenerator : public BaseGenerator {
void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
if (enum_def.generated) return;
GenComment(enum_def.doc_comment, code_ptr, nullptr, "# ");
GenComment(enum_def.doc_comment, code_ptr, &def_comment);
BeginEnum(NormalizedName(enum_def), code_ptr);
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, nullptr, "# ");
GenComment(ev.doc_comment, code_ptr, &def_comment, Indent.c_str());
EnumMember(enum_def, ev, code_ptr);
}
EndEnum(code_ptr);