nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 640 forks source link

Swiftformat 0.48.18 indentation rules changed (?) #1074

Closed psiwork closed 2 years ago

psiwork commented 2 years ago

When I updated SwiftFormat to 0.48.18 and run it, I have got a tons of new fixes, most of them with an indentations. The issue is: now it conflicting with Xcode's own indentation rules.

I attached the screenshot below with the changes being proposed by Swiftformat. Looks like it tries to align with the closing bracket }. But Xcode always keeps the indentations to distinguish between the code blocks. We discussed with the team and it's not obviously good idea: looks better but readability suffers.

Is it a intentional change or a kind of bug of SwiftFormat?

Screenshot 2021-11-11 at 12 12 37

timstudt commented 2 years ago

I can see why it's aligned with closing brackets, but especially in SwiftUI this decreases readability IMO.

struct SomeView: View {
    var body: some View {
        Button("1", action: {
            /* */
        })
        .padding(10)  // swiftformat
        Button("2", action: {
            /**/
        })
    }
}
struct SomeView: View {
    var body: some View {
        Button("1", action: {
            /* */
        })
            .padding(10)  // Xcode default
        Button("2", action: {
            /**/
        })
    }
}

even with --wraparguments before-first and --wrapparameters before-first it's harder to read:

struct SomeView: View {
    var body: some View {
        Button(
            "1",
            action: {
                /* */
            }
        )
        .padding(10) // aligns with closing bracket ')'
        Button(
            "2",
            action: {
                /**/
            }
        )
    }
}

when using trailing closures it looks fine

struct SomeView: View {
    var body: some View {
        Button(
            "1") {
                /* */
            }
            .padding(10) // <- indented
        Button(
            "2",
            action: {
                /**/
            }
        )
    }
}

I feel that ) should not be handled the same way as } but rather indent the old way.

nicklockwood commented 2 years ago

Hmm, this was always the intended behavior, but due to a bug wasn't always enforced before.

It may have other side-effects that you don't want, but have you tried using --xcodeindentation true? This enables a mode where swiftformat indenting behaves more like Xcode's defaults.

If that doesn't solve your issue, I'll consider adding another option just to control this case.

timstudt commented 2 years ago

@nicklockwood --xcodeindentation true works as expected. thx