Resharper 能否将 case 语句格式化为与 case 本身在同一行开始?

发布于 2024-10-09 07:48:32 字数 729 浏览 4 评论 0原文

我正在尝试找到一种方法让 Resharper 格式化这样的 switch 语句(使用 Ctrl+E、Ctrl+C):

switch (int_i) {
    case 1  : Console.WriteLine("You entered one");
              break;
    case 2  : Console.WriteLine("You entered two");
              break;
    case 3  : Console.WriteLine("You entered three");
              break;
    default : Console.WriteLine("Please enter a number between 1 and 5");
              break;
}

我个人根本不喜欢这种风格,但我正在学习的一本书中使用了它我希望 Resharper 帮助我以同样的方式格式化我的代码。

这里有两件事需要注意:

  1. 语句必须与 case 语句在同一行开始。
  2. 冒号必须对齐,因此由于 defaultcase 3 长,因此 case 1 的冒号之前必须有一个额外的空格, 情况 2情况 3

Resharper可以做到这一点吗?

I'm trying to find a way to have Resharper format a switch statement like this (using Ctrl+E, Ctrl+C):

switch (int_i) {
    case 1  : Console.WriteLine("You entered one");
              break;
    case 2  : Console.WriteLine("You entered two");
              break;
    case 3  : Console.WriteLine("You entered three");
              break;
    default : Console.WriteLine("Please enter a number between 1 and 5");
              break;
}

I personally don't like this style at all, but it's being used in a book I'm studying and I'd like Resharper to help me format my code the same way.

There are two things to note here:

  1. The statements have to start on the same line as the case statement.
  2. The colons have to be aligned, so because default is longer than case 3 there has to be an extra space before the colon on case 1, case 2 and case 3.

Can Resharper do this?

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

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

发布评论

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

评论(3

缱绻入梦 2024-10-16 07:48:32

您可以在ReSharper - 选项 - 语言 - C# - 格式化样式中自定义代码格式,但似乎没有任何关于您的要求。我发现的最接近的东西 - Switch 语句中的大括号格式。

You can customize code formatting in ReSharper - Options - Languages - C# - Formatting Style, but it seems there is nothing about you are asking. Closest thing I found - braces formatting in Switch statement.

不必在意 2024-10-16 07:48:32

我知道这已经很老了,但这是可能的。只是不太明显。我现在才偶然发现它。

转到 ReSharper 选项/代码编辑/C#/格式样式/大括号布局。选择‘Block under ‘case’ label’可以看到样本是错误的。现在选择其他。将其设置为“在行尾(K&R 样式)”。样本中的任何内容都不会改变。再次选择“在“case”标签下阻止”,查看格式现在是否正确。

我更喜欢 BSD 风格,它也可以工作。

I know this is old, but it is possible. It's just not obvious. I just now stumbled on to it.

Go to ReSharper Options/Code Editing/C#/Format Style/Braces Layout. Select 'Block under "case" label' to see that the sample is wrong. Now select Other. Set it to "At end of line (K&R Style)". Nothing in the sample will change. Select 'Block under "case" label' again to see that the formatting is now correct.

I prefer BSD style which also works.

缘字诀 2024-10-16 07:48:32

我认为现在有一个选择。我的是2017.3.2版本。

转到 ReSharper 选项/代码编辑/C#/格式化样式/换行和换行/嵌入语句的排列,并将“将简单的“case”语句放在同一行”设置为“始终”。

I think there is now an option for that. I have the version 2017.3.2.

Got to ReSharper Options/Code Editing/C#/Formatting Style/Line Breaks and Wrapping/Arrangement of Embedded Statements and set Place simple "case" statement on same line to Always.

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