nicklockwood / SwiftFormat

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

Comment before `--ifdef` results in unexpected modifier indentation #1527

Closed mikemahony closed 10 months ago

mikemahony commented 11 months ago

When you have a comment before an #if the indentation of any chained modifier is unexpected.

Input

struct ContentView: View {
    var body: some View {

        // swiftformat:options --ifdef indent

        Text("Hello, world!")
        // Comment above
        #if os(macOS)
            .padding()
        #endif

        Text("Hello, world!")
        #if os(macOS)
            // Comment inside
            .padding()
        #endif

        // swiftformat:options --ifdef no-indent

        Text("Hello, world!")
        // Comment above
        #if os(macOS)
            .padding()
        #endif

        Text("Hello, world!")
        #if os(macOS)
            // Comment inside
            .padding()
        #endif

        // swiftformat:options --ifdef outdent

        Text("Hello, world!")
        // Comment above
        #if os(macOS)
            .padding()
        #endif

        Text("Hello, world!")
        #if os(macOS)
            // Comment inside
            .padding()
        #endif
    }
}

Configuration

All three --ifdef options.

Expected output

struct ContentView: View {
    var body: some View {

        // swiftformat:options --ifdef indent

        Text("Hello, world!")
        // Comment above
        #if os(macOS)
            .padding()
        #endif

        Text("Hello, world!")
        #if os(macOS)
            // Comment inside
            .padding()
        #endif

        // swiftformat:options --ifdef no-indent

        Text("Hello, world!")
        // Comment above
        #if os(macOS)
            .padding()
        #endif

        Text("Hello, world!")
        #if os(macOS)
            // Comment inside
            .padding()
        #endif

        // swiftformat:options --ifdef outdent

        Text("Hello, world!")
// Comment above
#if os(macOS)
            .padding()
#endif

        Text("Hello, world!")
#if os(macOS)
            // Comment inside
            .padding()
#endif
    }
}

Actual output

struct ContentView: View {
    var body: some View {

        // swiftformat:options --ifdef indent

        Text("Hello, world!")
        // Comment above
        #if os(macOS)
        .padding()
        #endif

        Text("Hello, world!")
        #if os(macOS)
            // Comment inside
            .padding()
        #endif

        // swiftformat:options --ifdef no-indent

        Text("Hello, world!")
        // Comment above
        #if os(macOS)
        .padding()
        #endif

        Text("Hello, world!")
        #if os(macOS)
            // Comment inside
            .padding()
        #endif

        // swiftformat:options --ifdef outdent

        Text("Hello, world!")
// Comment above
#if os(macOS)
.padding()
#endif

        Text("Hello, world!")
#if os(macOS)
            // Comment inside
            .padding()
#endif
    }
}
nicklockwood commented 11 months ago

@mikemahony thanks for reporting this - I've added a fix for the next release.

nicklockwood commented 10 months ago

@mikemahony fixed in 0.52.5