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_SOURCEFILETEMPLATE_H
0008 #define KDEVPLATFORM_SOURCEFILETEMPLATE_H
0009 
0010 #include <QString>
0011 #include <QList>
0012 #include <QVector>
0013 #include <QVariant>
0014 
0015 #include <language/languageexport.h>
0016 
0017 class QStringList;
0018 class KArchiveDirectory;
0019 
0020 namespace KDevelop {
0021 class TemplateRenderer;
0022 class SourceFileTemplatePrivate;
0023 
0024 /**
0025  * Represents a source file template archive
0026  *
0027  * @section TemplateStructure Template Archive Structure
0028  *
0029  * Source file templates in KDevPlatform are archive files.
0030  * The archive must contain at least one .desktop file with the template's description.
0031  * If multiple such files are present, the one with the same base name as the archive itself will be used.
0032  *
0033  * The description file must contain a @c [General] section with the following keys:
0034  * @li @c Name - The user-visible name of this template
0035  * @li @c Comment - A short user-visible comment
0036  * @li @c Category - The category of this template. It can be nested, in which case levels are separated
0037  * with forward slashes. The top-level category is usually the language followed by the framework.
0038  * @li @c Type An optional type, which then adds some more default wizard pages and configuration.
0039  *             Currently supported are @c Class and @c Test.
0040  * @li @c Language The @c X-KDevelop-Languages compatible id of a language plugin which is then asked
0041  *                 for a custom @ref ICreateClassHelper object.
0042  * @li @c Files - List of files generated by this template. These are not actual file names, but names
0043  * of config groups describing those files.
0044  * @li @c OptionsFile (optional) - If the template uses custom configuration options, specify a path to
0045  * the options file here. @ref CustomOptions
0046  *
0047  * For each file name in the @c Files array, TemplateClassGenerator expects a section with the same name.
0048  * this section should contain three keys:
0049  * @li @c Name - User-visible name for this file. This will be show the user in the dialog and can be translated.
0050  * @li @c File - The input file name in the template archive. The template for this file will be read from here.
0051  * @li @c OutputFile - The suggested output file name. This will be renderer as a template, so it can contain variables.
0052  *
0053  * An example template description is below. It shows all features described above.
0054  *
0055  * @code
0056  * [General]
0057  * Name=Example
0058  * Comment=Example description for a C++ Class
0059  * Category=C++/Basic
0060  * Type=Class
0061  * Language=C++
0062  * Options=options.kcfg
0063  * Files=Header,Implementation
0064  *
0065  * [Header]
0066  * Name=Header
0067  * File=class.h
0068  * OutputFile={{ name }}.h
0069  *
0070  * [Implementation]
0071  * Name=Implementation
0072  * File=class.cpp
0073  * OutputFile={{ name }}.cpp
0074  * @endcode
0075  *
0076  * @section CustomOptions
0077  *
0078  * Templates can expose additional configurations options.
0079  * This is done through a file with the same syntax and .kcfg files used by KConfig XT.
0080  * The name of this file is specified with the @c OptionsFile key in the [General] section of the description file.
0081  *
0082  * @note
0083  * The options are not parsed by TemplateClassGenerator.
0084  * Instead, hasCustomOptions() returns true if the template specifies a custom options file,
0085  * and customOptions() returns the full text of that file.
0086  * The parsing is done by TemplateOptionsPage.
0087  *
0088  * The file can (and should) provide a type, name, label and default value for each configuration option.
0089  * So far, only variables with types String, Int and Bool are recognized.
0090  * Label is the user-visible string that will be shown next to the input field.
0091  * The default value will be rendered as a template, so it can contain variables.
0092  *
0093  * After the user configures the options, they will be available to the template as context variables.
0094  * The variable name will match the option name, and its value will be the one set by the user.
0095  *
0096  * An example options.kcfg file for a class template is included below.
0097  *
0098  * @code
0099  * <?xml version="1.0" encoding="UTF-8"?>
0100  * <kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
0101  *      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
0102  *      xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
0103  *      http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
0104  *  <kcfgfile arg="true"/>
0105  *  <group name="Private Class">
0106  *    <entry name="private_class_name" type="String">
0107  *      <label>Private class name</label>
0108  *      <default>{{ name }}Private</default>
0109  *    </entry>
0110  *    <entry name="private_member_name" type="String">
0111  *      <label>Private member name</label>
0112  *      <default>d</default>
0113  *    </entry>
0114  *  </group>
0115  * </kcfg>
0116  * @endcode
0117  *
0118  * In this example, if the class name is Example, the default value for the private class name will be ExamplePrivate.
0119  * After the user accepts the option values, the template will access them through the @c private_class_name
0120  * and @c private_member_name variables.
0121  *
0122  * For more information regarding the XML file format, refer to the KConfig XT documentation.
0123  */
0124 class KDEVPLATFORMLANGUAGE_EXPORT SourceFileTemplate
0125 {
0126 public:
0127     /**
0128      * Describes a single output file in this template
0129      */
0130     struct OutputFile
0131     {
0132         /**
0133          * A unique identifier, equal to the list element in the @c OutputFiles entry
0134          */
0135         QString identifier;
0136         /**
0137          * The name of the input file within the archive, equal to the @c File entry
0138          */
0139         QString fileName;
0140         /**
0141          * User-visible label, equal to the @c Name entry in this file's group in the template description file
0142          */
0143         QString label;
0144         /**
0145          * The default output file name, equal to the @c OutputFile entry
0146          *
0147          * @note The output name can contain template variables, so make sure to pass it through
0148          * TemplateRenderer::render before using it or showing it to the user.
0149          */
0150         QString outputName;
0151     };
0152 
0153     /**
0154      * Describes one configuration option
0155      */
0156     struct ConfigOption
0157     {
0158         /**
0159          * The type of this option.
0160          *
0161          * Currently supported are Int, String, Enum and Bool
0162          */
0163         QString type;
0164         /**
0165          * A unique identifier for this option
0166          */
0167         QString name;
0168         /**
0169          * User-visible label
0170          */
0171         QString label;
0172         /**
0173          * A context description for the option, shown to the user as a tooltip
0174          */
0175         QString context;
0176 
0177         /**
0178          * The default value of this option
0179          */
0180         QVariant value;
0181 
0182         /**
0183          * The maximum value of this entry, as a string
0184          *
0185          * This is applicable only to integers
0186          */
0187         QString maxValue;
0188         /**
0189          * The minimum value of this entry, as a string
0190          *
0191          * This is applicable only to integers
0192          */
0193         QString minValue;
0194         /**
0195          * The possible values of this entry, as a list of strings
0196          *
0197          * This is applicable only to enums
0198          */
0199         QStringList values;
0200     };
0201 
0202     /**
0203      * Describes a group of configuration options
0204      */
0205     struct ConfigOptionGroup
0206     {
0207         /**
0208          * A unique identifier for this option group
0209          */
0210         QString name;
0211 
0212         /**
0213          * The list of options in this group
0214          */
0215         QVector<ConfigOption> options;
0216     };
0217 
0218     /**
0219      * Creates a SourceFileTemplate representing the template archive with
0220      * description file @p templateDescription.
0221      *
0222      * @param templateDescription template description file, used to find the
0223      *        archive and read information
0224      */
0225     explicit SourceFileTemplate(const QString& templateDescription);
0226 
0227     /**
0228      * Copy constructor
0229      *
0230      * Creates a SourceFileTemplate representing the same template archive as @p other.
0231      * This new objects shares no data with the @p other, so they can be read and deleted independently.
0232      *
0233      * @param other the template to copy
0234      */
0235     SourceFileTemplate(const SourceFileTemplate& other);
0236 
0237     /**
0238      * Creates an invalid SourceFileTemplate
0239      */
0240     SourceFileTemplate();
0241 
0242     /**
0243      * Destroys this SourceFileTemplate
0244      */
0245     ~SourceFileTemplate();
0246 
0247     SourceFileTemplate& operator=(const SourceFileTemplate& other);
0248 
0249     void setTemplateDescription(const QString& templateDescription);
0250 
0251     /**
0252      * Returns true if this SourceFileTemplate represents an actual template archive, and false otherwise
0253      */
0254     bool isValid() const;
0255 
0256     /**
0257      * The name of this template, corresponds to the @c Name entry in the description file
0258      */
0259     QString name() const;
0260 
0261     /**
0262      * The top-level directory in the template archive
0263      *
0264      * @sa KArchive::directory()
0265      */
0266     const KArchiveDirectory* directory() const;
0267 
0268     /**
0269      * The list of all output files in this template
0270      */
0271     QVector<OutputFile> outputFiles() const;
0272 
0273     /**
0274      * @return true if the template uses any custom options, false otherwise
0275      **/
0276     bool hasCustomOptions() const;
0277 
0278     /**
0279      * @return the custom options this template exposes, in the order as defined in the config file
0280      **/
0281     QVector<ConfigOptionGroup> customOptions(TemplateRenderer* renderer) const;
0282 
0283     /**
0284      * @return The type of this template.
0285      *
0286      * This can be any string, but TemplateClassAssistant only supports @c Class and @c Test so far.
0287      */
0288     QString type() const;
0289 
0290     /**
0291      * An optional @c X-KDevelop-Languages compatible id by which a language plugin for this template can be found.
0292      */
0293     QString languageName() const;
0294 
0295     /**
0296      * The category of this template.
0297      */
0298     QStringList category() const;
0299 
0300     /**
0301      * @return the list of base classes specified by this template.
0302      * If this is not a class template, or if it specifies no default base classes, an empty list is returned.
0303      *
0304      * Each element is the full inheritance description, such as "public QObject".
0305      */
0306     QStringList defaultBaseClasses() const;
0307 
0308     /**
0309      * Add an additional search location where the code will look for archives
0310      *
0311      * @param location Absolute path to a directory with archives. Has to end with a '/'
0312      */
0313     void addAdditionalSearchLocation(const QString& location);
0314 
0315 private:
0316     const QScopedPointer<class SourceFileTemplatePrivate> d_ptr;
0317     Q_DECLARE_PRIVATE(SourceFileTemplate)
0318 };
0319 }
0320 
0321 Q_DECLARE_TYPEINFO(KDevelop::SourceFileTemplate::OutputFile, Q_MOVABLE_TYPE);
0322 Q_DECLARE_TYPEINFO(KDevelop::SourceFileTemplate::ConfigOption, Q_MOVABLE_TYPE);
0323 Q_DECLARE_TYPEINFO(KDevelop::SourceFileTemplate::ConfigOptionGroup, Q_MOVABLE_TYPE);
0324 
0325 #endif // KDEVPLATFORM_SOURCEFILETEMPLATE_H