Skip to content

Allow let declarations on multiple lines to align vertically (as is the Xcode standard) #1138

@SebastianLjungman

Description

@SebastianLjungman

We recently enabled running swift-format as a pre-commit hook, and while setting up the configuration/rules I noticed that there doesn't seem to be a way to enforce the standard alignment of let declarations on multiple lines.

E.g.: When typing the following in Xcode, each let aligns perfectly vertical with the first let after pressing Enter:

class TestClass {
    var x: Bool? = true
    var y: Bool? = false
    var z: Bool? = true
    
    func test() -> String {
        if let x = self.x,
           let y = self.y,
           let z = self.z
        {
            return "Hello"
        } else {
            return "Goodbye"
        }
    }
}

But once I run swift-format, all the let:s with the exception of the first one is indented one whitespace further!

class TestClass {
    var x: Bool? = true
    var y: Bool? = false
    var z: Bool? = true

    func test() -> String {
        if let x = self.x,
            let y = self.y,
            let z = self.z
        {
            return "Hello"
        } else {
            return "Goodbye"
        }
    }
}

This looks ugly, and it's weird that the linter would enforce a standard that doesn't match the editor behavior, especially without an option to turn it off. Is it perhaps a bug?

I'm using swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx26.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions