Swiftformat: Colon alignment for method arguments

Created on 22 Oct 2016  Â·  7Comments  Â·  Source: nicklockwood/SwiftFormat

I would like to see colon alignment for method parameters like this:

let result = self.method(param1: value1,
                         param2: value2,
                      param3333: value3333)

I'm not sure if this is the swift way, but I think it would make the code look cleaner like in Obj-C

Most helpful comment

I personally have come to prefer

let result = self.method(
    param1: value1,
    param2: value2,
    param3333: value3333
)

since Swift 3 unified the syntax by extracting the bar: from fooWithBar(42, …) to foo(bar: 42, …) on most of Apple's APIs.

The benefit is resilience against long variable/method names:

let itEvenWorksFlawlessly = self.withHighlyNestedMethodCalls(
    noReally: self.justLookForYourself(
        still: easilyReadable,
        noScrolling: necessary,
        now: self.thisIsGettingSilly(
            butYou: getThePointDontYou
        )
    )
)

vs.

let whileThis = self.becamePrettyMuch(unreadable: self.already(and: nestingCalls,
                                                               justAddsInsult: toInjury,
                                                               goodLuck: self.keepingThisInside(the: common,
                                                                                                column: limits)))

Colon-alignment was the best we had for Objective-C, but more than suboptimal with Swift 3+, imho.

All 7 comments

I personally have come to prefer

let result = self.method(
    param1: value1,
    param2: value2,
    param3333: value3333
)

since Swift 3 unified the syntax by extracting the bar: from fooWithBar(42, …) to foo(bar: 42, …) on most of Apple's APIs.

The benefit is resilience against long variable/method names:

let itEvenWorksFlawlessly = self.withHighlyNestedMethodCalls(
    noReally: self.justLookForYourself(
        still: easilyReadable,
        noScrolling: necessary,
        now: self.thisIsGettingSilly(
            butYou: getThePointDontYou
        )
    )
)

vs.

let whileThis = self.becamePrettyMuch(unreadable: self.already(and: nestingCalls,
                                                               justAddsInsult: toInjury,
                                                               goodLuck: self.keepingThisInside(the: common,
                                                                                                column: limits)))

Colon-alignment was the best we had for Objective-C, but more than suboptimal with Swift 3+, imho.

@regexident SwiftFormat fully supports that style, but it's up to you to insert the linebreaks.

@nicklockwood: I know. It was more meant as a "please don't make this the default, there's a much better alternative". :wink:

Of course no amount of formatting can make everything look pretty and it's always a matter of taste 😜
I wouldn't want this to be default since it is not used by Apple. I would make a fork just for myself if someone could point me to right direction, where to place such a formatter.

This is more difficult than I anticipated due to the way tabs work.

In Xcode, it has a concept of "tab width", e.g. a tab is equivalent to 4 spaces. Setting aside that this completely misses the point of using tabs, it makes it possible for Xcode to indent using a mix of tabs and spaces in order to align at a particular character.

In SwiftFormat, I don't have a concept of tab width - if you choose to indent using tabs, I have no way of knowing how many spaces a tab represents. While I can add additional spaces to a tabbed an indent, I can't substract spaces from it. That makes it impossible to align colons, because some of the method labels might need to be indented _less_ than the current overall indent for the method itself.

I guess I could support colon alignment only if you are using spaces for indenting though.

Ah, didn't actually think that tabs are preferred in swift. I've a "spaces" guy myself and work in a project where spaces are used instead of tabs. It'd be great to have it for spaces at least :)

@regexident SwiftFormat 0.19 can now enforce your preferred style using --wraparguments beforefirst

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cyberbeni picture Cyberbeni  Â·  4Comments

MaxDesiatov picture MaxDesiatov  Â·  4Comments

sebastianfrelle picture sebastianfrelle  Â·  4Comments

DavinAhn picture DavinAhn  Â·  4Comments

PompeiaPaetenari picture PompeiaPaetenari  Â·  4Comments