Search code examples
xcodeauto-indenteureka-forms

How can I make Xcode not auto indent Eureka forms code in a weird way?


When I use Eureka forms, Xcode seems to like to format it in a way that could cause confusion.

I'll use the one of the code blocks in the README as an example:

let row  = SwitchRow("SwitchRow") { row in      // initializer
    row.title = "The title"
    }.onChange { row in
        row.title = (row.value ?? false) ? "The title expands when on" : "The title"
        row.updateCell()
    }.cellSetup { cell, row in
        cell.backgroundColor = .lightGray
    }.cellUpdate { cell, row in
        cell.textLabel?.font = .italicSystemFont(ofSize: 18.0)
}

This really makes my OCD go off. The last } isn't inline with all the other ones feels so annoying.

I would like to format it like so:

let row  = SwitchRow("SwitchRow") { row in      // initializer
    row.title = "The title"
    }.onChange { row in
        row.title = (row.value ?? false) ? "The title expands when on" : "The title"
        row.updateCell()
    }.cellSetup { cell, row in
        cell.backgroundColor = .lightGray
    }.cellUpdate { cell, row in
        cell.textLabel?.font = .italicSystemFont(ofSize: 18.0)
    }

Or this:

let row  = SwitchRow("SwitchRow") { row in      // initializer
    row.title = "The title"
}.onChange { row in
    row.title = (row.value ?? false) ? "The title expands when on" : "The title"
    row.updateCell()
}.cellSetup { cell, row in
    cell.backgroundColor = .lightGray
}.cellUpdate { cell, row in
    cell.textLabel?.font = .italicSystemFont(ofSize: 18.0)
}

So I went to the preference pane of Xcode and looked for things like custom indentation. I thought there would be something similar to the formatting settings in IntelliJ, but I found nothing.

Then I found the closest thing to what I'm looking for - Automatic Indent. So I unchecked the checkbox for }, like the:

enter image description here

But as I type .onChange { then press enter, this happens:

    let row = SwitchRow("") {
        row in
        }.onChange {

    }

How can make it not automatically indent to that? I want one of the styles mentioned above.


Solution

  • If you are willing to use the non-trailing syntax, i.e. with extra parentheses (which admittedly bloat the code a bit) the auto-indentation should work fine.

    Your example code gets formatted to:

    let row  = SwitchRow("SwitchRow", { row in      // initializer
        row.title = "The title"
    }).onChange({ row in
        row.title = (row.value ?? false) ? "The title expands when on" : "The title"
        row.updateCell()
    }).cellSetup({ cell, row in
        cell.backgroundColor = .lightGray
    }).cellUpdate({ cell, row in
        cell.textLabel?.font = .italicSystemFont(ofSize: 18.0)
    })