Sunday, November 18, 2007

Refactoring vs Simple Code Transformation

After conversing with Trevor about the inner working of duplicate line, and a retelling of my understanding of said feature here, I have found myself wondering about a potential new feature.

Duplicate line is an interesting feature which takes advantage of RegEx to match expressions to code and then create further code based on the original code.

The self-defined purpose of "Duplicate Line" is to duplicate the line on which the caret resides.
Side-Note: I have a suggestion, registered here, which suggests that it would be useful to invoke duplicate line retroactively, when one's caret resided already on a blank line and one wished to insert a duplicate of the line above.
To this end, the 'create' pattern (RegEx) is largely the same as the 'match' pattern.

But is there any reason that they should be?

Well yes... for 'duplicate line'.... they should be, but what about some other function (from here referred to as 'code-transform') which we could activate manually in the same way and have the system match the preceding line of code and transform it into something else.

Perhaps said feature could even popup a list of named patterns which it was felt matched the current situation so that the user could pick the most appropriate one.

Does this sound familiar? Yes of course it does. It's RefactorPro isn't it?.

So why re-invent the (extremely streamlined, dead flashy and effective) wheel?

Well to be honest, I'm really not sure. My mind, like this post, started from 'Duplicate Line' and wandered away with itself from there.

I guess the difference is in how you create new "Code-Transforms" vs "Refactorings"

Refactorings are probably way more sophisticated and clever due to having a rather clever understanding of the code upon which they operate. But then again you do need to write a new piece of code for each one you create. There's the hassle of creating a new project, a new plugin, a new Refactoring and handling various events.

On the other hand, one might create a more basic type of code-transformation simply by setting up a few RegEx expressions.

This is theoretically made even easier by the existence of the DXCore's RegEx Aliasing System

So now Imagine that we have this new Code-Transform Action. We've hijacked the Ctrl+T keystroke and bound it to "Code-Transform" and we come across the following minor code...
Dim SC As New SomeClass
...which for reasons of taste and style (or lack thereof) we decide we would like to rewrite as...
Dim SC As SomeClass = New SomeClass
Well in exactly the same way as 'Duplicate line' works, you can create a match pattern...
%Dim1%%Identifier1%%mws1%%Initialization1%? %EndOfLineComment1%?
...and a replace pattern...
%Dim1%%Identifier1%%mws1%%As1%%Type1%%Initialization1%? %EndOfLineComment1%?
... and this when triggered, would introduce the extra code needed to Refactor our simple example.

Of course the reverse could easily be performed as well by simply reversing the RegEx's in another "code-transform"

There are limitations to this simplistic system. Off the top of my head I can think of "single line only Refactoring", (and therefore) transformations cannot affect code-blocks (Loops).

but there might be some merit to this idea.

What do you think? All comments/thoughts appreciated

No comments: