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.
1.0.0 (18.1.2010)
Initial release
1.0.1 (19.1.2010)
Bug fix: it worked only within project scope.
1.0.2 (19.1.2010)
Too strict parsing used which might resulted in no go to implementor option at all.
1.0.3 (21.1.2010)
Didn’t work on partial classes.
1.0.4 (26.1.2010)
Fixed navigational bug (thanks to Quan Truong Anh for finding the bug and creating a repro case)
1.0.5 (16.7.2010)
Added some logging – make sure DevExpress/Options…/Diagnostics/Message Logs is turned on (if you are turning it on then you have to restart IDE).
No new functionallity besides logging – you might consider this version if plugin doesn’t work well and you want to understand why.
GotoImplementator_1_0_5.zip (10.20 kb)
17.1.2011
Important: this is the last version supporting DXCore/CodeRush 10.1.x and lower.
Even more important: I’ve created a dedicated page and all further improvements will be published throug that page. This post won’t be updated anymore.
Hi Miha,
I have tried your Add-ins, it worked quite good!
But I have found this bug:
1. If the implementor has multi method with the same name
TestMethod(string param1, int param2)
TestMethod(string param1, string param2, string param3, int param4))
It will display the message: "Method … not found".
2. But if the implementor has the following methods:
AnotherMethod(string param1)
AnotherMethod(string param1, string param2, string param3)
It works fine! 🙂
Please have a check!
Hi there,
A sample code that exhibits the behaviour would be appreciated.
Hi Miha
Just started using your plugin and I love it, thanks for putting out this goodie.
Question: Is there any way I can view all my interface implementers while my carrot is on the interface name itself. At times as I am making changes to my interface I would like view all implementers and possibly jump to any of them…would this be another shortcut I would have to setup, or a new feature?
Thanks a lot
Shmuel
Hi Shmuel,
Sorry for really late answer. Somehow I’ve missed your post (due to spam). I usually do Shift+F12 and CodeRush (or VS if there is no CodeRush) lists all references to your interface. Perhaps you wanted more refined list?
Hi,
This plugin looks awesome. What would it take to get a working version for VS2010? The "Go to interface implementation" command does not appear in the options dialog in VS2010.
Cheers,
Rohland
Hi Rohland,
It should work with 2010 as well. Did you place the plugin in C:\Users\[USERNAME]\Documents\DevExpress\IDE Tools\Community\PlugIns? What version of CodeRush are you using? What version of my plugin are you using (1.0.4 is latest).
Hi,
I installed CodeRush Express. I downloaded it yesterday so it’s pretty much the latest version. Also downloaded your plugin yesterday so pretty confident is 1.0.4. I placed the plugin in the program files directory, but after reading your comment tried placing it in the users directory as well. I restarted VS2010 and attempted to configure it again, but the option is missing. It does work with Visual Studio 2008 installed on the same machine so it seems to be a VS2010 specific issue.
Cheers,
Rohland
Hi Rohland,
Check out this link on how to display CodeRush Xpress options dialog:
http://coderjournal.com/2010/04/show-coderush-xpress-10-1-menu-in-visual-studio/
Does it solve your problem?
Excellent work, thank you so much!
Hmm, any ideas why this would stop working?
I see the command in the devexpress options, can assign it to F12, but it doesn’t do anything and I don’t get a menu on rightclick.
It was working for a while and now it stopped, so must be something I’ve did/installed. How can I debug this?
I’ve added logging to version 1.0.5. Send me the log file (DevExpress/Tool Windows/Diagnostics/Messages) when you find a problem. Also note that for logging to work you have to turn it on (DevExpress/Options…/Diagnostis/Message Logs) and restart IDE.
Hi Rob,
I’ll add logging so you’ll be able to figure it out and I’ll be hopefully able to fix it 🙂
Stay tuned.
Hmm, it suddenly started to work. Strange, but I’m not complaining. If it stops again I’ll log the problem
Great to hear that.
I have a pb (Sometimes the menu does not appear):
public interface IDaoBase<T>
{
T GetById(long id);
}
public interface IGradeDao : IDaoBase<Grade>
{
IList<Grade> GetByCriterias(string code, string libelle);
}
public class GradeDao : IGradeDao
{
}
public class GradeService
{
public Grade GetById(long id)
{
return GradeDao.GetById(id); <- I don’t get the menu on rightclick
}
public DataTable GetByCriterias(string code, string libelle)
{
IList<Grade> lsGrade = GradeDao.GetByCriterias(code, libelle); <- I get the menu on rightclick
}
}
Hi Oliver,
I’ve tried to repro your case but for me it works fine. Are you using the latest CodeRush (10.1.6)? There were some small problems with previous versions AFAIK.
Let me know,
I’m getting below exception in latest code rush
Method not found: ‘DevExpress.CodeRush.StructuralParser.LanguageElement DevExpress.CodeRush.Core.SourceModelServices.get_Active()’.
GotoImplementator
Any Idea
Hi MD,
Thanks for letting me know. Apparently DXCore/CodeRush 10.2. have a breaking change. I ‘ve updated the plugin and also created a page for it. Just follow the link at the end of this post.
don’t work in 10.2.6
Hi,
Did you try with the latest version from page http://blog.rthand.com/page/Go-To-Implementator.aspx ?
I've followed your installation instructions. But it's performing the default action.
Showing me the definition. I was kinda hoping to see the implementation.
I'll check with the latest CodeRush. BTW, did you install the latest version – you have to go to the dedicated page
http://blog.rthand.com/page/Go-To-Implementator.aspx