Rory Becker - DevExpress CodeRush Blog

December 2010 - Posts

  • CodeRush Plugin – TabNav

    Today I was navigating some legacy VB.Net code, and found myself wishing that I could quickly navigate from the top of a method to the bottom or vice-versa. Naturally my first thought was to write a plugin to add this capability.

    Seconds later, without knowing quite how I got there (Now that’s real muscle memory) I was starring at the default code of a new CodeRush plugin in VS.

    Note: Yes I realise that C# already has this functionality (Ctrl+]), but I wanted it in VB.Net as well.

    And so now I’ll show you how easy it was to build this exact plugin.

    Let’s build it

    This plugin is another example of a Simple Action Plugin and assumes that you already know how to Create a Plugin Project and add an Action

    The specifics of this plugin require 3 steps:

    • Set the Action’s ActionName property to “TabNav”.
    • Handle the CheckAvailability event.
    • Handle Execute event.

    Since the first step is only setting a property, we’ll concentrate on the CheckAvailability and Execute events.

    The CheckAvailability Event

    The purpose of this event, is to give us a place to decide if our action should be available, given the state of the code and our position within it.

    In our case, we require have 3 requirements:

    • The caret must be positioned on an element which has a block construct.
    • The caret must not be positioned on the name of the element (if any).
    • The caret must not be positioned on the type of the element (if any).

    To explain a little….

    • We require a block construct, because otherwise our action would be allowed to fire for primitives and statements etc. To navigate from start to end has no real meaning in these cases.
    • We explicitly do not allow our action to fire if we’re on the Name or Type of an element, because we don’t want to clash with the already wonderful TabToNextReference

    If all of these requirements are met, then we set ea.Available to true.

    TabNavCheckAvailability

    The CheckAvailability event is fired whenever the DXCore wishes to determine if the Action should be available. Because of this, our Execute event can rely on the fact that these prerequisites have been met, and get on with it’s main function with no need to double check them.

    The Execute Event

    This event is raised when the user elects to trigger the action. As noted before, the CheckAvailability event will have already been raised, and by virtue of having reached the execute event, we can assume that all requirements were met.

    Our plugin basically requires that we jump from one line of code, to another.

    We determine which line we’re on and instruct the DXCore to move the caret to the other. As an added bonus we jump to the first non-space character of that line.

    TabNavExecute

    There are 3 other methods in the code for this plugin. The PosOfFirstNonSpaceChar function is hardly worth mentioning. It’s a simple iteration that determines the (1-based) index of the first non-space character on a given line.

    The other 2 functions (shown below) determine if the caret is positioned on either the Name or Type of an element.

    TabNavUtilities

    Using TabNav

    This plugin will compile and run just fine as it is, but there is one final step you must take before you can see it in action.

    The TabNav action must be bound to a key. As you might have gathered, I recommend you bind the TabNav command to the {Tab} key

    At this point you should be able to Tab from Class –> End Class or back again. Additionally the same functionality should work for Namespaces, Methods, If statements, Do While loops and many others.

    Compatibility

    You might have noticed that nothing in the code so far, specifies any sort of language compatibility. This is because we’re working with abstract language constructs. So by default, this plugin will work in any imperative language supported by the DXCore.

    As previously noted however, this functionality is already supported by the VS Team for brace-based languages. The Ctrl+] keystroke will jump you between a function’s opening and closing brace. If you wish to rebind this, the command name is “Edit.GotoBrace”.

    Personally I think my use of {Tab} is much more intuitive, especially considering the beauty that is CodeRush’s TabToNextReference

    Whilst developing this plugin I learned that there are a few edge cases for the C# language that simply don’t apply for VB.Net. Chief among these, is the fact that the block start line is not always the same line as the Element start line. There are simple ways around this but since C# doesn’t need this feature, I elected to disable it in languages other than VB.Net.  In order to do that you can use this additional code in the CheckAvailability event:

    TabNavCheckAvailabilityAdditional

    In any case the plugin and it’s source code is available from the community plugin site

    So download, install and enjoy your new found freedom Smile

  • Merging CodeRush Templates (via Export and Import)

    When a new version of CodeRush is released, it’s quite often the case that there are new templates included with it.

    So why when you’ve completed the install, are these templates not always immediately available?

    Well put simply, it’s because we don’t want to damage or break any Template customizations that you might have created yourself.

    As you are no doubt aware by now, CodeRush is one of the most configurable products on the market. In this spirit, we allow you to add your own Templates or even customise the ones we offer you out of the box. If we were to overwrite these changes each time we installed, we’d upset people pretty quickly.

    Since we don’t like to upset people, we choose instead to not overwrite your templates with our own stuff unless you ask us to.

    This post is designed to show you how to merge your own custom Templates, with any new ones we might be shipping in a new release.

    Step 0 – Always work within a ‘Custom’ folder

    Yes there’s a step zero. This isn’t so much a step, rather more of a mind-set for when you’re creating templates of your own. We don’t force you to create templates in any particular way, but we do suggest that when you create new templates, that you do so inside of a new top level folder called ‘Custom’. By placing your Templates in this folder you are creating a single parent for all of your templates which will greatly ease the management of them in situations like merging.

    Step 1 - Export your custom TemplatesTemplateContextMenu

    • Open up the Templates options page (Ctrl+Shift+Alt+OEditor\Templates)
    • Right-click the Custom folder (The one you always put your custom templates in) and choose Export Folder. Save the resultant file somewhere safe.
    • Repeat for each language where you have created Templates of your own.

    Step 2  - Force CodeRush to rebuild your templates from defaults

    • Choose DevExpress\About
    • Click Settings
    • Dig into the Editor\Templates folder.
    • Delete (or move if you’re feeling more cautious) the files in this folder.
    • Switch back to VS
    • Return to the Templates options page again
      • Note: All of the templates will have been rebuilt from (new) defaults (without your custom templates)

    Step 3 – Reimport your Custom Templates

    • Select in turn each language for which you exported a file in step 1.
      • Right-click the template tree and pick Import Templates
      • Select the previously exported file which pertains to your currently selected language.
      • Click Ok and note how the Custom folder returns to the template tree.
    • Repeat for each language

    You have now merged the new templates with your own customizations, and are done.

    You should be able to see your own custom templates, and any new ones shipped with the most recent version of CodeRush.

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