New CodeRush Plugin – CR_ImportNamespace

Problem 1: Have you ever started writing code, and then realised you were using a Type directly, without importing it’s namespace?

You could fix this by either fully qualifying the Type in place, or moving to the top of the file, adding an imports or using statement (referencing the appropriate namespace) and then finally working out where you were before you moved, and returning there.

This takes you away from your code, and context switches you away from the problem you were trying to solve. This costs you time and therefore money (and often sanity)

Note: With CodeRush, dropping a Marker [Alt+Home] before you move to the top of the file, will greatly ease the return journey [Esc].

Problem 2: Have you ever gone a stage further, and used a Type without even referencing it’s assembly?

This time it’s even worse… You have to switch your focus out of the editor entirely, and use … *gulp* … the “Add Reference” dialog.

Nooooooooooooooooooo!

The Add Reference dialog

I don’t know anyone who likes to use the ‘Add Reference’ dialog. I’ve heard it referred to as perhaps the most annoying part of visual studio. Others claim it’s the 2nd most annoying. beaten in this case to the top position, by “Accidentally hitting F1. both of these are seen in this light, because of the ridiculous amount of time that they can eat up.

The Add Reference dialog is slow and awkward and it takes you away from your code. It has several tabs that might help you in various situations, but 99% of the time these tabs are just not useful to me.

The Project Tab: Is very quick to load but is limited to the projects in your solution. This can be handy, but most of the time the references I need are not a part of my solution.

The Browse Tab: Works well also, but really it’s something of a last resort. Navigating directory structures looking for the assembly you want can be long and drawn out, and the fact that often you are returning to the same 2-3 locations makes it feel very repetitive.

The Recent Tab: Is comparable to the Projects tab. It is quick to load, but does not appear to list enough assemblies to be particularly useful. I work with a fair breadth of assemblies, which means that they cycle through the Recent Tab quite fast. I can’t honestly say that I visit the recent Tab with any real hope of finding the assembly I need.

The .Net Tab: Is the tab I find myself using more than every other, and it’s what makes me dread this dialog. The first time I choose to pick a reference from this tab in any given session, it takes around 30 seconds to fill and become responsive again. 30 seconds? Are you kidding me? Seriously?

Hmmmm … if only there was a way to avoid the Add Reference dialog……

Oh Wait..

Introducing CR_ImportNamespace

CR_ImportNamespace is designed to allow you to add namespace and assembly references without ever leaving the editor. It functions as a CodeProvider. Which is to say that the functionality is accessed through the Code menu on the CodeRush smart Tag.

Availability

The “Import Namespace…” code provider is made available when your caret is on an undeclared type.

For example:

ImportNamespaceNoCacheYet

The first time you trigger the “Import Namespace…” CodeProvider, the plugin will create a cache of all the types in the version of the .Net framework, which your current project targets. This cache will be referred to during subsequent invocations.

ImportNamespaceBuildingCache

Once this cache is built, you will be presented with a list of namespaces which contain a matching type:

ImportNamespaceAction

If only one namespace contains such a type, then this namespace will be automatically chosen after the cache is completed.

On subsequent invocations, if only a single namespace is applicable, then it will be offered as a top level menu item:

ImportNamespaceSingleChoice

If more than one namespace is suited to the task, then each of these will be presented as a submenu:

ImportNamespaceMultiChoice

However no matter how the namespaces are presented to you, the selection of one will produce the same effect.

  • - The namespace is imported.
  • - Any required Assembly is referenced.
  • … and you never move away from your existing code.
  • Finally unlike other implementations, CR_ImportNamespace is not limited to finding and referencing projects within your solution, or even to assemblies referenced by them. It covers the entire framework versions 2.0, 3.0, 3.5 and 4.0 . It understands different framework profiles (client and full) and will only offer up namespaces suited to your current project.

So where can you get this glorious new plugin? Do you have to wait for the next release of CodeRush? Do you have to wait for 2012? No, as ever, we’re committed to accelerating your productivity whenever we can. In this case… Now

Yes that’s right no. This plugin and it’s accompanying source has been generously donated to our favourite community project. You can find the relevant links and details here

  • So let’s stop messing around, manually adding namespace and assembly references. Let’s get on with the business of writing code to solve business problems, and see what develops.
15 comment(s)
Geert Depickere
Geert Depickere

This can indeed save us lots of time, time we can spend on more interesting things...

Besides the entire framework versions 2.0, 3.0, 3.5 and 4.0, does it also cover Silverlight?

21 December, 2011
richard morris
richard morris

Thanks Rory,  I hate that add refference dialog, but have been too lazy to do anything about it.

21 December, 2011
Robert Fuchs
Robert Fuchs

Does it cover DX assemblies and other 3rd party assemblies?

21 December, 2011
Rory Becker - DevExpress
Rory Becker - DevExpress

@Geert Yes the plugin will happily deal work with Silverlight 3.0 or 4.0 projects.

21 December, 2011
Rory Becker - DevExpress
Rory Becker - DevExpress

@Robert For the moment this plugin only covers framework assemblies and those linked to (or within) your current solution.

But keep an eye on this one. I imagine we'll be adding to it in the future. :)

21 December, 2011
Lester Henderson
Lester Henderson

After much fiddling - finally found out that this plug-in works with VS2008 but not VS2010.  

In VS2010 it shows up in the DX Modules as a unknown type of module.

21 December, 2011
Rory Becker - DevExpress
Rory Becker - DevExpress

@Lester This plugin should indeed work under VS2010. It is fully intended to support .Net 4.0 which as I'm sure you're aware, is not supported under VS2008

In your case are the VS2010 and VS2008 on the same machine. If not then I'd tend to suggest that the OS is blocking the dll on the VS2010 machine.

This happens when the OS detects that the dll comes from an untrusted internet source.

You can find some details on unblocking such dlls here -> www.paraesthesia.com/.../unblocking-multiple-files-at-once.aspx

If you would like further help please feel free to reply, although it might be easier to contact me via my email address roryb@devexpress.com.

21 December, 2011
Kim Major
Kim Major

Great! IMHO, this is an example of a plugin that should be incorporated into the official product in the next release. It's cool that we get early access and source, but certain plugins should just be part of the product.

21 December, 2011
Rory Becker - DevExpress
Rory Becker - DevExpress

@Kim Don't worry. It is certainly our intention to ship this with 12.1 :)

21 December, 2011
Eriq VanBibber
Eriq VanBibber

Rory,

I cannot seem to get this plugin to activate.  I see it in the list of plugins in plugin-manager, but it doesn't show up.

I try a simple Dim H as HttpApplication (knowing in advance that this assembly is not referenced) and it doesn't show up.

Any ideas?

I'm running VS2010 (and i also have vs2008 on the same box) on Win7 64bit.

Regards,

ERiq

5 April, 2012
Rory Becker - DevExpress
Rory Becker - DevExpress

@ Eriq Correct me if I'm wrong, but I believe we talked via email and a recent upgrade to 11.2 has solved this one for you?

13 April, 2012
Demetrios Seferlis
Demetrios Seferlis

Rory,

After checking out the sources an built it vs2010 is very slow with plugin loaded.

Is it gonna ship with v2012.1?

28 May, 2012
Rory Becker - DevExpress
Rory Becker - DevExpress

I do not believe this plugin shipped with 12.1

13 June, 2012
Renaud Bompuis
Renaud Bompuis

For some reason, I'm unable to make this work: I can see the pluging loaded in the list of plugins, but nothing is offered when I try to use it.

1 May, 2013
Rory Becker - DevExpress
Rory Becker - DevExpress

Hey Renauld

Could you raise an issue in @ http://www.devexpress.com/sc/

Please include as much detail as possible (VS version, CodeRush version and of course plugin version.)

Please also detail precise steps necessary to repro the problem you're encountering.

Thanks.

7 May, 2013

Please login or register to post comments.