belav / csharpier

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

Breaking of code embedded in raw strings with string interpolation #1136

Open pvginkel opened 9 months ago

pvginkel commented 9 months ago

Input:

See output.

Output:

var v = $"""
    abcde abcde abcde abcde abcde {string.Join(
        ", ",
        new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
    )}
    """;

Expected behavior:

Not sure :). Maybe this?

var v = $"""
    abcde abcde abcde abcde abcde {
        string.Join(", ", new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })
    }
    """;

or

var v = $"""
    abcde abcde abcde abcde abcde {
        string.Join(
            ", ",
            new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
        )
    }
    """;

I just don't think the current formatting is ideal.

Btw this is changed behavior from 0.26.0. I think until the current version it wouldn't even warp this code, not sure.

belav commented 8 months ago

Right now there is no formatting applied to expressions that appear within a raw interpolated string.

From what I recall, the original interpolated strings did not allow line breaks in any expressions within them. So CSharpier was forcing those expressions to be flat. When raw interpolated strings were first supported in CSharpier, they were treated the same way which would have resulted in

var v = $"""
    abcde abcde abcde abcde abcde {string.Join(", ", new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })}
    """;

And then when I was getting raw strings to indent correctly, the side effect was that I removed all formatting from the expressions in them.

Long story short, I believe it should be possible to get expressions within raw strings to format.

RReverser commented 1 month ago

Raw string literals, by their multiline nature, are currently the largest unformatted (well, manually formatted) pieces of code in our codebase. I briefly looked into the current implementation but I'm afraid the logic for raw literals is quite beyond my expertise to implement this formatting myself in a PR. @belav is this something still on your radar?