New CodeRush Plugin – CR_RemoveStringToken

A new plugin… What does it do?

Example 1

So imagine we have the following code..

RemoveStringTokenExample1

Imagine now, that we decide that it’s not important to explain ‘where’ we were searching, when reporting this error.

Normally we would…

  • Highlight the string token {1} (including it’s associated text).
  • Hit delete.
  • Highlight the “Y” argument (including it’s leading comma).
  • Hit delete.

RemoveStringToken allows us to remove the token and it’s associated parameter in a single step.

  • We place our caret on the token we want to remove ({1} in this case)
  • Choose Remove String Token from the Code menu.

RemoveStringTokenExample1a

A little tidying is required to remove ‘ in ‘, but that’s a whole lot simpler than before.

Example 2

In this next example, things are slightly more complicated.

RemoveStringTokenExample2

Users aren’t terribly good with codes, so we’ve decided to remove token {1} from the message.

The manual steps to achieve this are mostly the same as the previous example.

  • Highlight the string token {1} (including it’s associated text)
  • Hit delete.
  • Highlight the “Y” argument. (including it’s leading comma)
  • Hit delete.

However this time there is an additional requirement.

Since we’re removing a string token from the middle of the sequence, we need to renumber the tokens that are numerically higher than the one we’re removing.

So this time our manual steps would also include:

  • Renumber references to {2} so they reference {1} instead.

Of course if we had additional references to {1} in your string, we’d have to remember not to renumber those other tokens.

I’m sure you agree that’s a bit of a pain.

RemoveStringToken handles the token removal, the parameter removal and the token renumbering, all in a single step.

RemoveStringTokenExample2a

Once again there are some additional characters in need of a trim. but these are easily dealt with.

Checks and Balances

It’s worth noting that with any simple sequence of steps that a human might make, there is always the possibility of mistakes.

RemoveStringToken checks several things automatically every time, so you don’t have to.

  • Arguments are only removed if there are no remaining references to the target token.
  • Tokens are only renumbered if there are no remaining references to the target token.
  • Console.WriteLine and String.Format are both supported.
  • If no tokens remain within the string, and the outer call is to String.Format, then this outer call is removed.

Availability

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

The source code is available on GitHub

 

no comments
No Comments

Please login or register to post comments.