Code Template Add-in Enhancement

Environment: ******–>

I have added some additional (hopefully useful 🙂 features to Darren Richards original Code Template add-in.

The three main features are:

  • Sub menus.
  • Simple Parameter substition
  • Source code column alignment.

Please refer to Darren’s original article for the basics of how to set up your template file.

Sub Menus

You can now create sub menus in your code template file by the following syntax.

#{&Menu|Sub Menu 1|Sub Menu 2|Sub Menu …

The | separator is used to delimit the sub menus. I find this very useful way to organize your code
into different categories.






#{&MFC|Property Sheet|Declaration


#{&MFC|Property Sheet|Definition


#{&Classes|Generic Class|Declaration


#{&Classes|Generic Class|Definition


You end up with the following menu structure:

MFC            >> Dialog        >> Declaration
Property Sheet >> Declaration
Classes        >> Generic Class >> Declaration

Simple Parameter Substitution

I have added support for basic (I mean really basic) parameter substitution. This allows you
to parameterize your code templates which allows you to define more generic code templates. This
is done by using %1, %2, %3, ... to represent different parameters in your code template.
An example code template is provided below:

#{&Classes|Standard Class Declaration(ClassName)
/* C%1 */
class C%1
C%1(const C%1& r%1);

C%1& operator=(const C%1& r%1);

When this template is selected from the menu, all parameters are parsed and a dialog box
comes up with an edit control for each parameter. Simply fill in the parameters and voila
the parameters are bound to to the code template. For example if I typed OOGA for the parameter
%1 I would get the following bound template:

/* COOGA */
class COOGA

COOGA& operator=(const COOGA& rOOGA);

There is a special reserved parameter %0 which represents the current editor selection. This allows
you to define a code template as follows:

#{Class &Header
/* %0 */

This allows you to highlight a class in the editor and generate code templates based upon that selection
without being prompted.

Source Code Column Alignment

Nothing bugs me more than code that is not formatted according to my style. I just hate the
way the MFC wizards insert code formatted so you cannot even read it (what a mess). I spend
half my time reformatting the code! I added column alignment support so that I can define
code templates so that they will align according to my or anybody's style. This of course is only
useful in conjunction with parameter substition as stated above. You use the %Cnnn symbol to specify
which column to align to, where nnn represents a column number. So given our class header example
above you can define the template more exactly by:

#{Class &Header
/* %0 %C070*/

This ensures that the */ is going to align in column 70 giving you a perfectly square box.

A complete example is provided below of all the new features:

#{&MFC|CDialog Declaration(ClassName)
/* C%1Dialog %C070*/
#ifndef __%1Dialog_H__
#define __%1Dialog_H__

class C%1Dialog : public CDialog
static o_bool %C034Do%1();


virtual void %C034DoDataExchange(CDataExchange* pDX);

enum { IDD = IDD_%1 };

virtual BOOL %C034OnInitDialog();
virtual void %C034OnOK();


When %1 is bound to Printer you get the following output:

/* CPrinterDialog */
#ifndef __PrinterDialog_H__
#define __PrinterDialog_H__

class CPrinterDialog : public CDialog
static o_bool DoPrinter();


virtual void DoDataExchange(CDataExchange* pDX);

enum { IDD = IDD_Printer };

virtual BOOL OnInitDialog();
virtual void OnOK();


Tips on creating code templates:

  1. Name the parameters in the menu description when defining the template.

    For example the template below generates a set of const& Getter and Setter functions.
    It takes two parameters which are the Type and Name of the Getter and Setters.

    #{&Properties|const && Declaration(Type, Name)
    %1 %C034m_%2;

    const %1& %C034Get%2() const;
    void %C034Set%2(const %1& r%2);


  2. Work Backwards. Define your template in code first, then using your text editor search and replace
    to generate the parameters. for example the above template was originally written in code first like:

    #{&Properties|const && Declaration(Type, Name)
    CType m_Name;

    const CType& GetName() const;
    void SetName(const CType& rName);


    then using my text editor hilite the code template block and Search and Replace, Type with %1 and
    Name with %2, generating the template below:

    #{&Properties|const && Declaration(Type, Name)
    C%1 m_%2;

    const C%1& Get%2() const;
    void Set%2(const C%1& r%2);


    this allows you to test the syntax of your templates with your compiler in code first before attempting
    to parameterize them.

  3. Apply formatting.

Future Plans/Ideas:

To make these code templates more useful would be, instead of having them as a popup menu, have them more
integrated into the developer studio. For example perhaps as another tab in the workspace window.
You could have a tree control representing the code templates in another tab in the workspace window then
by double clicking/dragging dropping... you could easily add code or whatever to your project. You could
even drag a block of code from the editor to the code template window to define new templates!!!

I hope you find these minor modifications useful like I have. Don't hesitate to write me with bug fixes

Good Luck,

Sandy Place
Resonance Software Inc.

Download source - 20.5 Kb

More by Author

Must Read