Mark Miller

Customer Request: Insert a Method’s Parameters into a Template Expansion

The Question

This conversation popped up on Twitter:

InsertParameters

The customer wants a list of parameters inserted into a CodeRush Template. CodeRush Templates are like Visual Studio code snippets on steroids.

My first thought: “We might have this. Let me check.”

Investigation

I opened the Template Editor options page, right-clicked the template expansion area, chose “Insert StringProvider”, and entered “param” in the Filter textbox:

SelectStringProvider

Unfortunately, nothing. StringProviders are code-based helpers located in plug-ins that deliver strings of text. I close this dialog.

CodeRush also has TextCommands, which simply execute code (any code you write) from a plug-in. Sometimes that code execution can actually insert text in the middle of a template expansion. In cases like this, a TextCommand behaves similarly to a StringProvider. So I right-clicked the template expansion area again, but this time I chose “Insert TextCommand”, and once again entering “param” into the Filter textbox:

SelectTextCommand

Unfortunately, not what we were hoping for.

So nothing we are currently shipping outputs a list of the current method’s parameters as a string. But I know we can do this pretty easily by writing our own plug-in.

My reply on Twitter goes out:

WeCanBuildAPlugIn

I return to my work, prepping for the pending release, and then it occurs to me – we can do this without a plug-in. One of the TextCommands we do ship with CodeRush is ForEach. It iterates over structural elements in your code, calling a specified template for each element found, and also optionally calling specified templates before, between, and after each element in the list.

With some clever template creation, we can get the customer exactly what they need.

CodeRush’s ForEach TextCommand

Next I bring up the CodeRush User Guide, and click the “Reference\Text Commands” node, then click the ForEach TextCommand so I can take a look at the syntax. The dynamically-generated help page (generated from the plug-in TextCommand itself) looks like this:

ForEachHelp

The ForEach TextCommand takes two required parameters (iterationBounds and the template to expand each time an element is found), and three optional parameters (firstTemplate, betweenTemplate, and lastTemplate – all names of templates to expand).

I want to create a simple comma-separated list of parameters. So I will only need to use betweenTemplate (to place a comma and a space between each parameter name found). Note that this template will only be called if there are two or more elements in the list.

Making it Work

Now things get fast and easy. Inside the template editor, I create a new template for testing, named “zzz”. This template’s expansion looks like this:


«ForEach(Parameter in Method,#AddParameter#,,#Comma#)»
 

Notes on this call:

  • Parameter in Method is the iterationBounds. That means we’re going to iterate through every parameter in the active method (the method containing the caret).
  • #AddParameter# is the name of a template I haven’t created yet. It’s going to simply insert the text of the parameter and nothing more.
  • We pass nothing in for the firstTemplate argument.
  • #Comma# is the name of a template I haven’t created yet. It’s going to simply insert a comma followed by a space.
  • As a convention, I use the hash tag symbols around templates that I call from other locations (e.g., #Comma# and #AddParameter# – but these templates could have any name you want).

 

Next I create the #AddParameter# template. It looks like this:


«?Get(itemName)»
 

This references the “Get” StringProvider, which is responsible for managing a list of variables and their values during template expansion. The ForEach TextCommand defines two variables for use in Templates – itemName (the name of the item being iterated), and itemType (that particular item’s type).

So this template is done – it’s just going to return back the name of the parameter.

Finally, I need to create the #Comma# template. This one is simple – just a comma followed by a space. I click OK to drop the CodeRush Options dialog.

Testing

I move the caret inside a method with some parameters and enter the name of my test template – “zzz”:

AboutToTest

I press the Space bar (you might press Tab if that’s your template expansion key).

I see this:

expansionSuccess

Success!

The last step is to rename our “zzz” template into something more meaningful (e.g., #ParameterList#). You can rename templates by pressing F2 when the template is selected inside the Template Editor options page.

Now whenever we need a parameter list in a template, we can right-click that template expansion, choose “Insert Alias…” (a way of calling templates from templates), and select our new #ParameterList# template to insert a list of parameters.

Going Further

Note that if we wanted to include the parameter type with each parameter, we could do that easily by modifying the #AddParameter# template to something like this:


«?Get(itemType)» «?Get(itemName)»
 

Then our template expansion might give us something like this:

 

ModifiedExpansion

Learning More about TextCommands

Here’s a video webinar Rory Becker and I recorded a while back, that introduces TextCommands and also goes into several examples of what you can do with the ForEach TextCommand:

-Mark

Published Nov 14 2013, 12:25 AM by
Bookmark and Share

Comments

Kelvin Brostek-Van Manen 1

Y'all are the greatest!

November 13, 2013 4:46 PM

Crono

Every time I learn something new about CodeRush, I realize there's still so much more to learn (and love) about it. :)

November 14, 2013 9:42 AM

Dan Arnold

You should write a book covering DxCore, CR Template development, and how to write Visual Studio Add-Ins. I think it would boost attention to CodeRush, (away from ReSharper), as a more powerful developer toolkit.

Please......

April 9, 2014 8:17 PM

About Mark Miller (DevExpress)

Mark Miller is a C# MVP with strong expertise in decoupled design, plug-in architectures, and great UI. Mark is Chief Architect of the IDE Tools division at Developer Express, and is the visionary force behind productivity tools like CodeRush and Refactor!, as well as the DXCore extensibility layer for Visual Studio. Mark is a popular speaker at conferences around the world and has been writing software for over two decades.
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2016 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners