Using Grantlee for code generation

Grantlee rustles on toward a release. I recently added one more example application showing the code generation opportunities it makes possible.


[OGG link]

In the video I edit and create some Qt Properties and methods with arguments in a simple UI, fill in some additional metadata, and then generate code representing the content of the dialog. The actual code is generated with some Grantlee templates. Simple loops are used to create the properties in the dialog, and if conditions for empty argument lists or the use of the pimpl idiom.

This could potentially be a time saver for generating the boring bits when creating a new class. Beyond that though, it is language independent. The structure and grammar of the generated code comes entirely from some template code files. The output language is entirely contained in the templates, and the Context of the rendering stage describes the content of the class. That means that it can also generate python code with the same Context and the same UI. There is a strong separation and encapsulation of language structure in the templates themselves.

Use of the Qt Resource system means that additional language templates could be added and distributed as external binary resources. So potentially someone could write and ship java templates long after the initial application was deployed.

I didn’t add that myself because if I let feature-creepism take hold in the example applications, there would never be a release of Grantlee. So back to that.πŸ™‚

Unfortunately the video is quite jumpy, but if you want to try it out for yourself:

git clone git://gitorious.org/grantlee/grantlee.git
cd grantlee
mkdir build && cd build
cmake .. && make && make install
cd ../examples/codegen
mkdir build && cd build
cmake .. && make && ./codegen

So who wants to turn it into a QtCreator or KDevelop plugin?πŸ™‚

10 Responses to “Using Grantlee for code generation”

  1. Milian Wolff Says:

    I’ll make sure this is used in KDevelop’s file templates for 4.1πŸ˜‰

    And you forgot Kate’s snippet support (shared with kdevelop), lets see whether we can get it in there as well!

    • bbroeksema Says:

      Work harder Milian! I promise to switch back to KDevelop when you integrate this in a sane way. =:)

  2. The User Says:

    Hi!

    Shouldn’t be too complicate to integrate it in KDevelop.
    What would be cool for the future?
    1. The comments (license-header, doxygen) do not look very good, you should have a look at ArtisticComment (http://www.gitorious.org/kate/kate/trees/master/playground/kte_acomment), it consists of a small library to format comments (written exactly for that purpose: file-templates etc.) and a GUI for KTextEditor.
    2. Consider how the method-overloading-dialog could be integrated.
    3. Maybe there could be hints, where a section begins and ends. KDevelop could handle some cursors and then you could change parts of the class using the dialog.
    4. Put it into +kde-developers.πŸ˜‰

    Jonathan

    PS: I was a bit surprised about winston@smithfamily.org, he has no family and apart from that the family would not be important enough for a domain, there are other priorities.πŸ˜‰

  3. steveire Says:

    Hi Jonathan,

    Those are all very good points for future work on this. A real use of it would do well to have those uses covered. However, I don’t think the example application in Grantlee itself should be extended too much more. It’s just there to show the Grantlee specific stuff that’s possible. Those features belong in a real IDE and I’d like to see them in KDevelop too.

    We can see about doing that in a few months when people are thinking about KDevelop 4.1+.

    Execellent point about WinstonπŸ™‚. It’s a while since I’ve read the book actually.

  4. The User Says:

    I hadn’t noticed that it is an example before actually downloading it.πŸ˜‰ Of course you are right.

  5. The User Says:

    Something else:
    Do you think it would be possible/useful to integrate Grantlee into KAppTemplate?

    • steveire Says:

      I don’t know much about KAppTemplate, but I’ve just had a quick look.

      It would be possible to use Grantlee in that application. Whether that is useful depends on whether the KAppTemplate developers want to do things that are only possible with loops and conditionals and other things Grantlee makes possible.

      Features like those in the example application might make sense in KAppTemplate if they want them.

      • The User Says:

        KAppTemplate (and Plasmate, too) uses a very simple template-syntax, there are simply ~6 placeholders like %{APPNAME}, %{APPNAMELC} (lower case), %{EMAIL} etc. It is very unflexible, e.g. some string-transformation would be often useful (for CamelCase etc.), but it is impossible, there is no proper system for license-headers etc. I guess it would not be difficult to allow such things in Grantlee. There could simply be an option in the .kdevtemplate-files (type=Grantlee).

  6. Grantlee 0.1.3 now available « Steveire’s Blog Says:

    […] The most interesting part of this release is the support for generating ruby code in the code generator example. […]

  7. How do you use Grantlee? | Steveire's Blog Says:

    […] There is also rather advanced Grantlee integration available in KDevelop for new class generation, using the same principles I blogged about some years ago. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: