Posts Tagged ‘ReSharper 5.0’

ReSharper 5.0 Preview: Call Tracking

Wednesday, March 17th, 2010

There’s a cool new feature in ReSharper 5.0 called Call Tracking (or, alternatively, Call Hierarchy). Basically, it’s a convenient way to perform an all-out Find Usages or Go To Declaration. You can access it by choosing ReSharper | Inspect | Outgoing Calls or ReSharper | Inspect | Incoming Calls.

There’s also Inspect This, a new shortcut to Call Tracking, Value Tracking, and Type Hierarchy features: Ctrl+Shift+Alt+A.

At first we thought of comparing our Call Tracking to Call Hierarchy in Visual Studio 2010. However, it turned out that the VS 2010 version is just not up to par: it doesn’t support events, interfaces, closures and a few other things. It offers no help at all with the use cases we present here. So we’re only going to talk about Call Tracking in ReSharper 5.0.

Events

Let’s search for an outgoing call from Foo (ReSharper | Inspect | Outgoing Calls):


   using System;
   public class C2
   {
     public event EventHandler E = (sender, args) => Subscription_In_Initializer();

     static void Subscription_In_Initializer()
     {
     }

     void Foo()
     {
       E(this, EventArgs.Empty);
     }
   }

   class C3
   {
     void Bar()
     {
       new C2().E += Subscription_In_Method;
     }

     void Subscription_In_Method(object sender, EventArgs e)
     {
     }
   }

Outgoing Call From Foo

Pretty self-explanatory. ReSharper easily finds all subscriptions to E and displays them as possible calls. Nothing too special, but handy for sure.

Generics

Consider this code sample:


   public abstract class Base<T>
   {
     public void Do(T value)
     {
       DoImplementation(value);
     }

     protected abstract void DoImplementation(T value);
   }

   public class Concrete1 : Base<int>
   {
     protected override void DoImplementation(int value)
     {
     }
   }

   public class Concrete2 : Base<string>
   {
     protected override void DoImplementation(string value)
     {
     }
   }

Now let’s look at outgoing calls from Base.Foo:

Outgoing Calls From Do

Speaks for itself.

Now, let’s add a Main class and try searching for outgoing calls from Foo:


   class Main
   {
     void Foo()
     {
       Concrete2 c = null; // null so that we don't clutter the call tree
       c.Do("string");
     }
   }

Outgoing Calls From Foo

Concrete1.DoImplementation doesn’t show up anymore! That’s because ReSharper looked at the type parameters and realized that Base.Do will be called with T->string (see the second line of results: Base<string>.Dostring means we’re calling a method that substitutes a specific type). Accordingly, ReSharper filtered out Concrete1 because it uses inheritance with the substitution T->int.

Now let’s look at a slightly more complex, yet very vital example using the Visitor pattern. Let’s search for incoming calls from ConcreteVisitor1.VisitNode1 (ReSharper | Inspect | Incoming Calls). Note how we’re going the other way here, in the direction opposite of method calls:


   public interface IVisitor<T>
   {
     void VisitNode1(T data);
   }

   class Node1
   {
     public void Accept<T>(IVisitor<T> v, T data)
     {
       v.VisitNode1(data);
     }
   }

   public class ConcreteVisitor1 : IVisitor<int>
   {
     public void VisitNode1(int data)
     {
     }
   }

   public class ConcreteVisitor2 : IVisitor<string>
   {
     public void VisitNode1(string data)
     {
     }
   }

   public class C1
   {
     void Foo()
     {
       var v = new ConcreteVisitor1();
       new Node1().Accept(v, 1);
     }

     void Foo2()
     {
       var v = new ConcreteVisitor2();
       new Node1().Accept(v, "string");
     }
   }

The result:

Incoming Calls to VisitNode

While traversing the generic Visitor, ReSharper did not lose any details about the substituted type parameters and successfully filtered out the irrelevant call from Foo2. When you have a highly-branched hierarchy and a large number of generic types, this kind of logic helps to really narrow down your search.

Constructors

Let’s also look at an artificial example using constructors and field initializers. Let’s search for outgoing calls from the Derived class constructor:


   class Base
   {
     public Base()
     {
       Base_Bar();
     }

     void Base_Bar()
     {
     }
   }

   class Derived : Base
   {
     int _i = Foo();

     public Derived()
     {
       Bar();
     }

     void Bar()
     {
     }

     static int Foo()
     {
       return 0;
     }
   }

Outgoing Calls From Derived

Again, nothing out of the ordinary. ReSharper simply displays calls in their the natural order, mindfully listing the implicit call of the base constructor. For a less-experienced developer, this saves a lot of time spent understanding code, and is a nice crutch to lean on for an expert.

Value Tracking

And here’s the final tidbit. If you open the ReSharper | Inspect menu, you’ll see two very interesting items: Value Origin and Value Destination. These functions implement value tracking: they let you track where a particular variable value or parameter value came from, or where it is headed. Naturally, it works with collections and delegates (it determines that an item was taken from a collection and then searches for usages of that particular collection) and is indispensable for identifying the causes of NullReferenceExceptions.

Illustrating this will take a whole batch of screenshots and examples, so please stay tuned for our next post.

Author: Alexander Zverev, senior ReSharper developer. Translated from original article (in Russian)

ReSharper 5 Beta 2 Released

Wednesday, February 17th, 2010

In an irrepressible pursuit of deadly perfection, we’re releasing ReSharper Beta 2 today! Committed to sim-ship ReSharper 5.0 with Visual Studio 2010, we’ve fixed a ton of issues, from minors to show-stoppers. Here are some of the most conspicuous:

  • Support for Visual Studio 2010 RC. This inevitably means that R# 5 Beta 2 is not compatible with Visual Studio 2010 Beta 2. If for any reason you need ReSharper to be compatible with VS 2010 Beta 2, download build 1611 or earlier from ReSharper Nightly Builds.
  • Extended ASP.NET MVC functionality. ReSharper 5.0 Beta 2 supports ASP.NET MVC 2 Areas — like so:

    Other notable improvements include creating MVC View pages and user controls with or without master pages right from usage.
  • Stable Structural Search and Replace. SSR is now actually working working in a much more stable fashion than late last year, when we released Beta 1. That means, if you know about a code smell that ReSharper is unaware of, no problem! Create a search/replace pattern:

    and execute it! You can also set a severity level for the pattern so that ReSharper highlights code matching the search pattern as a hint, suggestion, warning, or error, and provides quick-fixes:

    By the way, you can now share your patterns with fellow developers by importing/exporting them as XML files.
  • Multiple usability improvements. Usability improvements in ReSharper 5.0 Beta 2 affect many features, including the aforementioned Structural Search and Replace; Localization feature pack (by the way, have you seen Hadi Hariri’s Localizing Your Applications with ReSharper 5 screencast? you should!) and Solution-Wide Warnings and Suggestions that you can now launch not only from the Solution Explorer but from ReSharper menu as well:

Download ReSharper Beta 2 now and remember that all new and upgrade licenses purchased since October 15, 2009 qualify for a free upgrade to ReSharper 5.0!

If you haven’t yet upgraded to ReSharper 4.5, right now could be the time to do so - and enjoy the new ReSharper 5.0 for free when it comes out later this spring.

ReSharper 5.0 Preview: Solution-Wide Warnings and Suggestions

Wednesday, January 27th, 2010

Over the years, ReSharper has gradually advanced from plainly highlighting errors and problems in individual code files to somehow summing up errors and problems in a larger scope, up to the entire solution.

Back in 2007, ReSharper 3.1 was the first product version to introduce Solution-Wide Error Analysis to gather data on all errors in the solution using a single tool window. ReSharper 4.5 split many code inspections to execute in private and public scope, with the latter only available when you turned on Solution-Wide Error Analysis.

Taking another huge step towards instantly seeing every problem anywhere in your solution, ReSharper 5 introduces a new code analysis feature called Solution-Wide Warnings and Suggestions. As you’ve probably guessed, it lists all warnings and suggestions that ReSharper displays in the scope of your whole solution or in a narrower scope, depending on how you call it. In other words, any code smells or other problems that you may have in your solution can now be summarized in a single tool window, making tool-based code review much easier.

When you switch on Solution-Wide Errors Analysis, Solution-Wide Warnings and Suggestions starts showing errors as well, plus warnings and suggestions that work in non-private scope.

That doesn’t mean Errors in Solution tool window becomes obsolete with R# 5: when your goal is as simple as to make your code compile, Errors in Solution quickly indicates that something in the solution went awfully wrong and you should fix things up before you can compile.

However, as soon as you’re ambitious enough to make your code green - that is, eliminate compiler warnings, implement best practices, or upgrade your code to leverage the latest language opportunities - Solution-Wide Warnings and Suggestions is exactly what you need to get the job done quickly.

The feature includes two items:

  • Inspect Code - a new command that is available by right-clicking any node in Solution Explorer, from a single file to the whole solution:
  • Inspection Results - a tool window that actually shows problems found in a certain scope, broken up into several categories:

The Inspection Results tool window is pretty much similar to other ReSharper tool windows like Type Hierarchy or Find Results: it provides common navigation, grouping and export options. It also has a code preview pane where you can instantly see the context of a problem without opening the corresponding file in the text editor:

The tool window is not updated automatically, so as soon as you’ve made substantial changes, make sure to click the Refresh button to see up-to-date inspection results:

In many scenarios, chances are you don’t want to review all code problems you may have in a scope - you’d rather focus on a particular subset. Inspection Results lets you do this by clicking the glaring Filter button:

This displays the Filter Issues dialog box where you can select or unselect individual inspections or even entire inspection groups. For example, here’s how you can discover code that can be transformed to leverage C# 3.0 and C# 4.0 language features:

When you double-click an item in Inspection Results, the corresponding file opens in the text editor in the right position for you to actually take measures: apply a quick-fix, refactor or clean up code, or whatever:

However, instead of double-clicking through all inspections, in many cases you can apply fixes right from Inspection Results: say, you’re inspecting a single file that has three inspection items:

Of the three inspection items, two can be safely fixed using Code Cleanup, which can both delete a redundant “using” directive and make an explicitly typed variable implicitly typed. Then, you press Ctrl+Alt+F right on one of these two inspection items, and the Code Cleanup dialog box displays:

You launch Code Cleanup with Full Cleanup profile, and ReSharper applies fixes for both inspections straight away! Upon refreshing Inspection Results, you only have one inspection left:

In addition to cleaning up code, you can also apply refactorings and navigation actions right from Inspection Results. I hope to cover scenarios involving this approach in subsequent posts about ReSharper 5.

To try out Solution-Wide Warnings and Inspections in its latest incarnation, make sure to download a fresh ReSharper 5 nightly build!

ReSharper 5.0 Preview: Loops 2 LINQ

Friday, December 11th, 2009

On the eve of ReSharper 5.0 going beta, we thought it’s just about time to start elaborating on new R# features. We’re hoping for a series of posts on improved code analysis, navigation to and within external sources, structural search & replace, ASP.NET support, and other stunning capabilities that ReSharper 5.0 provides. Today, we’ll talk about R# going crazy over LINQ.

Among many new code inspections introduced in ReSharper 5.0, two are specifically aimed at converting for and foreach loops into LINQ statements and/or combinations of extension methods and lambda expressions. Here they are:

  • Loop can be converted into LINQ-expression
  • Part of loop’s body can be converted into LINQ-expression

If you are wondering why you would ever transform traditional cozy loops to LINQ, Jimmy Bogard, Matthew Podwysocki, and Justin Etheredge have done a great job of explaining why.

Back to ReSharper work, let’s see how it converts loops to LINQ method or query syntax for you in common scenarios.

First, there’s the aggregation scenario where we loop through a collection in order to get the number of this collection’s items for which a certain condition holds true:

ReSharper highlights the foreach keyword with a green curly underline. You press Alt+Enter for available actions, and ReSharper suggests that you convert the loop to a LINQ expression:

You apply the conversion, and here we go: no more temporary counter variable. Instead, we’ve got Count extension method with a lambda-styled condition:

Next, we’re taking on the transformation scenario where we’re looping though a collection to populate another collection:

Again, ReSharper is smart enough to see that the code fragment can be converted to LINQ method syntax:

On the output side, we’ve got one line of code instead of three, thanks to the ToDictionary extension method:

For the next scenario, let’s take a collection and filter it out, so to speak, like this:

ReSharper encounters three foreach loops and readily suggests that we convert each of them. Because conversion only occurs within a selected loop and its nested loops, let’s press Alt+Enter at the top-level loop:

What we’ve got here is a pretty return statement that leverages LINQ query syntax:

All three examples above show how ReSharper converts entire loops to LINQ. In many cases, specifically with write usages inside loops, that can’t be done. For such occasions, ReSharper provides its second LINQ-related inspection: Part of loop’s body can be converted into LINQ-expression. Unlike the previous action, it’s displayed as a hint and highlighted with a straight green underline.

Let’s take another foreach loop with a complex condition and a write usage inside:

Pressing Alt+Enter at the foreach keyword highlighted as a hint lets you convert a part of the loop to LINQ syntax:

ReSharper integrates the condition with iteration variable definition, resulting in a LINQ query that makes the previously bloated write usage clear:

If, for any reason, you want to keep your loops for future generations and you don’t need LINQ-related code inspections, or, say, you want them to display as warnings or even errors, you can always configure them by choosing ReSharper | Options | Code Inspection | Severity.

The two loop 2 LINQ inspections and many more exciting features are available right now: we keep publishing fresh ReSharper 5 nightly builds that you can download and evaluate.

Welcome to ReSharper 5.0 EAP!

Tuesday, October 20th, 2009

We’re happy to finally provide public access to early bits of the renewed ReSharper!

Starting today, you can download nightly ReSharper builds, and the first one is already there for you.

Note that this is only pre-release software. Nightly builds are assembled automatically every night without proper alpha testing, and we can’t guarantee that they will even run on your machine. However, we hope that by participating in this Early Access Program, you can help us gradually improve product quality up to a level that is proper for final release.

Some additional notes on this EAP:

  • At this time, we’re only publishing ReSharper builds for VS 2008. We’ll start publishing VS 2010-compatible builds as soon as we verify that ReSharper works decently with VS 2010 Beta 2.
  • The UI for many new and improved ReSharper features is not yet finalized. Shortcuts and titles may change as well.

Once you have downloaded, installed, and used a nightly build for a while, please vote for it in the nightly builds page so that other users are able to judge on its quality. If you’re willing to report a bug or feature request, please do so using our bug tracker (here’s how it works).

To further comment on the quality of individual nightly builds please use jetbrains.resharper.eap newsgroup or its web mirror in our development community.

To get an idea of improvements and new features that we’re starting to reveal during this EAP, take a look at the recently published ReSharper 5 Overview.

Happy early accessing!

Preview of ReSharper for Visual Studio 2010 Coming Soon

Tuesday, May 19th, 2009

Scientists hypothesize on the existence of ReSharper for Visual Studio 2010. They argue pros and cons endlessly, bringing history to the table, and even gamble around the odds of ReSharper running in the new shining Visual Studio 2010 Beta 1.

You can know for sure, it does run! Want to know when it will be running for you? Read on!

See how the new Extension Manager happily shows ReSharper as an installed extension, making it a first class citizen in the new Visual Studio shell.

Here, our well-known ReSharper tool windows are shown participating in the improved Visual Studio docking system.

Can you see those unit tests running? Of course not, it’s a screenshot. But rest assured that development pleasure-inducing bits are being cooked up in large integration ovens here at JetBrains.

Next to integration ovens, there is a high-tech lab where die-hard developers in white coats are implanting smartness into ReSharper’s understanding of C# 4. Variance and dynamic, optional parameters and named arguments - all the features of the new C# version are being mercilessly stuffed inside the most intelligent add-in for Visual Studio. Developers perform their cruel experiments with Visual Studio 2008, writing C# 4 code where only the previous version of the language is meant to be used.

Now, you might want to know when all the intelligence of ReSharper will be available for Visual Studio 2010. No need to wait for ReSharper 5.0 to come out! We plan to release preview bits compatible with Visual Studio 2010 for you to play and test in June, and June is just around the corner!