Templates - how would you use them?

Miha Čančula miha at noughmad.eu
Thu Jun 14 09:41:13 BST 2012


Hello!

As you might know, this summer I'm working on a template system for
KDevelop. You can probably imagine that they are most useful for code
generation, which is why I started with the existing ClassGenerator and
replaced the code-writing part with the rendering of a Grantlee (
http://www.grantlee.org/) template. This eases maintenance, allows
templates to be changed and shared, reduces the dependence on language
plugins, and is user-configurable. Apart from a page for selecting the
template, I reused the existing CreateClassAssistant, so there are minimal
user-visible changes.

The new functionality (loading, rendering templates) mostly works. But now
I'm looking for input from both users and future writers of such templates.
You know what kind of code you write and where automatic generating would
be most useful.
*
Template variables and functions*
The Grantlee template library works in a way that allows template access to
variables, exposed as Context, and functions, which are loaded in plugins.
I have already added some of both. Variables are the easier and arguably
more useful, so I already added a bunch of them. Class name, base classes,
declarations, methods and members grouped by access, license. Templates
also can specify its own options, for which a GUI is shown and their values
are gives as additional variables. On the other hand, I included only a
small number of functions. So far, these are conversions from
under_score_notation to CamelCase and back, and prepending characters in
from of each line (like how it's usually done with *'s in license headers).

Grantlee templates can also "include" other templates. This means that
instead of compiled functions, we can provide common functionality in the
form of templates. Two examples I already implemented are function argument
lists and function declarations, but there certainly are others. They can
also be added to the default KDevPlatform installation and made available
to all templates.

I would like to know what variables and functions would simplify your life
if they were available in templates. Since they are loaded as plugins,
anyone can write additional functions and use them, but I would like as
much of commonly useful functionality to be in KDevPlatform.

*Other uses for templates*
As I said, templates are most useful for generating documents, in our case
source code files. So far, this means creating new projects (Project => New
>From Template) and new classes (still only in the "file-templates" branch).
However, I imagine there are other instances where templated code
generation is useful. Any sort of generated code, like adding documentation
or properties with getters/setters would benefit from templates because of
easier support for different languages.

Is there anything else you have in mind?

Thank you,
Miha Čančula
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kdevelop/attachments/20120614/05d7b3d0/attachment.html>


More information about the KDevelop mailing list