belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.42k stars 99 forks source link

Extra newline in switch case statement with curly braces #1192

Closed emberTrev closed 8 months ago

emberTrev commented 8 months ago

Input:

switch (foo)
{
    case 0:
        {
            // do stuff here
        }
        break;
    case 1:
        {
            // do stuff here
        }
        break;
}

Output:

switch (foo)
{
    case 0:

        {
            // do stuff here
        }
        break;
    case 1:

        {
            // do stuff here
        }
        break;
}

Expected behavior:

There shouldn't be an empty line added between the case statement and the opening curly brace.

For the record, I am not a fan of these curly braces, but you need to create a new scope if you want to declare the same variable name in multiple cases of the switch (CS0128). My coworkers always put the break outside the ending curly brace, which causes CSharpier to add a newline before the opening curly brace. There is a workaround by moving the break inside the closing curly brace, which I have been doing when I can. Doesn't change that the extra newline shouldn't be there in this case.

belav commented 8 months ago

This is what I ended up doing

switch (someValue)
{
    case 0:
    {
        // dedented because the only statement is a block
        break;
    }

    case 1:
        {
            // indented because there are two statements, a block then a break
        }
        break;
}

It looks a bit weird with the break; dedented

switch (someValue)
{
    case 0:
    {
        // some comment
    }
    break;
    case 1:
    {
        // some comment
    }
    break;
}

And also with the block and break; at different indents.


switch (someValue)
{
    case 0:
    {
        // some comment
    }
        break;
    case 1:
    {
        // some comment
    }
        break;
}