Meta coding? Macros and regular expressions.

I have just read a short article on CodeProject about using regular expressions in coding (

http://www.codeproject.com/article.asp?tag=38295622841838755) and decided to comment on that.

In situations as in that article and especially when converting a table or list of values into a sequence of assignments, calls, declarations etc – I prefer to use macros. It is easier to record and play a macro that does something, than write a regular expression for the same thing. Sometimes only one of the two options is possible so I value them both highly, as they enable to really streamline code generation. Aside from that – it is not something they teach at those programming courses at the university (at least they did not on mine) – so it looks so pro… :]

In this example, using VS2005 I would record the macro as following…

Piece of initial text
workstation id=LEBOWSKI;packet size=4096;user id=sa;data source=LEBOWSKI;persist security info=True;initial catalog=TheInternet;password=protected

The sequence of actions to record the macro:
0. Put the cursor at the beginning of the text
1. Ctrl+Shift+R (starts recording)
2. Type: options.Add(“
3. Open a find window (Ctrl+F) and look for character ‘=’
4. Type: “, “
5. Press: Ctrl+Right Arrow (jumps to the end of the word)
6. Type: “);
7. Press Enter for new line
8. Press Delete to remove the semicolon
9. Ctrl+Shift+R to end recording

Now while holding Ctrl+Shift press P a couple of times and you’ve got your output text with sequence of function calls.
You can even see the actual VB code (Alt+F11) for the macro and edit it if you want to get something more custom, but that probably does not happen very often – at least I have not used that yet.
The key settings for macros might be different in your environment, so you might need to check them or set them up.

Using a similar technique I often generate properties in C# – just starting with a list of a few dozen words – you just have to change one word into a property, remembering to start with the caret at the beginning of the first word and ending with the caret at the beginning of the second word and using clipboard to put the word in the source of the property a few times.

That is most useful when you want to read some data from a file – e.g. an XML one. You get a list of tag names in your source code, record and use one macro to generate the properties with the same names as XML element names to store the values and then another macro to retrieve values from the XML file and assign them to these properties.
You can create hundreds of lines of tedious code in a matter of seconds. Real robust.

The reason why I prefer macros when possible is that you do not have to remember all the special characters and the syntax used in search and replace strings, which is a problem if you only need them once in a while – especially, that they differ among various editors – they even differ between VS and .NET System.Text.RegularExpressions namespace.

The macro code sample below can be used to transform a list of names separated by a new line only into a sequence of properties of a C# class, nicely enclosed in #region outlining tag. Just open the macro editor and replace the code of the RecordingModule with the one below.

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module RecordingModule

    Sub TemporaryMacro()
        DTE.ActiveDocument.Selection.EndOfLine(True)
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        DTE.ActiveDocument.Selection.Text = "#region "
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.Indent(2)
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "#endregion"
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.Indent(2)
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "private string m_"
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = ";"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "public string "
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "get"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "return m_"
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = ";"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "set"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "m_"
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = " = value;"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.LineDown()
        DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        DTE.ActiveDocument.Selection.LineDown()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.DeleteLeft(2)
    End Sub
End Module

Advertisements

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: