Wrapping code blocks with the CodeRush Code Embeddings feature
CodeRush Code Embeddings allow you to wrap the selected code block or any text into another predefined code block, such as: try/catch, try/finally, using and lock statements, while and other loops, region directives, etc. Code Embeddings are available via the predefined keyboard shortcuts, via the Embed Selection code editor context menu item, or using the Embed Selection code provider.
Embeddings are not context sensitive. This means that you can wrap any text into any code block, because the feature does not analyze selected code – you only add text before and/or after the selected text. However, depending on the selection type (e.g. whole line, multi-line, line fragment), different embeddings are available. So, it is up to you to decide whether to apply a specific code embedding or not.
The feature can also change the resulting editor caret position, drop markers, paste clipboard contents into the specified location and much more. This means that you have a full control over Code Embeddings, i.e. you are able to add string providers and text commands to participate in embeddings. Embeddings are customized on the same named Embeddings options page in the Options Dialog, where you can modify existing items or create custom ones.
Consider the following code:
string connectionString = Application.Properties.Settings.Default.ConnectionString; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Main", connection); SqlDataReader reader = command.ExecuteReader(); reader.Read(); Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
This code creates a new SQL connection, executes an SQL command, reads its result and prints them on the Console. Using the Code Embeddings feature we can enhance the code to the following:
string connectionString = Application.Properties.Settings.Default.ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Main", connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2)); } } } }
Which is much more appropriate for using an SQL database access APIs.
To apply an embedding use one of the following ways:
- Press the shortcut key. You can see all available shortcuts and create your own on the Shortcuts options page. To create a new shortcut, use the Embed action name.
- Select the appropriate text and right click the code editor. Then choose the Embed Selection menu item:
- Select the appropriate text and perform the Embed Selection code provider:
Here’s the list of available code embeddings for different languages.
CSharp:
Embedding | Shortcut | Description |
try..catch | C | Embeds the selection into the try{} catch{} code block. |
try..finally | F | Embeds the selection into the try{} finally{} code block. |
try..catch/finally | T | Embeds the selection into the try{} catch{} finally{} code block. |
#region.. #endregion | R, Ctrl+3 | Embeds the selection between the #region and #endregion directives. |
#if..#endif | Embeds the selection between the #if and #endif directives. | |
if () | Embeds the selection into the if(){} code block. | |
if () else | Embeds the selection into the if(){} else{} code block. | |
while() | Embeds the selection into the while(){} code block. | |
do..while() | Embeds the selection into the do{} while() code block. | |
using() | U | Embeds the selection into the using(){} code block. |
lock() | L | Embeds the selection into the lock(){} code block. |
block – {} | B, Shift+[ | Encloses the selection in {} braces. |
BeginUpdate.. EndUpdate | Embeds the selection between the BeginUpdate and EndUpdate method calls of an object from the clipboard. | |
WaitCursor | W | Embeds the selection into the try{} finally{} block, provided that the WaitCursor will be used during the selected code lines’ execution. |
Stopwatch | Creates a new instance of the System. Diagnostics. Stopwatch class, embeds the selection into the try{} finally{} block between the Stopwatch. Start() and Stopwatch. Stop() method calls, and outputs the Stopwatch. Elapsed value to the console. | |
To string | Shift+’ | Quotes the selected code section. |
Comment box | Embeds the selection into the comment box. | |
Parentheses | Shift+0 | Embeds the selection into the parentheses. |
Not Parentheses | Shift+1 | Embeds the selection into the parentheses and places the logical Not (!) operator before the parentheses. |
Brackets | [ | Embeds the selection into the brackets. |
Show Visual Basic code embeddings… »
Embedding |
Shortcut |
Description |
Try..Catch | C | Embeds the selection into the Try.. Catch.. End Try code block. |
Try..Finally | F | Embeds the selection into the Try.. Finally.. End Try code block. |
Try.. Catch/Finally | T | Embeds the selection into the Try.. Catch.. Finally.. End Try code block. |
#Region.. #End Region | R, Ctrl+3 | Embeds the selection between the #Region and #End Region directives. |
#If..#EndIf | Embeds the selection between the #If and #End If directives. | |
If..End If | Embeds the selection into the If().. End If code block. | |
If..Else End If | Embeds the selection into the If().. Else.. End If code block. | |
While.. End While | Embeds the selection into the While.. End While code block. | |
Do..While | Embeds the selection into the Do.. Loop While code block. | |
Using.. End Using | U | Embeds the selection into the Using.. End Using code block. |
SyncLock | L | Embeds the selection into the SyncLock.. End SyncLock code block. |
TryCast(..) | Embeds the selection into the TryCast(..) code block. | |
BeginUpdate.. EndUpdate | Embeds the selection between the BeginUpdate and EndUpdate method calls of an object from the clipboard. | |
WaitCursor | W | Embeds the selection into the Try.. Finally.. End Try block, provided that the WaitCursor will be used during the selected code lines’ execution. |
Namespace | Embeds the selection into the Namepsace.. End Namespace code block. | |
Stopwatch | Creates a new instance of the System. Diagnostics. Stopwatch class, embeds the selection into the Try.. Finally.. End Try block between the Stopwatch. Start() and Stopwatch.Stop() method calls, and outputs the Stopwatch. Elapsed value to the console. | |
To string | Shift+’ | Quotes the selected code section. |
Comment box | Embeds the selection into the comment box. | |
Parentheses | Shift+0 | Embeds the selection into the parentheses. |
Not Parentheses | Shift+1 | Embeds the selection into the parentheses and places the logical Not operator before the parentheses. |
Embedding |
Shortcut |
Description |
Strong | Ctrl+B | Embeds the selection into the strong tag. |
Italic | Ctrl+I | Embeds the selection into the em tag. |
Underline | Ctrl+U | Embeds the selection into the span with the underline text decoration style tag. |
Div | D, Shift+Alt+D | Embeds the selection into the div tag. |
Paragraph | P, Shift+Alt+P | Embeds the selection into the p tag. |
Anchor | Shift+Alt+A | Embeds the selection into the a tag, allowing you to enter the link id and href attributes. |
ListItem | Shift+Alt+L | Embeds the selection into the li tag. |
Table | Shift+Alt+T | Embeds the selection into the table, tr, and td tags. |