File indexing completed on 2024-05-12 04:37:48

0001 /*
0002     SPDX-FileCopyrightText: 2012 Miha Čančula <miha@noughmad.eu>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_TEMPLATECLASSGENERATOR_H
0008 #define KDEVPLATFORM_TEMPLATECLASSGENERATOR_H
0009 
0010 #include <QHash>
0011 #include <QVariantHash>
0012 
0013 #include <language/languageexport.h>
0014 #include <language/duchain/duchainpointer.h>
0015 
0016 class QUrl;
0017 
0018 namespace KTextEditor {
0019 class Cursor;
0020 }
0021 
0022 namespace KDevelop {
0023 struct ClassDescription;
0024 class TemplateRenderer;
0025 class SourceFileTemplate;
0026 class DocumentChangeSet;
0027 class TemplateClassGeneratorPrivate;
0028 
0029 /**
0030  * Generates new classes from templates
0031  *
0032  *
0033  * @section Variables Variables Passed to Templates
0034  *
0035  * TemplateClassGenerator makes use of the ClassDescription returned by ClassGenerator::description().
0036  * From this description, it constructs the following variables:
0037  * @li @c description (ClassDescription) - the class description
0038  * @li @c name (QString) - the class name, same as @c description.name
0039  * @li @c namespaces (QStringList) - the list of nested namespaces in which the class will be declared
0040  * @li @c identifier (QString) - the full class identifier, composed of namespaces and name
0041  * @li @c members (VariableDescriptionList) - data members, same as @c description.members
0042  * @li @c functions (FunctionDescriptionList) - function members, same as @c description.methods
0043  * @li @c base_classes (InheritanceDescriptionList) - directly inherited classes, same as @c description.baseClasses
0044  * @li @c license (QString) - the license for this class, including author copyright, without comment characters or indentation. It is recommended to use the "lines_prepend" filters from library "kdev_filters" to format it.
0045  *
0046  * For each output file, TemplateRenderer add two variables named @c output_file_x
0047  * and @c output_file_x_absolute, where @c x is replaced
0048  * with the file name specified in the template description file.
0049  * See TemplateRenderer::renderFileTemplate() for details.
0050  *
0051  * If the templates uses custom options, these options are added to the template variables. Their names match the
0052  * names specified in the options file, and their values to the values entered by the user.
0053  *
0054  * Subclasses can override templateVariables() and insert additional variables.
0055  *
0056  **/
0057 class KDEVPLATFORMLANGUAGE_EXPORT TemplateClassGenerator
0058 {
0059 public:
0060     using UrlHash = QHash<QString, QUrl>;
0061 
0062     /**
0063      * Creates a new generator.
0064      *
0065      * You should call setTemplateDescription() before any other template-related functions.
0066      *
0067      * @param baseUrl the folder where new files will be created
0068      **/
0069     explicit TemplateClassGenerator(const QUrl& baseUrl);
0070     virtual ~TemplateClassGenerator();
0071 
0072     /**
0073      * @brief Selects the template to be used
0074      *
0075      * This function must be called before using any other functions.
0076      *
0077      * The passed @p templateDescription should be an absolute path to a template description (.desktop) file.
0078      * TemplateClassGenerator will attempt to find a template archive with a matching name.
0079      *
0080      * @param templateDescription the template description file
0081      **/
0082     void setTemplateDescription(const SourceFileTemplate& templateDescription);
0083 
0084     /**
0085      * Set the name (without namespace) for this class
0086      */
0087     void setName(const QString&);
0088 
0089     /**
0090      * \return The name of the class to generate (excluding namespaces)
0091      */
0092     QString name() const;
0093 
0094     /**
0095      * \param identifier The Qualified identifier that the class will have
0096      */
0097     virtual void setIdentifier(const QString& identifier);
0098 
0099     /**
0100      * \return The Identifier of the class to generate (including all used namespaces)
0101      */
0102     virtual QString identifier() const;
0103 
0104     /**
0105      * \param namespaces The list of nested namespaces in which this class is to be declared
0106      */
0107     virtual void setNamespaces(const QStringList& namespaces);
0108 
0109     /**
0110      * \return The list of nested namespace in which this class will be declared
0111      */
0112     virtual QStringList namespaces() const;
0113 
0114     void addBaseClass(const QString& base);
0115     void setBaseClasses(const QList<QString>& bases);
0116     QList<DeclarationPointer> directBaseClasses() const;
0117     QList<DeclarationPointer> allBaseClasses() const;
0118 
0119     void setLicense(const QString& license);
0120     QString license() const;
0121 
0122     void setDescription(const ClassDescription& description);
0123     ClassDescription description() const;
0124 
0125     virtual DocumentChangeSet generate();
0126 
0127     QHash<QString, QString> fileLabels() const;
0128 
0129     QUrl baseUrl() const;
0130     UrlHash fileUrls() const;
0131 
0132     void setFileUrl(const QString& outputFile, const QUrl& url);
0133     QUrl fileUrl(const QString& outputFile) const;
0134 
0135     void setFilePosition(const QString& outputFile, const KTextEditor::Cursor& position);
0136     KTextEditor::Cursor filePosition(const QString& outputFile) const;
0137 
0138     SourceFileTemplate sourceFileTemplate() const;
0139 
0140     /**
0141      * Adds variables @p variables to the context passed to all template files.
0142      *
0143      * The variable values must be of a type registered with Grantlee::registerMetaType()
0144      *
0145      * @param variables additional variables to be passed to all templates
0146      **/
0147     void addVariables(const QVariantHash& variables);
0148 
0149     /**
0150      * Convenience function to render a string @p text as a Grantlee template
0151      **/
0152     QString renderString(const QString& text) const;
0153 
0154     /**
0155      * The template renderer used to render all the templates for this class.
0156      *
0157      * This function is useful if you want a rendeder with all current template variables.
0158      */
0159     TemplateRenderer* renderer() const;
0160 
0161 private:
0162     const QScopedPointer<class TemplateClassGeneratorPrivate> d_ptr;
0163     Q_DECLARE_PRIVATE(TemplateClassGenerator)
0164 };
0165 }
0166 
0167 #endif // KDEVPLATFORM_TEMPLATECLASSGENERATOR_H