Rory Becker - DevExpress CodeRush Blog

November 2014 - Posts

  • Extending Selection Embedding with CodeRush

    In my last post, I showed you around some of the Selection Embeddings that ship with CodeRush. 

    Like many CodeRush features, Selection Embeddings are user extensible. In this post I’m going to show you around the options for Selection Embedding and how you can make your own custom Selection Embeddings.

    As an Example:

    Let’s imagine you have some text in a HTML file that looks like this…

    SelectionEmbedding5Items

    … and that you’d like to have it look like this

    SelectionEmbedding5ItemsAfter

    That’s the Selection Embedding we’re going to create.

    Options

    In order to create or alter a Selection Embedding, you need to access the corresponding options page.

    • Press Ctrl+Shift+Alt+O and the Options screen will appear.
    • Select Editor \ Selections \ Embedding from the tree of pages on the left.

    You will be presented with the Selection Embedding configuration page.

    Ensure you have the correct language settings for the file you want to target. This dropdown is at the bottom of the page.

    Since this time we’re going to create a HTML Selection Embedding, we’ll select HTML from the language drop down.

    SelectionEmbeddingLanguageSelection

    Next press the green plus symbol at the top left of the page. You’ll be presented with a template for a new Selection Embedding.

    SelectionEmbeddingNewEmbedding_thumb[2]

    Let’s walk through the various parts of configuring your new selection embedding available on this page.

    Naming

    The Caption field represents the text value which you will see on the Code \ Embed Selection submenu.

    The Name field is used if you wish to bind the embedding to a shortcut later on.

    SelectionEmbeddingOptionsNameCaption

    Set the Name and Caption of our embedding to ‘Unordered List ‘

    Style offers 5 alternate styles of selection embedding.

    SelectionEmbeddingOptionsStyles

    With Style 1 you are able to enter text for the Top, Bottom, Left and Right of your selection.

    • Text entered for left and right will be repeated for each line of your selection.
    • Text entered for Top and Bottom, will be added above and below your selection.

    SelectionEmbeddingOptionsStyle1

    In Style 2 you are able to enter text for the Top and Bottom of your selection.

    • Text entered for Top and Bottom, will be added above and below your selection .

    SelectionEmbeddingOptionsStyle2

    In Style 3 you are able to enter text for the Left and Right of your selection.

    • Text entered for Left and Right will be repeated for each line of your selection.

    SelectionEmbeddingOptionsStyle3

    In Style 4 things change a little:

    • The Top Text is moved to the Left of the Top line of your selection replacing the Left text from Style 3 and Style 1.
    • The Bottom text is moved to the Right of the Bottom line of your selection replacing the Right text from Style 3 and Style 1.
    • Note how text which will appear in the Bottom Left and Top Right sections, is copied in the screenshot from the appropriate Middle Line text.

    SelectionEmbeddingOptionsStyle4

    Style 5

    In Style 5

    SelectionEmbeddingOptionsStyle5

    Set the Style of our Selection Embedding to ‘Style 1’

    Text Options

    If you select a style with Left and Right Text (any style except Style 2) then the following set of options will be enabled

    SelectionEmbeddingOptionsLeftRight

    These options specify where you would like the Left and Right text to be placed relative to the selection they wrap. Experiment with these and see what a difference they can make to your Embeddings.

    In our case choose ‘Start of code’ and ‘End of code’ respectively.

    The last options allow you to enter TextCommands and StringProviders into your Embedding to produce numerous effects when the expansion happens.

    SelectionEmbeddingTextCommands

    We don’t need these options for our selection embedding, but it’s worth knowing they’re here.

    The last part of the configuration is the most important. You need to specify the various pieces of text that will surround your selection.

    You’re fully configured Selection Embedding, should look something like this:

    SelectionEmbeddingUnorderedListWithHighlights

    Click Ok, and you’re done.

    Once you have completed the previous steps, all you have to do is

    • Select your text
      SelectionEmbeddingSelectText
    • Hit your CodeRush\Refactor key and Select Unordered List from the Embedding menu.
      SelectionEmbeddingSelectOrderedList

    and CodeRush does the rest.

    As I’ve stated many times before. One of the best features of CodeRush is it’s extensibility. If the features don’t ship out of the box just the way you like them, you can adjust, configure and augment to your hearts content.

    Feel free to share any custom Selection Embedding ideas in the comments below.

  • Selection Embedding with CodeRush

    Selection Embedding is one of those CodeRush features that doesn’t get a lot of mention, but it is extremely powerful.

    It provides the user with the ability to wrap some text (code, mark-up, comments) around some existing code already in place.

    Selection Embedding is available from your the Code section of your CodeRush SmartTag menu (As well as via several custom shortcuts)

    Each Selection Embedding is context sensitive, so that only those for the current document type are shown to you.

    Below is an example of the selection embeddings available from within the C# editor.

    SelectionEmbeddingCSharp

    These are produced when I highlight some text in the HTML editor.

    cSelectionEmbeddingHTML

    Selection Embeddings do not have to be picked from the SmartTag menu. As mentioned, some have associated shortcut keys.

    Try highlighting 1 or more lines of C# code and then …

    …pressing T causes your code will be wrapped in a try..catch..finally block.
    …pressing C causes your code will be wrapped in a try..catch block.
    …pressing F causes your code will be wrapped in a try..finally block.
    …pressing B or { causes your code will be wrapped in a brace (ie { and } ) block.
    …pressing ( or ) causes your code will be wrapped in parenthesis.
    …pressing R causes your code will be wrapped in a #region…#endregion block.
    …pressing U causes your code will be wrapped in a using block.
    …pressing W causes your code will be wrapped in a waitcursor block.

    Some of these are pretty simple wrapping effects. Others put CodeRush fields and text commands in place to enhance your experience further.

    Here the using embedding places your caret within the using expression so you can more easily fill it out.
    Additionally, when you press enter, your caret is moved to the bottom of the expression ready for further code.

    SelectionEmbeddingUsing

    Here CodeRush provides a specialized try..finally block…

    SelectionEmbeddingWaitCursor

    • Saves your current cursor
    • Sets your cursor to the WaitCursor.
    • Executes your original code.
    • Restores your cursor to it’s original state, regardless of the success of your code.

    This is all boiler-plate code, but it’s boiler-plate code you don’t have to write yourself. This reduces the chance of typos or, worse still, logic errors.

    As you can see, Selection Embedding can be a very powerful tool, providing you with boiler-plate code in a fraction of the time, with fewer keystrokes and reducing the chance for typewritten errors across the board.

    As with most CodeRush features, Selection Embedding is user extensible.

    In my next post, I’ll show you how to make your own Selection Embeddings.

LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners