Jintin / Swimat

An Xcode formatter plug-in to format your swift code.
https://jintin.github.io/Swimat/
MIT License
1.65k stars 89 forks source link

Bug in closure #153

Closed PMExtra closed 7 years ago

PMExtra commented 7 years ago

My code:

func test(a: [Int]) -> [Int] {
    return a
        .map { (i) in
            i * 2
        }
}

After Swimat:

func test(a: [Int]) -> [Int] {
    return a
        .map { (i) in
    i * 2
    }
}
PMExtra commented 7 years ago

以上例子只為重現Bug,所以看起來有些多餘,本可以一行解決。但實際中的程式碼通常是這樣的:

func getDevices(token: String, cache: Bool = true) -> Observable<[Device]> {
    // ...
    // Other code to return cache
    return RxMoyaProvider<MyService>()
        .request(.list(token: token))
        .map { (response) in
            let items = JSON(data: response.data)["items"].arrayValue
            return items.map { Device(jsonData: $0)! }
        }
        .do(onNext: { (devices) in
            self._devices = devices
            self.lastUpdated = Date()
        })
}
Jintin commented 7 years ago

這個問題很有趣也有點難,可能要一些時間 It's a funny and hard one, might take a long time than I expect.

第一個是跟 #150 衝突了 First of all, it conflict with #150

if let a = b,
    c == d {
    a + c // correct indent by Xcode
        a + c // additional indent here
}

第二個是當 train 的最後一個沒有 indent Second, the indent is different in different context.

func getDevices(token: String, cache: Bool = true) -> Observable<[Device]> {
    // ...
    // Other code to return cache
    return RxMoyaProvider<MyService>()
        .request(.list(token: token))
        .map { (response) in
            let items = JSON(data: response.data)["items"].arrayValue
            return items.map { Device(jsonData: $0)! }
        }//BEFORE
        .do(onNext: { (devices) in
            self._devices = devices
            self.lastUpdated = Date()
        })
}

func getDevices2(token: String, cache: Bool = true) -> Observable<[Device]> {
    // ...
    // Other code to return cache
    return RxMoyaProvider<MyService>()
        .request(.list(token: token))
        .map { (response) in
            let items = JSON(data: response.data)["items"].arrayValue
            return items.map { Device(jsonData: $0)! }
    }//AFTER
}

你覺得應該怎麼處理比較好呢? 我可以先寫個不會差太遠的版本再想要怎麼處理

What do you think. I can make a simple version to make it more readable first.

Jintin commented 7 years ago

請更新再試一次,謝謝你的回報。 如果還有什麼問題,請不吝指教。

Please update and try again, thank you for your feedback.