Resharper 能否将 case 语句格式化为与 case 本身在同一行开始?
我正在尝试找到一种方法让 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 帮助我以同样的方式格式化我的代码。
这里有两件事需要注意:
- 语句必须与 case 语句在同一行开始。
- 冒号必须对齐,因此由于
default
比case 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:
- The statements have to start on the same line as the case statement.
- The colons have to be aligned, so because
default
is longer thancase 3
there has to be an extra space before the colon oncase 1
,case 2
andcase 3
.
Can Resharper do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在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.
我知道这已经很老了,但这是可能的。只是不太明显。我现在才偶然发现它。
转到 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.
我认为现在有一个选择。我的是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.