Monday, November 12, 2007

Selection-Inversion

Selection-Inversion (CTRL-SHIFT-I) is a mechanism for transforming one simple code structure into another or back again.
For example:

From .... "X = True" ....into.... "X = False"

..OR..

From .... "A = B" ....into.... "B = A"

So how does this work?

We it's all down to pattern matching and the wonderful alias engine provided by DevExpress.

Each Selection-Inversion requires a pattern to match and another pattern to replace.

The expressions that allow the above Inversions are simple in nature and, unlike most regular expressions, quite readable.

For example, the first Selection-Inversion above matches using...
-------------------------------------------------------------
%Identifier1% = %BooleanValue1% %EndOfLineComment1%?
-------------------------------------------------------------
...and replaces with...
-------------------------------------------------------------
%Identifier1% = «?InvertBoolean(%BooleanValue1%)» %EndOfLineComment1%
-------------------------------------------------------------
This shows an additional strength of Selection-Inversion. That of the StringProviders. I'll cover StringProviders in another post but as you can see from this small example. a StringProvider is essentially like a function which takes 0 or more parameters and returns a string. A very simple idea but one which allows for a lot of customization.

The example here uses an "InvertBoolean" StringProvider which, as it's name suggests, inverts the passed boolean and passes out the text which represents it's opposing value.

The second example matches using...
-------------------------------------------------------------
%Identifier1% = %Identifier2% %EndOfLineComment1%?
-------------------------------------------------------------
...and replaces with...
-------------------------------------------------------------
%Identifier2% = %Identifier1% %EndOfLineComment1%
-------------------------------------------------------------

Note the subtle change here. Only the numerals 1 and 2 have switched positions. The numerals can be tacked on to any alias and allow each alias type to be used multiple times in the match string and then placed in arbitrary positions in the replacement string.

As you can see the Selection-Inversion is quite powerful.

There are also a few more Inversions in the shipping version of CodeRush which deal with inverting loops so that they progress in the opposite direction from which they started but I'll leave these for you to investigate.


No comments: