(→Configuring templates) |
Neverendingo (Talk | contribs) m (Text replace - "<code ini n>" to "<syntaxhighlight lang="ini" line>") |
||
| (8 intermediate revisions by 3 users not shown) | |||
| Line 42: | Line 42: | ||
The Code Generation plugin files are located into the [http://techbase.kde.org/KDE_System_Administration/KDE_Filesystem_Hierarchy#Directory_Tree KDE user data directory] of KDevelop. | The Code Generation plugin files are located into the [http://techbase.kde.org/KDE_System_Administration/KDE_Filesystem_Hierarchy#Directory_Tree KDE user data directory] of KDevelop. | ||
| − | Each resource has its own directory : | + | Each resource has its own directory: |
| − | *codegeneration/ | + | :*codegeneration/ |
| − | **'''templates/''' | + | :**'''templates/''' |
| − | ***cpp/ | + | :***cpp/ |
| − | ***php/ | + | :***php/ |
| − | **'''contexts/''' | + | :**'''contexts/''' |
| − | ***cpp/ | + | :***cpp/ |
| − | ***php/ | + | :***php/ |
| − | **'''generators /''' | + | :**'''generators /''' |
| − | ***cpp/ | + | :***cpp/ |
| − | ***php/ | + | :***php/ |
Each resource directory can contain directories to organize files in categories (typically, a directory by language). | Each resource directory can contain directories to organize files in categories (typically, a directory by language). | ||
| Line 68: | Line 68: | ||
The file hierarchy may look like this: | The file hierarchy may look like this: | ||
| − | *codegeneration/ | + | :*codegeneration/ |
| − | **templates/ | + | :**templates/ |
| − | ***cpp/ | + | :***cpp/ |
| − | ****'''''class.tpl''''' | + | :****'''''class.tpl''''' |
| − | ****'''''class.rc''''' | + | :****'''''class.rc''''' |
| − | ****'''''function.tpl''''' | + | :****'''''function.tpl''''' |
| − | ****'''''function.rc''''' | + | :****'''''function.rc''''' |
| − | ***php/ | + | :***php/ |
| − | **contexts/ | + | :**contexts/ |
| − | ***cpp/ | + | :***cpp/ |
| − | ****''class.ui'' | + | :****''class.ui'' |
| − | ***php/ | + | :***php/ |
| − | **generators/ | + | :**generators/ |
| − | The template configuration file is handled as a '''KConfig''' file : | + | The template configuration file is handled as a '''KConfig''' file: |
| − | < | + | '''templates/cpp/class.rc''' |
| + | <syntaxhighlight lang="ini"> | ||
[Template] | [Template] | ||
| − | contexts=cpp/class | + | contexts=cpp/class.ui |
| − | includes=cpp/function | + | includes=cpp/function.tpl |
| − | </ | + | </syntaxhighlight> |
| − | + | :The '''contexts''' value is a list of context names | |
| + | :The '''includes''' values is a list of template names. It contains the templates included into the current template with the '''{% include %}''' directive. It seems repetitive but it is necessary to allow the plugin to load the contexts needed by the included templates (until Grantlee provide an API to parse template files). | ||
| − | + | === Configuring generators === | |
| + | Creating a generator consists in creating a '''.rc''' config file. | ||
| − | + | :*codegeneration/ | |
| + | :**templates/ | ||
| + | :***cpp/ | ||
| + | :****''class.tpl'' | ||
| + | :****''class.rc'' | ||
| + | :****''function.tpl'' | ||
| + | :****''function.rc'' | ||
| + | :***php/ | ||
| + | :**contexts/ | ||
| + | :***cpp/ | ||
| + | :****''class.ui'' | ||
| + | :***php/ | ||
| + | :**generators/ | ||
| + | :***cpp/ | ||
| + | :***'''''class.rc''''' | ||
| − | + | As the template configuration file, the generator configuration file is is handled as a '''KConfig''' file: | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | == | + | <div class="codebox"> |
| + | <div class="codebarbg">generators/cpp/class.rc</div> | ||
| + | <syntaxhighlight lang="ini" line> | ||
| + | [Generator] | ||
| + | actionName=Create class | ||
| + | desciption=Create a simple C++ class with member functions. | ||
| + | |||
| + | [Files] | ||
| + | templates=cpp/class.tpl | ||
| + | outputs=<classname>.cpp | ||
| + | </syntaxhighlight> | ||
| + | </div> | ||
| + | |||
| + | :The '''actionName''' value is the name of your generator like it appears in the Code Generation menu. | ||
| + | :The '''description''' value is a short description of your generator used to display a tooltip. | ||
== See also == | == See also == | ||
{{UserbaseLink|KDevelop4/Manual/Plugins:_Code_Generation}} | {{UserbaseLink|KDevelop4/Manual/Plugins:_Code_Generation}} | ||
| Under Construction |
|---|
| This page is under construction. This page is actively being developed and updated with new information, and may be incomplete. You can help by editing this page |
KDevelop Code Generation plugin uses Grantlee template engine to generate code (like classes, functions, code documentation etc.).
The goal of KDevelop's Grantlee implementation is to provide a very flexible template system. The architecture is based on 3 main concepts which results as resources for the plugin:
Contents |
The template define the body of the generated code.
Templates syntax is the same as the Grantlee templates syntax. All features provided by its engine are available.
The user interface is what the end-user uses to configure the template. You should use Qt UIs files created with Qt Designer to define your own context.
Code Generation plugin uses form fields to generate data. Typically, binary valued fields (checkboxs) will result as booleans, single valued fields (text fields, text areas, single selection lists, radio buttons) as strings and multi valued fields (multiple selection lists) as lists of strings.
For convenience, all variables defined in a context are included in a namespace. It enables the use of several contexts into a single template without variables names conflict.
Many global contexts are always available in any template. They give data which does not need to be configurable by the end-user.
Currently available global contexts are :
The generator contains the templates and contexts used to generate a coherent object (a class, a function, the documentation for an item in the code etc.).
The generator also define outputs. Each template is associated to one output where generated code will be put.
The Code Generation plugin files are located into the KDE user data directory of KDevelop.
Each resource has its own directory:
Each resource directory can contain directories to organize files in categories (typically, a directory by language).
Then the files can be placed in a category directory or in directly in a resource directory, if the template/context/generator does not concern a specific language.
For each template, 2 files should be created:
The file hierarchy may look like this:
The template configuration file is handled as a KConfig file:
templates/cpp/class.rc
[Template] contexts=cpp/class.ui includes=cpp/function.tpl
Creating a generator consists in creating a .rc config file.
As the template configuration file, the generator configuration file is is handled as a KConfig file:
[Generator]actionName=Create class
desciption=Create a simple C++ class with member functions.
[Files]templates=cpp/class.tpl
outputs=<classname>.cpp
The KDevelop4/Manual/Plugins:_Code_Generation page on Userbase.