swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.2k stars 10.32k forks source link

[SR-2188] Xcode closure auto-indentation is hard to read with some styles #44796

Open swift-ci opened 8 years ago

swift-ci commented 8 years ago
Previous ID SR-2188
Radar None
Original Reporter bnut (JIRA User)
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 3 | |Component/s | Source Tooling | |Labels | Bug | |Assignee | @nkcsgexi | |Priority | Medium | md5: 51aa435be00baf4ac468c07bab9de164

blocks:

Issue Description:

Currently Xcode handles this indentation well:

UIView.animate(
    withDuration: 12.0,
    animations: {
        print("statements have good indentation")
    },
    completion: { done in
        print("statements have good indentation")
    })

However, if the arguments are not broken over multiple lines it looks like this:

UIView.animate(withDuration: 12.0, animations: {
    print("statements have good indentation")
    }, completion: { done in // this shouldn't be indented
        print("statements have weird indentation")
})

Having the print statement at the same level as the closure's closing bracket makes it less clear that the closure has closed. At a glance a programmer may think that completion is an expression inside the first closure.

I expect it to look like this:

UIView.animate(withDuration: 12.0, animations: {
    print("statements have good indentation")
}, completion: { done in // this shouldn't be indented
    print("statements have weird indentation")
})

I'm also happy if it looks like this, although it's a little weird, it's at least consistent:

UIView.animate(withDuration: 12.0, animations: {
        print("statements have good indentation")
    }, completion: { done in // this shouldn't be indented
        print("statements have weird indentation")
    })

I expect indentation to approximately reflect the syntax tree, all arguments at the same level should get the same indentation. So sibling closures should get the same indentation on their closing bracket.

This is discussed in more detail here: https://github.com/apple/swift/pull/3754

swift-ci commented 5 years ago

Comment by shingo (JIRA)

Please fix this.

Promise hard to read.

https://github.com/mxcl/PromiseKit

README.md say

firstly {
    when(fulfilled: fetchImage, fetchLocation)
}.done { image, location in
    self.imageView.image = image
    self.label.text = "\(location)"
}.ensure {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
}.catch { error in
    self.show(UIAlertController(for: error), sender: self)
}

But, Xcode

firstly {
    when(fulfilled: fetchImage, fetchLocation)
    }.done { image, location in
        self.imageView.image = image
        self.label.text = "\(location)"
    }.ensure {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }.catch { error in
        self.show(UIAlertController(for: error), sender: self)
}