belav / csharpier

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

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

Open pvginkel opened 10 months ago

pvginkel commented 10 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 10 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 2 months 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?