coderushforroslyn-403096-getting-started-examples-code-generation-how-to-create-a-custom-code-embedding-and-assign-a-shortcut-to-it.md
The Selection Embedding feature allows you to wrap a selected code block into a boilerplate block (for instance, try..catch ).
This example shows how to create an embedding that wraps selected text (code, comments) into the following code:
using (Graphics formGraphics = CreateGraphics())
{
// Selected text
}
Using formGraphics As Graphics = CreateGraphics()
' Selected text
End Using
Follow the steps below:
Open the Editor | All Languages | Selection Embeddings options page.
Choose the C# or Visual Basic language in the drop-down list, if it is not active:
To add a new embedding, click the “+” toolbar button.
Type the Name and Caption for the new embedding in the corresponding text boxes:
Leave the style and other options as is, then fill the code embedding in the editor, as shown in the screenshot below:
Specify the following embedding context in the corresponding text field:
Click Apply and OK to save the changes and close the Selection Embeddings options page.
In the code editor, select code you want to embed, run the Embed Selection code provider, and choose the created embedding from the list.
The screencasts below show the “CreateGraphics” selection embedding in action:
To create a shortcut for the created selection embedding:
Choose the CodeRush | Shortcuts… menu item to open the Shortcuts options page.
To add a new shortcut, choose the Selection | Embedding folder in the shortcut list and click the Add a new shortcut toolbar button.
Specify a shortcut in the “First shortcut” text box. In this example, Ctrl+G.
Choose the “SelectionEmbed” command from the list. Specify a parameter for this command that matches the embedding name. In this example, CreateGraphics.
Specify the [CodeEditorHasFocus] context in the “Context” text box. For this, press the ellipsis button in the Context field and select the context provider from the “Select Context Provider” dialog.
Click Apply and OK to apply the changes and close the Shortcuts options page.
Select code you want to embed. You can use the Ctrl+W shortcut to do it. Then, press Ctrl+G to run the “CreateGraphics” code embedding.