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

Spacing issues for method parameters #164

Closed saagarjha closed 7 years ago

saagarjha commented 7 years ago

Swimat is formatting this incorrectly:

timer = DispatchSource.makeTimerSource( queue: queue)

Swimat doesn't remove the space before queue: for whatever reason. @Jintin could you look into it?

Jintin commented 7 years ago

Thank you for this feedback

saagarjha commented 7 years ago

Another issue with spacing: operator functions. For example:

func ==(lhs: Foo, rhs: Foo) -> Bool {
    return true
}

becomes

func == (lhs: Foo, rhs: Foo) -> Bool {
    return true
}
Jintin commented 7 years ago

It is a hard one, I'll try to find out.

saagarjha commented 7 years ago

While we're at it,

infix operator ?! : NilCoalescingPrecedence

becomes

infix operator ? ! : NilCoalescingPrecedence

which breaks code.

Jintin commented 7 years ago

Is ?! your custom operator or general library usage?

You can modify SwiftParser.swift and add ("?!", 2) to line 27 "?" : [] to become "?" : [("?!", 2)], than everything should works.

And I think this style is more consistence.

infix operator ?!: NilCoalescingPrecedence

Also found from Apple website,https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html

infix operator +-: AdditionPrecedence
extension Vector2D {
    static func +- (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x + right.x, y: left.y - right.y)
    }
}
let firstVector = Vector2D(x: 1.0, y: 2.0)
let secondVector = Vector2D(x: 3.0, y: 4.0)
let plusMinusVector = firstVector +- secondVector
// plusMinusVector is a Vector2D instance with values of (4.0, -2.0)
saagarjha commented 7 years ago

Yes, it's a custom operator similar to the Nil Coalescing operator ??.

As for the formatting of operators, I would tend to agree with you, except that Swift seems to put a space between operators and the colon–I think the one on Apple's website just hasn't been updated.

In addition, the example I gave above of

func == (lhs: Foo, rhs: Foo) -> Bool {
    return true
}

appears to be correct behavior (both Swift and The Swift Programming Language use this formatting); please keep this as-is.

Finally, I did see your workaround, and I'll be using it, but I think we'll have to eventually come up with a better method for custom operators since they can incorporate basically any symbol characters. Maybe we could search for operator and turn off the formatter temporarily?

Jintin commented 7 years ago

Yes, the function style should add one space. I'll keep working on it.

And for the custom operator, I do have some plan about it. We should allow user to add, modify like preference.

Jintin commented 7 years ago

The space issue is fix, we can keep going on in #112 with the custom operator.