New Plugin: CR_StringFormatter

Another CodeRush Feature Workshop (Part 1 and Part 2) has yielded a plugin, and this one is truly awesome.

String Formatter works in 3 separate ways to increase your productivity:

Tab to Next Reference Extension

Tab among references to a string token and variable/expression whose value will replace it at runtime.

Simply place the caret on a String.Format token (ie {0}) and then press the Tab key.

TabToNextStringToken
The Tab to Next Reference engine is engaged, and the user may now tab between references to the token, and the argument which will be used to fill it at run-time.

Note: For now, this new Tab To Next Reference functionality will require the presence of our previous CR_PrimitiveTab plugin and it’s required configuration. We hope to address this in a future version of CodeRush.

CodeIssue - Format Item Index Too Large

Wouldn’t it be useful, if studio could let you know when you reference a token inside a String.Format, but then fail to supply a corresponding argument to fill it. The “Format Item Index Too Large” CodeIssue, highlights when the index of a token in a String.Format string references an argument which is not present.

In this example, note how the string contains references to 3 distinct tokens, {0}, {1} and {2}. However there are only 2 further arguments.

StringFormatterCodeIssueBefore

This will crash at runtime and Studio does nothing to warn you of this. CR_StringFormatter to the rescue…

See below, how CR_StringFormatter’s CodeIssue highlights the offending token as an error, and provides a message to help you understand what is wrong..

FormatItemIndexTooLarge

The ‘Format String’ Builder

Do you have all the FormatString notations memorized? You do ? … Really? … Wow.. Well good for you.

I don’t… Sad smile 

…but then l I don’t need to. I have CR_StringFormatter Open-mouthed smile

[Are you ready? This is even more awesome than the first 2 bits.]

The 3rd piece of awesome productivity that CR_StringFormatter provides you with, is a Format String Builder. This is a context sensitive FormatString Builder dialog.

It’s like our Color swatch dialog, only for format strings and is is available when the your caret is within a String.Format token. (ie {0})

Simply choose "Format Item..." from the Code SmartTagMenu. 

FormatItem

 

When “Format Item…” is chosen, the user is presented with a dialog designed to aid them in the building of a Format String suitable for their needs.

This dialog works with String, Ints, Real (floating point) numbers , Dates and Times. Depending on the type of data being dealt with, you can expect to be given help with alignment, leading or trailing zeros, decimal places, currency, hex conversion and more.

Go on, drink it in…

StringFormatter

Upon acceptance, this carefully crafted string will replace the original token in the string from which the dialog was requested.

Isn’t that awesome. Yet more information that you don’t have to remember (or go look up) every time you need it.

Now download from the community site, and be sure to keep an eye out for more CodeRush Feature Workshops

17 comment(s)
Bradley Uffner

I can't get the format builder dialog to open.  The options dialog says that the plugin is loaded, but the smart tag never contains the "Format Item" menu.

My code looks like "Dim s As String = String.Format("The date is {0}", #3/11/1979#)"

with the cursor on the {0}.

25 July, 2011
Rory Becker - DevExpress

I'm going to look into this straight away. Can you file an issue on the community site so we can track this a little better. Thanks

25 July, 2011
Bradley Uffner

Just a quick test.  It works fine in C#, but not in vb.net.  at least for me.

Going to go open that issue for you now.

25 July, 2011
Rory Becker - DevExpress

Just to confirm, that issue is now fixed and published as build 1946. this is now available as the default build for this plugin.

26 July, 2011
Mark DuCharme

Nicely done Rory and Mark. Good stuff!

26 July, 2011
Tim Shnaider

Thanks guys, very useful

27 July, 2011
Imar_Spaanjaars

Nice plugin indeed.

I had trouble configuring the plugin at first though. This article says "this new Tab To Next Reference functionality will require the same little configuration fix as described in our previous post" but that's not enough. You also need that very plugin installed, or the InPrimitive scope is not available.

Hope this helps anyone running into the same issue.

Cheers,

Imar

29 July, 2011
Rory Becker - DevExpress

@Imar_Spaanjaars

A very good point. Thanks very much. I've amended the instructions appropriately.

29 July, 2011
Johnny K

seems really nice,

will it help me make a concat string and align it in 4 colums?

like

Johnny          123           value

Jak                25666       value2

is it possible to do that with this plugin? :(

VB.net always :D

29 July, 2011
Tomas Juricek 3443

Nice, but what about other contexts where formatting is used? There are many methods which take format string and arguments as parameters. I.e. Debug.Print.

3 August, 2011
Rory Becker - DevExpress

@Karel: That's a good point. Could you file an issue on the community site listing as many as you can think of.

4 August, 2011
Imar_Spaanjaars

Hi Rory,

Seems like this is now part of Code Rush 11.2.8? I can see the plugins being loaded in the options dialog.

However, despite the presence of the plugin, I can't seem to tab from a string.Format token to its value. I just get a tab inserted instead.

Any ideas? I tried this on a clean machine without older CodeRush settings or plugins, and it doesn't seem to work. Also, I had to enable "InIdentitfier" on the Tab shortcut to get Tab to Next Reference to work at all. Is this by design?

Best regards,

Imar

11 February, 2012
Rory Becker - DevExpress

Hi @Imar.

Indeed this functionality was included in the official 11.2 release of CodeRush. If you are using this version or later, (especially on a clean machine with no previous settings) then it should 'Just work'.

If this is not the case, I suggest contacting support either via support@devexpress.com or via our Support Center (www.devexpress.com/.../Center)

5 March, 2012
Michael_Hachen

Hi Rory,

I would like to use this plug-in. I have installed Refactor!Pro 11.2.10. However there is no smarttag menu "Format item..." Is is possible that this plug-in doesn't work in VS 2008 with vb.net? It works in VS 2010 with c# (without the Tab to Next Reference Extension).

Thanks for your help!

Mike

16 April, 2012
Rory Becker - DevExpress

Hi Michael,

Yes I believe this plugin only works with CodeRush installed rather than Refactor Pro only.

Typically Refactoring only plugins will work with RefactorPro, but this is not such a plugin. Indeed it provides no refactoring functionality.

16 April, 2012
Michael_Hachen

Hi Rory

I have installed DxCore 11.2.10, CodeRush 11.2.10 and Refactor!Pro 11.2.10.

Do I understand you correct: CR_StringFormatter doesn't work on VS 2008 with vb.net?

16 April, 2012
Rory Becker - DevExpress

Sorry Michael,

I understood from your previous message, that you only had Refactor Pro installed rather than CodeRush.

This plugin was written for CodeRush 11.1 and does indeed support both VS2008, VS2010 and both C# and VB.Net

However this plugin also shipped as a part of the full  version of CodeRush 11.2, so you should no longer need the Community plugin version.

If after removing the Community plugin version of this plugin, you still cannot get the functionality to work, I suggest contacting support@devexpress.com

16 April, 2012

Please login or register to post comments.