The problem
One of the biggest annoyance when doing unit-test-friendly projects is that you have to deal with interfaces much more than you usually would. This isn’t bad by itself but it will effectively kill your F12 aka “Go To Definition”. In other words F12 won’t show you the code anymore but rather the interface definition of the method or property. Which is not what I like and I guess not what you like as well.
Let’s see an example:
When you F12 (or right click, Go To Definition) on DoTubo() you’ll get catapulted to the interface declaration of the method. Which might be what you want but mostly it isn’t. I’d like to see the Tubo.DoTubo() method declaration where the code I am interested is. Specially because often an interface is really implemented just by one class, at least in design time.
This is what I’d like to see. And this is what you’d get if you weren’t using IAnnoy interface but rather Tubo type directly.
The solution
Good news is that now there is a way to use the go to method implementation. Even better news is that it is free. As a free lunch.
I’ve created a DXCore plugin named “Go To Implementator” that does exactly that. When over a interface’s method or property reference it will give you an option to go directly to (one of the) implementation. Sounds fine?
Installation
1. If you don’t have CodeRush already installed then do install its Express version which is free or even better, go with full version (which is not free but it is well worth it).
2. Download attached zip file and unpack its content into either [USER]\Documents\DevExpress\IDE Tools\Community\PlugIns or [Program files [x86]]\DevExpress [DX version, i.e. 2009.3]\IDETools\System\CodeRush\BIN\PLUGINS.
3. Run Visual Studio. Go to DevExpress/Options… menu and select IDE/Shortcuts node the left.
4. Create a new shortcut: click on the first icon under Shortcuts title. Click on Key 1 text box on the left and press F12 (you are telling which key to respond to). Pick “Go to interface implementation” command from Commands combo box. The last step is to click on checkbox Focus/Documents/Source/Code Editor on the usage tree list on right side – a green checkmark should appear. Note that you can bind this action (or any other) to a different shortcut or even a mouse shortcut or any other way supported by CodeRush.
Take also note that there is a “Parameters” text box. I’ll use it pass parameters to my action later on in the article.
Test & use
Create a console application and past in there this code:
class Program { static void Main(string[] args) { IAnnoy annoy = new Tubo(); annoy.DoTubo(); } } interface IAnnoy { void DoTubo(); } class Tubo : IAnnoy { public void DoTubo() { } }
Position the caret on DoTubo() method reference. There are two ways to invoke my plugin.
Context menu
Right click, there should be an submenu “Go To Implementator” in context menu:
Keyboard shortcut (F12)
Just press F12. But what if you are not on a interface method/property reference? The default “Go To Definition” will be called like it would be without this plugin.
Dealing with more than one interface implementation
So far there was only one interface implementation. But what happens if there are two or more classes that implement the interface?
Let’s add another implementation to the crowd:
class AnotherTubo : IAnnoy { public void DoTubo() { } }
Now both Tubo and AnotherTubo implement the IAnnoy interface. Right click context menu should give both options in no particular sort order, like this:
Let’s pick AnotherTubo class. Plugin will remember this choice and next time it will be placed on the top:
But what about F12?
If there is no default class assigned then it should present you a smart tag with options:
However, if a default is available it would go straight to the implementation rather then presenting this smart tag.
Customizing the action
Popup menu behavior is fixed and can’t be customized in current version. The action, one that you can bind to a keyboard shortcut or whatever else input CodeRush is supporting can be customized. There are two parameters (parameters are a comma delimited string that you pass to Parameters text box when you are creating/editing shortcut in DevExpress/Options… – see the 4. step in installation) you might use.
NoPassGoToDefinition
You can disable invoking Go To Definition when action doesn’t do anything. The default behavior is to pass through when action does nothing. So why would you need this option? In case you are using the action from non F12 shortcut or if you want the action to do its job and nothing else.
ShowPopupAlways
When there is a default class for action available no smart tag is shown by default. You can override this behavior by passing ShowPopupAlways parameter. Then smart tags menu will be shown always when there is more than one class suitable regardless the default value is present or not.
Here is an example of passing both parameters:
The conclusion
I hope you’ll find this plugin useful. I am starting to use it and it saves me a lot of clicking. And thanks to DXCore it was really easy to create it.
Let me know if you have ideas how to enhance it or anything else, all feedback is welcome.