CodeRush style typed templates for ReSharper
Asked Answered
I

2

5

I'm a big fan of CodeRush and their philosophy around templates. At my current job, we'll be doing a large amount of pairing and the consensus is a preference for ReSharper (v6), which pretty much puts me in a place where I MUST use it.

I'm not looking to start a CodeRush/Resharper war here. There are plenty of things to like about Resharper, but there's one thing I'm having a hard time getting past in ReSharper.

ReSharper's Live template mechanism, wile good, doesn't have built-in notions for typing the way CodeRush does (at least not as I can tell). A simple example is as follows. To gen the following code:

public String MyStringProperty { get; set; }

In CodeRush...I could type "as" ('a' for AutoProperty and 's' for string), then simply change the name of the property.

In ReSharper, I need to type "prop" (for Property), then set the type and and name.

There does not seem to be a similar notion for type awareness or type shortcuts in ReSharper's Live Templates. As such, there doesn't appear to be anything akin to the numerous two and three character templates to get you pre-typed variables, properties, methods, etc...

So, finally the question after all that background. Is there any way to replicate this notion of "typed templates" in ReSharper without creating a new live template for every template/type combination?

Illegal answered 27/12, 2011 at 16:9 Comment(0)
C
4

Currently, ReSharper does not support the usage pattern you're referring to. However, there are two options I can think of to make this possible.

  • One option is to generate live templates for all common use cases. I use this file, but please note that it hasn't been tested with R#6.1.
  • Another option would be to simply write a plug-in to do this. The ReSharper SDK has a HtmlZen example that does something very similar, i.e. expand a DSL string into a piece of code. You could easily do the same for mnemonic expansion.

Note that whichever option you select, you won't get an ability to expand templates by pressing Space — this is simply not possible in ReSharper right now.

Please let me know if you have further questions.

Dmitri

Chromosphere answered 28/12, 2011 at 14:18 Comment(2)
Dmitri,Thanks for the info. I'll see what I can do. Expanding a template with Space isn't really an issue for me. That was really just used to describe the process. Tab, or any other expansion mechanism is just fine.Illegal
I just tried importing the above file into 6.1, it appears to work well.Chromosphere
B
3

I don't think it's possible to get Resharper to have a template that takes a one letter name followed by a one letter type. You will have to create a new template for each type, e.g. 'as' and define it as:

public string $NAME$ { get; set; }

You may want to change where it is available, i.e. C# files where type member declaration is allowed.

You could rename the 'prop' templete to just 'a', then you would type: 'a', TAB, 's', TAB, TAB, 'property_name', TAB

Bruxelles answered 28/12, 2011 at 10:7 Comment(1)
Yup, that's pretty much what I've discovered. Thanks.Illegal

© 2022 - 2024 — McMap. All rights reserved.