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

3 comment(s)
Miles

If I'm understanding what you are trying to accomplish, VB.Net already has this capability. CTRL+Down and CTRL+Up

8 December, 2010
Rory Becker - DevExpress

Interesting. I checked the shortcuts you mentioned in Visual Studio's Tools\Options\Keyboard screen and they appear to be linked to Scroll up and Scroll down.

Perhaps you have a different configuration. Or some other plugin providing this functionality?

Looking in my latest CodeRush configuration, I see some disabled shortcuts (Ctrl+Up, Ctrl+down) which are tied to NavNextSibling and NavPrevSibling. These are not strictly the same as what I have built, but may be what you are referring to.

In any case, this post is only really supposed to be an example of how you can add navigation functionality to VS. The fact that there may be similar functionality already is not something that overly worries me.

Indeed it was faster for me to create this plugin, than it was to discover that C# had this functionality already. Proving the point about simplicity :)

8 December, 2010
Steve Sharkey

An excellent example of how to build an add in. However, not having attempted anything like this before, I would like to see the bit between coming up with the code as posted and actually building, distributing and installing the add in.

9 December, 2010

Please login or register to post comments.