Forráskód Böngészése

Migrate to Indent/Outdent

- Change all indents to integrated Indent functions
- Move few templates to template header
- Fix few issues in generation
Alexey Edelev 6 éve
szülő
commit
703b1a9592

+ 29 - 7
src/generator/classgeneratorbase.cpp

@@ -123,16 +123,22 @@ void ClassGeneratorBase::printClass()
 void ClassGeneratorBase::printProperties(const Descriptor *message)
 {
     //private section
+    Indent();
     for (int i = 0; i < message->field_count(); i++) {
         printField(message->field(i), PropertyTemplate);
     }
     for (int i = 0; i < message->field_count(); i++) {
         printField(message->field(i), MemberTemplate);
     }
+    Outdent();
+
     printQEnums(message);
 
     //public section
     printPublic();
+
+    //Body
+    Indent();
     printConstructor();
     printCopyFunctionality(message);
     printEqualOperator(message);
@@ -148,10 +154,15 @@ void ClassGeneratorBase::printProperties(const Descriptor *message)
             printField(field, SetterTemplateSimpleType);
         }
     }
+    Outdent();
+
     mPrinter.Print(SignalsBlockTemplate);
+
+    Indent();
     for (int i = 0; i < message->field_count(); i++) {
         printField(message->field(i), SignalTemplate);
     }
+    Outdent();
 }
 
 void ClassGeneratorBase::printField(const FieldDescriptor *field, const char *fieldTemplate)
@@ -205,20 +216,22 @@ void ClassGeneratorBase::printCopyFunctionality(const ::google::protobuf::Descri
     mPrinter.Print({{"classname", mClassName}},
                    CopyConstructorTemplate);
 
+    Indent();
     for (int i = 0; i < message->field_count(); i++) {
         printField(message->field(i), CopyFieldTemplate);
     }
+    Outdent();
 
-    mPrinter.Print("    ");
     mPrinter.Print(SimpleBlockEnclosureTemplate);
     mPrinter.Print({{"classname", mClassName}},
                    AssignmentOperatorTemplate);
 
+    Indent();
     for (int i = 0; i < message->field_count(); i++) {
         printField(message->field(i), CopyFieldTemplate);
     }
+    Outdent();
 
-    mPrinter.Print("    ");
     mPrinter.Print(SimpleBlockEnclosureTemplate);
 
 }
@@ -269,7 +282,7 @@ void ClassGeneratorBase::printConstructor()
     //            }
     //        }
     //    }
-    mPrinter.Print("    {}\n\n");
+    mPrinter.Print(EmptyBlockTemplate);
 }
 
 void ClassGeneratorBase::printPublic()
@@ -278,7 +291,7 @@ void ClassGeneratorBase::printPublic()
 }
 
 void ClassGeneratorBase::printEqualOperator(const Descriptor *message)
-{
+{    
     bool isFirst = true;
     PropertyMap properties;
     mPrinter.Print({{"type", mClassName}}, EqualOperatorTemplate);
@@ -286,13 +299,22 @@ void ClassGeneratorBase::printEqualOperator(const Descriptor *message)
         const FieldDescriptor* field = message->field(i);
         if (producePropertyMap(field, properties)) {
             if (!isFirst) {
-                mPrinter.Print("\n            && ");
+                mPrinter.Print("\n&& ");
+            } else {
+                Indent();
+                Indent();
+                isFirst = false;
             }
-            isFirst = false;
             mPrinter.Print(properties, EqualOperatorPropertyTemplate);
         }
     }
+
+    //Only if at least one field "copied"
+    if (!isFirst) {
+        Outdent();
+        Outdent();
+    }
+
     mPrinter.Print(";\n");
-    mPrinter.Print("    ");
     mPrinter.Print(SimpleBlockEnclosureTemplate);
 }

+ 24 - 4
src/generator/classgeneratorbase.h

@@ -24,8 +24,11 @@
  */
 
 #pragma once
+
 #include <google/protobuf/io/printer.h>
 
+#include "templates.h"
+
 namespace google { namespace protobuf {
 class FieldDescriptor;
 class Descriptor;
@@ -63,23 +66,31 @@ protected:
             return;
         }
 
-        mPrinter.Print("    Q_ENUMS(");
+        Indent();
+        mPrinter.Print("Q_ENUMS(");
         for (int i = 0; i < message->enum_type_count(); i++) {
             const auto enumDescr = message->enum_type(i);
             mPrinter.Print(enumDescr->name().c_str());
         }
         mPrinter.Print(")\n");
+        Outdent();
+
         printPublic();
+
+        Indent();
         for (int i = 0; i < message->enum_type_count(); i++) {
             const auto enumDescr = message->enum_type(i);
-            mPrinter.Print({{"enum", enumDescr->name()}}, "    enum $enum$ {\n");
+            mPrinter.Print({{"enum", enumDescr->name()}}, EnumDefinitionTemplate);
+            Indent();
             for (int j = 0; j < enumDescr->value_count(); j++) {
                 const auto valueDescr = enumDescr->value(j);
                 mPrinter.Print({{"enumvalue", valueDescr->name()},
-                                {"value", std::to_string(valueDescr->number())}}, "        $enumvalue$ = $value$,\n");
+                                {"value", std::to_string(valueDescr->number())}}, EnumFieldTemplate);
             }
-            mPrinter.Print("    };\n");
+            Outdent();
+            mPrinter.Print(SemicolonBlockEnclosureTemplate);
         }
+        Outdent();
     }
 
     void printCopyFunctionality(const ::google::protobuf::Descriptor *message);
@@ -87,6 +98,15 @@ protected:
     void printProperties(const ::google::protobuf::Descriptor *message);
     void printConstructor();
     void printPublic();
+    void Indent() {
+        mPrinter.Indent();
+        mPrinter.Indent();
+    }
+
+    void Outdent() {
+        mPrinter.Outdent();
+        mPrinter.Outdent();
+    }
 };
 
 }

+ 2 - 2
src/generator/generator.cpp

@@ -71,7 +71,7 @@ public:
         printIncludes(mMessage);
 
         if (mExtractedModels.size() > 0) {
-            mPrinter.Print("\n#include \"listmodels.h\"\n");
+            mPrinter.Print(ListModelsIncludeTemplate);
         }
 
         printNamespaces(mPackage);
@@ -143,7 +143,7 @@ bool QtGenerator::Generate(const FileDescriptor *file,
         printer.Print({{"type_lower", modelTypeNameLower}}, InternalIncludeTemplate);
     }
 
-    printer.Print("\n#include <universallistmodel.h>\n");
+    printer.Print(UniversalListModelIncludeTemplate);
     for(auto modelTypeName : extractedModels) {
         printer.Print({{"type", modelTypeName}}, ModelClassTemplate);
     }

+ 31 - 26
src/generator/templates.h

@@ -32,56 +32,61 @@
 namespace qtprotobuf {
 
 static const char *PreambleTemplate = "/* This file is autogenerated. DO NOT CHANGE. All changes will be lost */\n\n"
-                                      "#pragma once\n"
+                                      "#pragma once\n\n"
                                      "#include <QObject>\n";
 
 static const char *InternalIncludeTemplate =  "#include \"$type_lower$.h\"\n";
 static const char *ExternalIncludeTemplate = "#include <$type$>\n";
+static const char *ListModelsIncludeTemplate = "\n#include \"listmodels.h\"\n";
+static const char *UniversalListModelIncludeTemplate = "\n#include <universallistmodel.h>\n";
 
 static const char *NamespaceTemplate = "\nnamespace $namespace$\n"
-                                  "{\n\n";
+                                  "{\n";
 
 static const char *ClassDefinitionTemplate = "\nclass $classname$ : public QObject\n"
                       "{\n"
                       "    Q_OBJECT\n";
 
-static const char *PropertyTemplate = "    Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
-static const char *MemberTemplate = "    $type$ m_$property_name$;\n";
+static const char *PropertyTemplate = "Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
+static const char *MemberTemplate = "$type$ m_$property_name$;\n";
 static const char *PublicBlockTemplate = "\npublic:\n";
-static const char *ConstructorTemplate = "    $classname$(QObject *parent = nullptr) : QObject(parent)\n";
-static const char *CopyConstructorTemplate = "    $classname$(const $classname$ &other) {\n";
-static const char *CopyFieldTemplate = "        m_$property_name$ = other.m_$property_name$;\n";
-static const char *AssignmentOperatorTemplate = "    $classname$ &operator =(const $classname$ &other) {\n";
-static const char *EqualOperatorTemplate = "    bool operator ==(const $type$ &other) {\n"
-                                          "        return ";
+static const char *EnumDefinitionTemplate = "enum $enum$ {\n";
+static const char *EnumFieldTemplate = "$enumvalue$ = $value$,\n";
+static const char *ConstructorTemplate = "$classname$(QObject *parent = nullptr) : QObject(parent)\n";
+static const char *CopyConstructorTemplate = "$classname$(const $classname$ &other) {\n";
+static const char *CopyFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n";
+static const char *AssignmentOperatorTemplate = "$classname$ &operator =(const $classname$ &other) {\n";
+static const char *EqualOperatorTemplate = "bool operator ==(const $type$ &other) {\n"
+                                          "    return ";
 static const char *EqualOperatorPropertyTemplate = "m_$property_name$ == other.m_$property_name$";
 
-static const char *GetterTemplate = "    $type$ $property_name$() const {\n"
-                                    "        return m_$property_name$;\n"
-                                    "    }\n";
+static const char *GetterTemplate = "$type$ $property_name$() const {\n"
+                                    "    return m_$property_name$;\n"
+                                    "}\n\n";
 
-static const char *SetterTemplateSimpleType = "    void set$property_name_cap$($type$ $property_name$) {\n"
-                                              "        if (m_$property_name$ != $property_name$) {\n"
-                                              "             m_$property_name$ = $property_name$;\n"
-                                              "             $property_name$Changed();\n"
-                                              "        }\n"
-                                              "    }\n";
+static const char *SetterTemplateSimpleType = "void set$property_name_cap$($type$ $property_name$) {\n"
+                                              "    if (m_$property_name$ != $property_name$) {\n"
+                                              "        m_$property_name$ = $property_name$;\n"
+                                              "        $property_name$Changed();\n"
+                                              "    }\n"
+                                              "}\n\n";
 
-static const char *SetterTemplateComplexType = "    void set$property_name_cap$(const $type$ &$property_name$) {\n"
-                                              "        if (m_$property_name$ != $property_name$) {\n"
-                                              "             m_$property_name$ = $property_name$;\n"
-                                              "             $property_name$Changed();\n"
-                                              "        }\n"
-                                              "    }\n";
+static const char *SetterTemplateComplexType = "void set$property_name_cap$(const $type$ &$property_name$) {\n"
+                                              "    if (m_$property_name$ != $property_name$) {\n"
+                                              "        m_$property_name$ = $property_name$;\n"
+                                              "        $property_name$Changed();\n"
+                                              "    }\n"
+                                              "}\n\n";
 
 static const char *SignalsBlockTemplate = "\nsignals:\n";
-static const char *SignalTemplate = "    void $property_name$Changed();\n";
+static const char *SignalTemplate = "void $property_name$Changed();\n";
 
 static const char *EnumTemplate = "$type$";
 static const char *ModelClassTemplate = "using $type$Model = UniversalListModel<$type$>;\n";
 
 static const char *SimpleBlockEnclosureTemplate = "}\n\n";
 static const char *SemicolonBlockEnclosureTemplate = "};\n";
+static const char *EmptyBlockTemplate = "{}\n\n";
 
 static const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> TypeReflection = {
     {::google::protobuf::FieldDescriptor::TYPE_DOUBLE, "qreal"},