Skip to main content
All docs
V22.2

How to: Create a Custom Code Embedding and Assign a Shortcut to it

  • 3 minutes to read

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 
}  

Follow the steps below:

  1. Open the Editor | All Languages | Selection Embeddings options page.

    Embeddings Page

  2. Choose the C# or Visual Basic language in the drop-down list, if it is not active:

    Language drop-down-list Page

  3. To add a new embedding, click the “+” toolbar button.

    New Embedding

    The new embedding follows the existing selected embedding, as shown in the screenshot below:

    New Embedding Settings

  4. Type the Name and Caption for the new embedding in the corresponding text boxes:

    Embedding Name and Caption

    The embedding name (for example, region) is used as a parameter for the SelectionEmbed command in the Shortcuts options page, as shows in the screenshot below:

    Embedding shortcut

    CodeRush shows the embedding caption in the Code Actions menu when you select the Embed Selection code provider.

    Try-catch item visibility

  5. Leave the style and other options as is, then fill the code embedding in the editor, as shown in the screenshot below:

    Insert Embedding Code

    You can use the context menu to insert text commands and string providers in the embedding code, if required.

  6. Specify the following embedding context in the corresponding text field:

    Specify Embedding Context

    You can choose the embedding context in the “Select Context Provider” dialog. Click the ellipsis button to open this dialog.

    Embedding Context Provider

  7. Click Apply and OK to save the changes and close the Selection Embeddings options page.

  8. 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:

  1. Choose the CodeRush | Shortcuts… menu item to open the Shortcuts options page.

    Menu

  2. To add a new shortcut, choose the Selection | Embedding folder in the shortcut list and click the Add a new shortcut toolbar button.

    New Shortcut

  3. Specify a shortcut in the “First shortcut” text box. In this example, Ctrl+G.

    Click the ellipsis button to add special keys (for example, Delete, Tab) or modifiers (Ctrl, Alt, and so on) to your shortcut.

    New Shortcut

  4. Choose the “SelectionEmbed” command from the list. Specify a parameter for this command that matches the embedding name. In this example, CreateGraphics.

    SelectionEmbed command

  5. 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.

    Context

  6. Click Apply and OK to apply the changes and close the Shortcuts options page.

  7. 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.