Swiftformat: Disable indentation in multi-line if statements

Created on 18 Oct 2018  路  4Comments  路  Source: nicklockwood/SwiftFormat

I can't seem to find an option to disable the indentation that is done on multi-line if and guard statements by default.

The problem is that you can easily end up with something like this:

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid {
    a.doSomething()
    b.doSomething()
}

The indentation here makes it nearly impossible to tell where the if statement ends and its body begins. I would usually just leave it as:

if let a = something,
let b = somethingelse,
a.isValid,
b.isValid {
    a.doSomething()
    b.doSomething()
}

Which is much easier to parse visually.

Any chance of getting an option to leave these statements alone?

enhancement

Most helpful comment

I arrived at this issue because I apparently want the opposite of what @DagAgren wants. A few examples:

SwiftFormat's format:

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid {
    a.doSomething()
    b.doSomething()
}

DagAgren's desired format:

if let a = something,
let b = somethingelse,
a.isValid,
b.isValid {
    a.doSomething()
    b.doSomething()
}

My desired format:

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid {
        a.doSomething()
        b.doSomething()
}

Personal style is a funny thing, as I find @DagAgren's desired format much more challenging to read than the SwiftFormat's format, but still not as easy to read as the one I desire. (Which, by the way, is how Xcode auto-indents the block, FWIW.)

Another fairly readable format is:

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid
{
    a.doSomething()
    b.doSomething()
}

This one creates the visual distinction between the conditional test and the block.

Is there a rule (or a pair of them) that could be added and then mixed-and-matched to satisfy all variants?

All 4 comments

@DagAgren there's currently no option to disable indent for wrapped lines, but I've been considering adding an option to use an intermediate indentation level for them, as discussed here: https://github.com/nicklockwood/SwiftFormat/issues/191

If it could be set to "use same ident as first line", that would solve the problem quite well I think.

I arrived at this issue because I apparently want the opposite of what @DagAgren wants. A few examples:

SwiftFormat's format:

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid {
    a.doSomething()
    b.doSomething()
}

DagAgren's desired format:

if let a = something,
let b = somethingelse,
a.isValid,
b.isValid {
    a.doSomething()
    b.doSomething()
}

My desired format:

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid {
        a.doSomething()
        b.doSomething()
}

Personal style is a funny thing, as I find @DagAgren's desired format much more challenging to read than the SwiftFormat's format, but still not as easy to read as the one I desire. (Which, by the way, is how Xcode auto-indents the block, FWIW.)

Another fairly readable format is:

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid
{
    a.doSomething()
    b.doSomething()
}

This one creates the visual distinction between the conditional test and the block.

Is there a rule (or a pair of them) that could be added and then mixed-and-matched to satisfy all variants?

@MattLewin swiftformat 0.45.0 now converts

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid {
    a.doSomething()
    b.doSomething()
}

to

if let a = something,
    let b = somethingelse,
    a.isValid,
    b.isValid
{
    a.doSomething()
    b.doSomething()
}

by default. Hope that helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

meherkasam picture meherkasam  路  3Comments

AniHovhannisyanAni picture AniHovhannisyanAni  路  3Comments

MaxDesiatov picture MaxDesiatov  路  3Comments

mdiep picture mdiep  路  3Comments

DavinAhn picture DavinAhn  路  4Comments