nicklockwood / SwiftFormat

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

Formatting a SwiftUI view modifier surrounded by #if/#endif has extra indentation #1632

Open lyamamot opened 7 months ago

lyamamot commented 7 months ago

SwiftFormat is giving me extra indentation when formatting code inside an #if block and I don't know why.

I'd like to format the following code as shown:

Essentially, I want to disable a block of code using #if but keep what's inside the block formatted as usual.

public struct IfIndentView: View {
    public var body: some View {
        Text("Hello")
    }
#if false
    // Hi
    .task {
        print("Some work here.")
    }
#endif
}

However, when I format this using swiftformat --swiftversion 5.9 --ifdef outdent (version 0.53.2), the code inside the #if has extra indentation in the comment before .task and in part of that block itself:

public struct IfIndentView: View {
    public var body: some View {
        Text("Hello")
    }
#if false
        // Hi
    .task {
            print("Some work here.")
        }
#endif
}

I couldn't find a good combination of configuration options to try to fix this, though if I remove the #if altogether, of course it formats correctly.