Home > Coding Helpers > Intelligent code lines enhancement and modification using the Duplicate Line feature

Intelligent code lines enhancement and modification using the Duplicate Line feature

April 19th, 2011

The Duplicate Line CodeRush feature intelligently creates a copy of an existing line of code, allowing you to easily modify it. If the code line is recognized as a predefined pattern, the line is copied and a portion of it is selected for further modification. Note that some code line bits can be modified automatically for you as well.

The predefined duplication patterns are configurable. They are stored and presented as regular expressions that contain one or several DXCore built-in reg-ex aliases for easy understanding. If the line doesn’t match any of the existing patterns – nothing happens, by default. However, there an option to copy-paste the entire line of the text if no matches are found when the feature is performed. All available options are listed later.

To perform the Duplicate Line, press the Shift+Enter key anywhere on the line:

CodeRush Duplicate Line preview

You can duplicate variable declarations, constants, events, single-line interface members, using statements, method calls, and other code pieces. Here’s the list of predefined duplication patterns shipped with CodeRush:

Pattern Name Description Sample code
Local Variable Duplicates local variables, allowing you to change its name. C#: string varName;
VB: Dim varName As String;
Field Duplicates field declarations. C#: private string fieldName;
VB: Private fieldName As String
Event Duplicates event declarations. C#: public event EventHandler MyEvent;
VB: Public Event MyEvent As EventHandler
Property
(auto-implemented)
Duplicates single-line auto-implemented property declarations. C#: public string PropertyName {get; set;}
VB: Public Property PropertyName As String
Using/Imports Duplicates using (Imports) statements. C#: using System.Drawing;
VB: imports System.Drawing
Initialized Field Duplicates an initialized field declaration (bringing the initialization to the new line). C#: private bool fieldName = true;
VB: Private fieldName As Boolean = True
Initialized
Integer
Field
Duplicates an initialized field of type integer. The initialization value is incremented by one against the source line value. C#: private int fieldName = 1;
VB: Private fieldName As Integer = 1
Method (Interface) Duplicates interface method declarations. C#: void MethodName();
VB: Sub MethodName()
Property (Interface) Duplicates interface property declarations. C#: String PropertyName { get {} set {} }
VB: Property PropertyName() As String
Assignment
Statement
Duplicates an assignment statement, selecting the left side of the statement. C#: Identifier = value;
VB: Identifier = value
if (aa is bb) Matches if-expressions checking to see if object (O) is of type T. Declares a new local variable of type T, initialized to O (casts it to T type). C#: if (myObject is SomeType)
VB: If (myObject Is SomeType)
Flagged Enum
Element
Duplicates an initialized element of an enumeration, doubling the initialization value on the next line. C#: Identifier = 1,
VB: Identifier = 1

Because Duplicate Line is based on the regular expression engine (similar to the Intelligent Paste feature), you can use it in all CodeRush supported languages. All duplication patterns are configurable on the Editor | Auto Complete | Duplicate Line options page in the Options Dialog:

CodeRush Duplicate Line options page

Here you are able to change any existing patterns, or create your own, using the appropriate buttons. On the left, you can observe the list of duplication patterns and change their order. The order is important, because patterns are evaluated in the order that they appear in the list. You can move items of the list with the arrow buttons provided or via the right-clicking context menu. More specific duplications should be listed before more general duplications.

To create a new duplication pattern, click the New button or press the Insert key. You can also create a new pattern based on an existing one, by right-clicking on the pattern from the list, and choosing the “Duplicate” menu item in the context menu. Note that it is possible to create patterns that match “subsets” of other patterns. For example, a field that is initialized to an integer (“Initialized Integer Field“) is a subset of “Initialized Field” pattern. To remove the pattern, click the Delete button or press the Ctrl+Delete shortcut.

On the right side of the options page you can modify options for a particular duplication pattern, selected on the left side. Available options are:

Option Name Description
Name The name of the duplication pattern. Appears in the Feature UI hint.
Match With The regular expression pattern of the code line where this pattern is available.
New Line The regular expression pattern of the resulting duplicated code line.
Strip white space before match Specifies whether the preceding whitespace in the source line is ignored.
Strip comments before match Specifies whether the preceding comments in the source line are ignored.
Treat single spaces as optional white space If this option is on, Duplicate Line treats single spaces as an optional whitespace from the Match With and New Line values. This allows you to enter these values easily, which makes them more flexible in regard to whitespaces (tabs, spaces, several spaces in a row, etc).
Use The context for the duplication pattern where it is available.
Comment The comment for the duplication pattern.

Two additional options are available on another options page: Editor | Auto Complete | Duplicate Line Setup:

CodeRush Duplicate Line Setup options page

  • Copy a source code line if no Duplicate Line match occurs

The options specifies whether the entire line of text should be copy-pasted, if no duplication patterns match the current line.

  • Duplicate a previous source code line if the current one is empty

The option specifies whether you are able to perform the Duplicate Line feature on an empty line, followed by the line you were going to duplicate. This is useful when you moved the editor caret to the next line (e.g. by pressing the Enter key at the end of line) and realized that you can actually duplicate the previous line with manual typing.

As always, any shortcuts like Shift+Enter for the Duplicate Line are configurable on the IDE | Shortcuts options page in the IDE Tools Options Dialog.

Note that the Duplicate Line feature is also available in the free CodeRush Xpress version.

—–
Products: CodeRush Pro and CodeRush Xpress
Versions: 10.2 and up
VS IDEs: any
Updated: Aug/30/2011
ID: C089

Similar Posts: