views:

498

answers:

4

I'm watching Stephen A Bohlen's excellent Summer of NHibernate series, and have been watching him interact with CodeRush. I've recently installed ReSharper (I'm a ReSharper newbie), and I'm trying to find some of the ReSharper productivity equivalents that Stephen is demonstrating (tangentially) with CodeRush.

As an example, he demonstrates highlighting a code block that looks like this:

ISession session = GetSession();
session.xxx

and then turning it into

using (ISession session = GetSession())
{
   session.xxx
}

He does this by highlighting the block he wants to surround with the using statement and then invoking some CodeRush template. I've been unable to figure out how to do the equivalent thing with ReSharper. I found the ReSharper Surround command (within the Code command), but when you select the using statement, it does surround your highlighted code block, but it does not seem smart enough to put the first line within the using clause. That is, it results in:

using () 
{
  ISession session = GetSession();
  session.xxx
}

which requires me to move the first line into the using clause. Am I missing an easier way?

+6  A: 
Mindaugas Mozūras
This isn't working for me? I only get split declaration and assignment?namespace DataAccessLayer { public class hptest { public void Test() { DirectoryInfo di = new DirectoryInfo("xx"); FileInfo[] files = di.GetFiles(); } }}
Decker
DirectoryInfo does not implement IDisposable. Any class instance defined in the parameter list of a 'using' keyword has its Dispose() member called at the end of the statement. DirectoryInfo doesn't have a Dispose() member to call, so it can't be in a "using" statement.
Mindaugas Mozūras
A: 

Alternatively, if you are in a situation where the using is already typed and you want to wrap an amount of code with braces... you can do a CTRL-E + U and then 7.

It will wrap the selection with braces.

Maxim
A: 

I wonder if something is wrong with my ReSharper setup. When my cursor is on the

Class1 c = new Class1();

line in the code sample, below, ReSharper only suggests, Use 'var'. If offers NEITHER Split declaration and assignment NOR Put into using construct??

(Mindaugas -- Your comment is of course correct about DirectoryInfo. My bad. Hopefully, this example more clearly illustrates what I'm seeing).

using System;

namespace DataAccessLayerTest {
    public class Class1 : IDisposable {
        public void Moo()
        {
            Console.Out.WriteLine("Moo");
        }
        public void Dispose()
        {
        }
    }

    public class Class2 {
        public void m()
        {
            Class1 c = new Class1();
            c.Moo();
        }
    }
}
Decker
You have to have your cursor on variable name. Like this:http://img204.imageshack.us/img204/3872/sshot1dd8.jpghttp://img99.imageshack.us/img99/9543/sshot2dn0.jpghttp://img220.imageshack.us/img220/9697/sshot3hd5.jpgIt's debatable, if that's really the best decision from Resharper team.
Mindaugas Mozūras
That did it! You should post your answer so that I can mark it as answered. :-)
Decker
I've edited it into my answer.
Mindaugas Mozūras
A: 

I was just watching that episode and wondering if I the same question. Based on the leads here, I found that if I had this code:

1        IList<Supplier> returnValue;
2        ISession session = SessionProvider.GetSession();
3        returnValue = session.CreateQuery("select from Supplier s").List<Supplier>();
4        return returnValue;

If I put my cursor on the session variable in line 2, and then did Alt-Enter, it would give me:

1        IList<Supplier> returnValue;
2        using (ISession session = SessionProvider.GetSession())
3        {
4            returnValue = session.CreateQuery("select from Supplier s").List<Supplier>();
5        }
6        return returnValue;

It did get me a couple of times because I did not have my cursor on the variable name, but I did get it to work.

Just a side note, I really prefer watching demonstrations where they are using Code Rush simply because you have visual indicators of what is going on. I wonder if that would get in the way if I was not presenting.

And you are NOT a Resharper Newbie: You are a Resharper Padawan :)

Swampy

SwampyFox