New CodeRush Plugin – SplitTag

This is certainly one of the more simplistic plugin’s I’ve written, and yet it’s also one I use all the time.

Imagine that you have the following fragment of HTML and you’d like to split it in half.

SplitTagBefore

  • Simply place your caret where you’d like to split the tag.
  • Hit your CodeRush \ Refactor key

SplitTagMenu

  • Select ‘Split Tag’ from the menu
  • … and CodeRush will break your single tag into 2 of the same type.

SplitTagAfter

This plugin can be particularly useful when creating <li> tags within various lists.

CR_SplitTag

Availability

This plugin is available from either The Visual Studio Gallery or from GitHub

The source is available on Github

New CodeRush Plugin – CR_CollapseXML

Another new plugin! This one requested by a customer so they could collapse some XML (or HTML) on command.

Is there any way to have CodeRush collapse HTML or XML elements which have a ‘name’ or ‘id’ attribute?

The theory behind this is that those elements with an id or name attribute, are typically the ones that hold the high level structure. Collapsing these items will cause the structure of the HTML to be revealed.

In some ways it ‘s like CodeRush’s own Collapse to Projects functionality in solution explorer.

The user wishes to get a high level view of their HTML so that they can decide which area to drill back down into.

Now CodeRush doesn’t naturally support this functionality for HTML/XML out of the box, but you guys know me by now Smile

Yeah I wrote a plugin Smile

CollapseXML for CodeRush

CollapseXML works (despite it’s name) on both HTML and XML. It adds a new command (CollapseXML) which you can bind to any key you like.

Once installed and configured, simply place your caret within an XML or HTML file, and invoke the command using your configured key.

CodeRush will happily collapse your XML/HTML from…

HTMLExpanded

… to…

HTMLCollapsed

Availability

This plugin is available from either The Visual Studio Gallery or from GitHub

The source is available on Github

New CodeRush Plugin – Sort Format Tokens

Some programmers feel that they have some sort of very minor OCD. Despite no real rational reason, they feel that certain things should be a certain way. The world seems out of place, slightly of kilter, slightly more irritating or distracting when these things are not quite as they would like them to be.

Of course when I say ‘Some programmers’, I certainly include myself in that group.

Today’s plugin is kind of an OCD plugin. It’s just my personal attempt to scratch a little itch. I’d be amazed if this helps anyone else at all. I’m just putting it out there as an example of just how flexible CodeRush can be.

When I set out to create it, I had no other purpose in mind, than the appeasement of that part of my brain, which objected every time I didn’t adjust my code to this specific pattern.

That said, the plugin does have some benefits as a natural consequence of the way it works.

Sort Format Tokens

This plugin operates on a subset of the String.Format calls.

Consider the following call:

SortFormatTokensExample1

It’s simple and it does exactly what you need it to. However over time you might feel the need to add more elements to the message:

SortFormatTokensExample2

Something about this statement irks me. Something just doesn’t sit right. So I find myself rewriting it

SortFormatTokensExample3

This new code operates exactly the same as the old code. There isn’t even a performance benefit here.

However my particular brand of very minor OCD feels better and I’m able to scan through the code and comprehend it quicker.

The Problem

Changing one statement into the other is a manual task. There is the risk of typos and logic errors, and of course it takes time to achieve.

Not very much time I guess, but every moment that I spend thinking about this sort of thing, is less time spent on solving business problems and adding value.

Therefore I did what I usually do when I spot a coding task I’d prefer not to have to do manually, which has an predictable pattern to it….

I wrote a plugin Open-mouthed smile

The Solution –> Sort String Tokens for CodeRush

This plugin does exactly what it says on the tin.

It rewrites the a String.Format call so that…..

  • The string tokens are (where appropriate) in numerical order.
  • The arguments that follow the string are in the same matching order.

Simply place your caret within the first argument of a String.Format call and choose 'Sort Format Tokens’ from the Code subsection of your CodeRush\Refactor Smart tag menu.

SmartTagMenuSortFormatTokens

Bonus Features

Beyond the original spec of “Sort my string tokens so that they look nice”, SortFormatTokens is also careful to correctly interpret several corner case scenarios.

  • It deals correctly with sequences of tokens with missing items.
    Gaps in the sequence are removed.  “{1} – {3} – {6}”  becomes “{0} – {1} – {2}”.
  • It deals correctly with string tokens that have formatting information.
    Formatting information is retained within the renumbered token. “{1:00} – {0} – {2}” becomes “{0:00} – {1} – {2}”
  • It deals correctly with duplicate usage of a token within a string.
    The 2nd and further instances of any given token are renumbered the same way as the first instance. “{1} – {0} – {1}” becomes “{0} – {1} – {0}”.
  • It also removes any unreferenced arguments from the call, optimizing your call to be faster in the process.

Where can I get this wonderful plugin from?

As usual this plugin is available from the Visual Studio Gallery and from GitHub.

The source code is available on GitHub

New CodeRush Plugin – CR References Metric

Just a quick little post to announce a tiny little plugin which provides member reference counts in the form of a metric.

Simply click the metric tile and choose ‘References’ from the dropdown.

ReferencesMetric

CodeRush will then display the Reference count of each member next to each one.

Availability

As usual this plugin is available from the Visual Studio Gallery and from GitHub.

The source code is available on GitHub

New CodeRush Plugin – CR_CorrectReturnType

In my previous post I was extolling the virtues of Convert to Function.

You’ll recall that it is used to alter the return type of the current function, from void to the value of the expression on the return statement at the caret.

MethodReturningIEnumerableInt

Well what if you have a pre-existing function which returns a string, and you need to alter the function to return an int.

Convert to Function is only available if the method is of type void. It is not available if the function returns anything else.

Enter CR_CorrectReturnType

My latest plugin, Correct Return Type provides the same functionality as Convert to Function, however it works where Convert to Function does not.

If the active function’s return type is non-void, and differs from that of the return statement at the caret, then Correct Return Type will become available instead of Convert to Function.

CorrectReturnType

Availability

As usual this plugin is available from both the Visual Studio Gallery and from GitHub.

The source code is available on GitHub.

More Posts