DCSIMG
CodeRush Plugin – Navigate to Implementation Part 4 - .NET Geek

.NET Geek

"It is upon the Trunk that a gentleman works" - Confucius

CodeRush Plugin – Navigate to Implementation Part 4

This is the last post in series on developing the navigate to an implementing method plugin. Here are links to the previous posts, part 1, part 2 and part 3.

Just to recap, the goal of the plugin is to be able to position the caret (what most people call the cursor), on a method call on an interface variable and navigate to specific implementations of the method.

image

In this post we’ll look at how we navigate to the correct method.

The call to navigate is made when the navigation provider (part of CodeRush) receives the navigation event.

private void navigationProvider1_Navigate(object sender, DevExpress.CodeRush.Library.NavigationEventArgs ea)

{

    SubMenuItem selectedMenu = ea.SelectedSubMenuItem;

    if (selectedMenu == null)

        return;

    Class implementor = _implementors.Where(c => c.FullName == selectedMenu.Name).FirstOrDefault();

    if (implementor != null)

    {

        Navigator navigator = new Navigator(implementor, ea.Element);

        navigator.Navigate();

    }

}

In the code above we request the implementing class from our implementor finder and if one is found we instantiate a new Navigator and navigate. Here’s the navigation code.

public class Navigator

{

    /// <summary>

    /// The source code element that the navigation was initiated on.

    /// </summary>

    private LanguageElement _elementActivated;

    /// <summary>

    /// The class that contains the implementation of the invocation of _elementActivated.

    /// </summary>

    private Class _targetClass;

 

    public Navigator(Class targetClass, LanguageElement element)

    {

        _elementActivated = element;

        _targetClass = targetClass;

    }

    public void Navigate()

    {

        if (_elementActivated == null || _targetClass == null)

            return;

        Method activatedMethod = _elementActivated.GetDeclaration() as Method;

        if (activatedMethod == null)

            return;

        Method matchingMethodInClass = FindMatchingMethodInClass(activatedMethod);

        if (matchingMethodInClass == null)

            return;

        CodeRush.File.Activate(matchingMethodInClass.FileNode.FilePath);

        CodeRush.Documents.ActiveTextView.Selection.Set(matchingMethodInClass.NameRange);

    }

    private Method FindMatchingMethodInClass(Method activatedMethod)

    {

        ISourceTreeResolver resolver = new SourceTreeResolver();

        Method matchingMethod = null;

        foreach (IElement node in _targetClass.Nodes)

        {

            if (node is Method)

            {

                if (node.Name != activatedMethod.Name)

                    continue;

                if (SignatureHelper.SignaturesMatch(resolver, activatedMethod, node as Method))

                {

                    matchingMethod = node as Method;

                }

            }

        }

        return matchingMethod;

    }

}

The _elementActivated field is the method call. See Drive() in the screenshot above. I’m not going to walk through the code explaining every detail. The reason for this is that I think the code is not hard to read. The main difficulty in developing CodeRush plugins is to write code that you don’t have any samples for. So hopefully by putting this plugin I’ve added another reference point for anyone who is interested in taking advantage of the extremely powerful plugin model of CodeRush.

The code is available on Google code here http://dxcorecommunityplugins.googlecode.com/svn/trunk/CR_NavigationContrib - dxcorecommunityplugins. Check out the source using a Subversion client. For more instructions on downloading the code you can go to the home page of the community plugins.

If you prefer a zip file with code and binaries, just send me an email through the blog.

Binaries can be downloaded from here. (Linked against CodeRush 3.2.2)

Comments

Mark Miller said:

Hey Kim,

Suggestion: Change the DisplayName property of your NavigationProvider to "Method Implementer". The "Jump to" title is already at the top of the menu, so you don't need the words "Navigate to" in te DisplayName.

# December 6, 2008 12:59 PM

Claudio Lassala said:

I'd really like to get the binaries so I can just start using this plugin right away.  :)

# December 11, 2008 10:54 AM

Kjell-Åke Andersson said:

It would also be great if you would drop a marker at the method call so you can navigate back using the standard navigation model.

Brilliant plugin! I like this implementation more than Mark Miller's as it does not bring up a new window where you navigate to the implementation.

# December 16, 2008 5:11 AM

Kim said:

Kjell Ake: Er du norsk eller svensk?

# December 16, 2008 3:44 PM

CodeRush Plugin ??? Navigate to Implementation Part 3 - .NET Geek said:

Pingback from  CodeRush Plugin ??? Navigate to Implementation Part 3 - .NET Geek

# November 12, 2009 4:39 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: