如何在 ReSharper 中用 using 语句包围代码块?

发布于 2024-07-09 06:19:23 字数 819 浏览 8 评论 0原文

我正在观看 Stephen A Bohlen 的精彩 NHibernate 之夏 系列,并一直在观看他与 CodeRush 的互动。 我最近安装了 ReSharper(我是 ReSharper 新手),并且正在尝试找到 Stephen 正在用 CodeRush 演示(切线)的一些 ReSharper 生产力等效项。

作为一个例子,他演示了突出显示如下所示的代码块:

ISession session = GetSession();
session.xxx

然后将其转换为

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

他通过突出显示他想要用 using 语句包围的代码块,然后调用一些 CodeRush 模板来实现此目的。 我一直无法弄清楚如何使用 ReSharper 做同样的事情。 我找到了 ReSharper Surround 命令(在 Code 命令内),但是当您选择 using 语句时,它确实包围了突出显示的代码块,但它似乎不够智能将第一行放在 using 子句中。 也就是说,它的结果是:

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

这要求我将第一行移到 using 子句中。 我错过了更简单的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绝不服输 2024-07-16 06:19:23

Resharper 提供了此功能。 如果您的类实现了 IDisposable,只需按 Alt-Enter:

(已删除图像死链接“之前”)
(删除了图像死链接“之后”)

您还必须将光标放在变量名称上。 像这样: SShot1 SShot2 SShot3

Resharper offers this capability. If your class implements IDisposable, just hit Alt-Enter:

(removed image dead link "Before")
(removed image dead link "After")

You also have to have your cursor on variable name. Like this: SShot1 SShot2 SShot3

橙幽之幻 2024-07-16 06:19:23

我刚刚看了那集,想知道我是否也有同样的问题。 根据这里的线索,我发现如果我有这样的代码:

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

如果我将光标放在第 2 行中的会话变量上,然后按 Alt-Enter,它会给我:

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;

它确实让我好几次了,因为我我的光标没有放在变量名称上,但我确实让它工作了。

顺便说一句,我真的更喜欢观看他们使用 Code Rush 的演示,因为您可以通过视觉指示器了解正在发生的情况。 我想知道如果我不做演讲,这是否会造成妨碍。

而且您不是 Resharper 新手:您是 Resharper 学徒 :)

Swampy

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

凡间太子 2024-07-16 06:19:23

或者,如果您处于已经键入 using 的情况,并且想要用大括号括起大量代码...您可以执行 CTRL-E + U,然后7

它将用大括号括住所选内容。

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.

旧话新听 2024-07-16 06:19:23

我想知道我的 ReSharper 设置是否有问题。 当我的光标位于

Class1 c = new Class1();

下面代码示例中的行时,ReSharper 仅建议使用“var”。 如果既不提供拆分声明和赋值也不放入使用构造

(明道加斯——您对 DirectoryInfo 的评论当然是正确的。我的错。希望这个示例更清楚地说明了我所看到的内容)。

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();
        }
    }
}

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();
        }
    }
}
一人独醉 2024-07-16 06:19:23

在 Visual Studio 2013 上,它是 CTRL + E + U
这应该会出现以下对话框

在此处输入图像描述

,然后出现 A 用于使用.

希望这可以为您节省一些时间。

On Visual Studio 2013 it's CTRL + E + U
This should bring following dialog

enter image description here

and then A for using.

Hope this saves you some time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文