Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch expressions have extra line breaks #1282

Closed
charliefoxtwo opened this issue Jun 7, 2024 · 3 comments · Fixed by #1295
Closed

Switch expressions have extra line breaks #1282

charliefoxtwo opened this issue Jun 7, 2024 · 3 comments · Fixed by #1295

Comments

@charliefoxtwo
Copy link

charliefoxtwo commented Jun 7, 2024

There appears to be an extra line break in nested switch expressions, as well as for all default branches

Input:

public class ClassName {
    public bool Foo(object entry)
    {
        return entry switch
        {
            string s => s.Length switch
            {
                1 => true,
                2 => false,
                _ => throw new ArgumentOutOfRangeException("this specific string length is not supported"),
            },
            int i => i,
            _ => throw new ArgumentOutOfRangeException($"entry type {entry.GetType()} not supported"),
        };
    }
}

Output:

public class ClassName
{
    public bool Foo(object entry)
    {
        return entry switch
        {
            string s
                => s.Length switch
                {
                    1 => true,
                    2 => false,
                    _
                        => throw new ArgumentOutOfRangeException(
                            "this specific string length is not supported"
                        ),
                },
            int i => i,
            _
                => throw new ArgumentOutOfRangeException(
                    $"entry type {entry.GetType()} not supported"
                ),
        };
    }
}

Expected behavior:

public class ClassName {
    public bool Foo(object entry)
    {
        return entry switch
        {
            string s => s.Length switch
            {
                1 => true,
                2 => false,
                _ => throw new ArgumentOutOfRangeException(
                    "this specific string length is not supported"
                ),
            },
            int i => i,
            _ => throw new ArgumentOutOfRangeException(
                $"entry type {entry.GetType()} not supported"
            ),
        };
    }
}
@belav
Copy link
Owner

belav commented Jun 7, 2024

The lines with exceptions end up long enough that they break. Right now when switch arms break, the first break occurs before the => and I don't recall if there was a specific reason it was done that way. Your example does look cleaner, I'll take a look at making this change.

@belav belav added this to the Planned milestone Jun 7, 2024
@charliefoxtwo
Copy link
Author

Yup, I agree the exception lines should break, it's just the break being before the => that I find weird. Is that maybe done with the assumption/in the case that the text preceding the arrow is very long? I'm still not sure if that's how I'd want it to look, but I could see that making more sense. e.g.

return s switch {
    "some very very long string"
        => "something else very very long",
}

I feel like I'd still prefer the arrow on the same line though 🤔

return s switch {
    "some very very long string" =>
        "something else very very long",
}

@belav
Copy link
Owner

belav commented Jun 16, 2024

CSharpier has adopted operators at the beginning of lines as opposed to the end, but after saying that I realize that it seems the other instances of => are always at the end of lines when things break. It looks pretty straightforward to change and should really be consistent with the other instances.

public class ClassName
{
    public string ShortPropertyName =>
        "einairenst_____________________________________________________________________";

    public string LongUglyMethod() =>
        "einairenst_____________________________________________________________________";

    public void DoStuff()
    {
        CallMethod(o =>
            DoThingWith_______________________________________________________________________________(
                o
            )
        );
    }
}

belav added a commit that referenced this issue Jun 16, 2024
@belav belav modified the milestones: Planned, 0.29.0 Jun 16, 2024
belav added a commit that referenced this issue Jun 28, 2024
belav added a commit that referenced this issue Jul 27, 2024
@belav belav closed this as completed in 7d1dc56 Aug 16, 2024
pisolofin pushed a commit to pisolofin/csharpier-editorconfig that referenced this issue Aug 22, 2024
Other instances of `=>` stay on the line and break after, this seems to
be the only instance that breaks before `=>` and should be consistent
with the others.

closes belav#1282
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants