CodeRush 17.2.6 is Available (and Includes a New Killer Feature for Visual Studio)

Another 30 day sprint, another CodeRush release for Visual Studio. And this release includes a powerful new feature we’ve been working on that we think you’ll love. Let’s get right into it:

Smart Duplicate Selection

CodeRush’s Smart Duplicate Line (SDL) gets even smarter and morphs into Smart Duplicate Selection. This feature makes creating structurally-similar code significantly easier, and orders of magnitude faster.

This feature can be used in any language you can work on in Visual Studio, however its intelligence and abilities vary depending on the content you’re building, so let’s go through a few samples to get an idea of what it can do.

First, let’s start with some C# test code that creates a new instance of a Ninja, like this:

CreateNinja

Now I might want to create more Ninjas for the unit test. Normally I would select the block of Ninja-building code, copy it to the clipboard, move the caret to the end of the block, paste the copied code, then navigate into the new code, change a part needed to create my second Ninja, and then repeat these last two steps until the new ninja code is done.

Phew! That’s a lot of work just to create a second Ninja.

Smart Duplicate Selection (SDS) starts the same way - select the code you want to duplicate. But instead of copying anything to the clipboard, just press Shift+Enter to duplicate the selection.

ninjaDup1

SDS duplicates the selection, placing text fields around the code - parts likely to change in the duplication. Don’t worry if you see a lot of text fields when you first try this. If I’m on a text field and I don’t want to change it, I press Enter to move on to the next text field. I’ll show you how to clean this up in a moment. For now, let’s focus on the changes we want to make to this duplicated block.

For example, I might change the variable name to “ninja2”, the Name to “Leonardo”, ServedInOniwaban to true, and the birth year to 1985. pressing Enter after each edit.

It’s fast and efficient.

But this feature gets even smarter when you try to duplicate again. Let’s create a third Ninja based on the changes we just made.

Now, you could select the second Ninja-building block we just edited, and press Shift+Enter again. And while that would work, it would be an excessive number of key presses just to repeat the last duplication.

Fortunately SDS gives you a super-fast way to repeat the last duplication - just press Shift+Enter again when you reach the end of the duplicated selection (or when the caret is inside any text field of the duplication).

Pressing Shift+Enter (in our ninja-building example) to repeat the last duplication, we now get something more closely reflecting what we want to modify:

ninja3

Now we have only four fields, surrounding the duplicated code we changed in the ninja2 declaration. SDS also auto-increments the variable’s name (and other numeric primitives and enums) based on the changes made to the code.

With only four fields we just change or accept what we need, pressing Enter between changes four times.

It’s pretty amazing stuff. You can create structurally similar blocks of code or script or markup or any text at all, changing only the parts you need, and create multiples of that with what some might consider the fewest keystrokes possible.

Once SDS learns what we want to change, we can repeat it by continuing to press Shift+Enter when we reach the end of the duplicated selection (or when we’re inside any TextField of the duplication).

It’s the most efficient path to creating similar repeating blocks of code/script/markup/text.

You might also be wondering, “well, what if I already know what fields I want to modify?”

This question is best answered in a second example. Let’s switch over to XAML, where we might be creating an interface that (so far) looks like this:

XamlInstallationUI1

This UI shows download progress, but we want to add similar status controls to show progress for installation, configuration, and registration (all of which happen during our install).

The XAML for the “Download: 0%” part of the UI looks like this:

We’ve got a horizontal StackPanel containing two TextBlocks, one for the label and the other, named, for the value. So, we need to duplicate this StackPanel and its children three more times. And in each duplication, we want to change only the Text of the label TextBlock and the Name of the value TextBlock.

Since we already know what fields we want to change, we can mark those using CodeRush’s multi-select feature. It’s easy. Just select the sections of code you want to modify and press Ctrl+Alt+Enter. Like this:

PreselectSDS

Notice I’ve only multi-selected one instance of “Download”, even though I want to also modify the name of the second occurrence in “tbDownload”.

That’s okay, because SDS will automatically find all camelcase matches to anything we multi-select and link those together in the duplication.

Now select the block we want to duplicate (tip - for even faster selections, use Selection Increase)…

SelectPreMultiSelectSDS

Press Shift+Enter

DulicatedXaml

And now enter the new text for the label: “Installation”.

SDS - Installation

Done.

Ready to create another StackPanel/TextBlock set of controls for the “Registration” part of the spec? We’re already inside a text field, so we can press Shift+Enter to repeat the last duplication:

SDSXamlThird

Enter the label text: “Registration” and press Shift+Enter again, then enter “Configuration” and press Enter because we’re done.

Our interface now looks like this:

UI - finished SDS

And it includes four properly-named TextBlocks (tbDownload, tbInstallation, tbRegistration, and tbConfiguration) that we can easily access from code-behind.

You can also use SDS to duplicate CSS styles, HTML controls, parameters in method declarations, binary expressions, arguments, string constants and entire method blocks.

SDS will also remember changes you make in each file and suggest those same changes in similar duplications you perform in the same file.

You can also now dismiss all text fields by pressing the Esc key. This is especially useful for Smart Duplicate Selection because it may generate many text fields on the first duplication, and you can press Escape before or after you’ve modified the fields you need to change.

That’s the killer feature in this release, and we hope you find it as useful as we do. Let us know what you think and how we can make it even better.

Code Formatting

Earlier versions of the code wrap option would move the entire expression to the next line if any part of it exceeded the margin. The new Simple Wrap formatting option allows you to wrap only those parts of the expression that exceed the margin, performing a non-greedy wrapping and in some cases reducing the number of lines needed to see the code.

SimpleWrap

Unit Testing

  • You can now run unit tests under SciTech Software’s .NET Memory Profiler. If this third-party product is installed, you can use the Run Under Memory Profiler option to run tests designed to identify memory leaks and other memory usage issues. Note: support for the CodeRush Test Runner is included with the .NET Memory Profiler versions 5.6 and up.
  • CodeRush Test Runner now supports running tests created for the F# language.

Code Analysis

CodeRush Analyzers now appear in the Code Issues Catalog. You can use this options page for the following tasks:

  • Enable/disable specific Code Issues.
  • Add/remove Code Issues to/from Visual Studio’s background analysis.
  • Specify the severity of each Code Issue.

CodeIssuesCatalog

Region Creation

CodeRush Classic’s Region Creation feature is now available in CodeRush. Use the Ctrl+3 shortcut to create a region around the current member or selection instantly.

RegionCreation

C# 7 Syntax Support

We are supporting С# 7 syntax in CodeRush features that generate exception-throwing statements (like Throw Exceptions Contract).

Now these CodeRush features generate the specified throw-expression operation if the corresponding preference is set in the Visual Studio settings.

ThrowExpression

Give CodeRush a Try!

Download CodeRush from the Visual Studio Marketplace.

Every 30-day sprint we work hard to make CodeRush the best it can be. Let us know how we’re doing, and what we can do to make CodeRush even better for the way you develop.

Jim Foye

Very cool. The example you gave in XAML is a common scenario when doing forms, copying and pasting lots TextBlock/TextBox pairs.

Also adding support to Test Runner now for F# is great. Currently I have to write a separate script to call those tests.

Keep up the good work.

14 February, 2018
Mark Miller (DevExpress)

Thanks James! As always, let us know how we can make CodeRush even faster/smarter and more useful for the work you do.

15 February, 2018
Thomas ONeil

The smart duplicate code selection is great.  Often during development of a project I will create a list of classes to demonstrate the UI.  I just tested this out and it works great!

Many thanks this will definitely be a time saver.

Tom

20 February, 2018

Please login or register to post comments.