Sunday, November 18, 2007

Duplicate line - How does it work?

Note: Information in this post is based entirely on observation. It contains no insider knowledge whatsoever.

So how does it work? Well..."Duplicate line" doesn't strictly duplicate the line. It creates a transformation of a copy of the line and then expands the transformation into a new line underneath the original.

So why does it do that? Wouldn't it be simpler to just copy the line of code?

Well yes but then again, how useful would that really be?

You'd have an exact copy of the line you already had. But then typically you don't want an exact copy of the code. You typically want a version which is slightly different.

For example you might want to duplicate a variable declaration but then change the name of the variable which is being declared. Or perhaps duplicate a method call but then alter the parameters that are passed.

In these cases it is better for CodeRush to help you out by setting up which part of the new code is selected and placing field wrappers around other selected bits.

And this is what it does.

It can do this because of several pre-setup rules each comprising a pattern to match code and another to construct code.

Thus a match pattern of...
%SubOrFunction1%%Identifier1% \( %ParamsDec1%? \) (%As1%%Type1%)? %EndOfLineComment1%?
...together with a Create pattern of ...
%SubOrFunction1%«BlockAnchor»%Identifier1%«Cursor» ( %ParamsDec1% ) %As1%%Type1% %EndOfLineComment1%
...allows us to duplicate...
Dim SomeVar as Integer
... and have CodeRush immediately highlight "SomeVar" in the duplicate ready for replacement.

No comments: