开发者

Can Resharper format case statements to start on the same line as the case itself?

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 o开发者_JS百科n case 1, case 2 and case 3.

Can Resharper do this?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜