Where to go for highest quality opinionated swift formatting tool (that works well with xcode)

Is there a quality swift analogue for javascript's prettier?

I did some googling and am finding a bunch of highly configurable tools -- and configurability is 100% what I don't want ... I'm looking for the swift formatting tool that is most philosophically similar to javascript's prettier (https://prettier.io) -- (nearly) no config options, just good defaults.

I don't want to ever spend any time discussing how the code formatter should be configured or resolving configuration differences across developer machines -- or at least take steps which actively keep such disagreements to an absolute minimum ...

Its my experience with similar tools in various languages (prettier, rufo vs rubocop for ruby) that NOT having configuration options is a massive engineering win -- so I tend to look for the maximally opinionated code formatter immediately when adopting tools for a new language ecosystem as they tend to produce the best developer experiences (by a large margin).

As I understand it, not allowing extensive configurability options allows the formatter to make much better decisions overall with much higher reliability as appropriate heuristics can be built to handle all manner of scenarios that would otherwise be impossible to solve for when also trying to satisfy all constraints associated with all possible configuration options.

I won't attempt to adjudicate a comparison of them, but the two most commonly used tools that I've seen are swiftformat and swiftlint (there's a third, swift-format, although I don't know of folks tending to use that one over the previous two). Both provide integrations to "auto-fix", a large selection of customizable choices, and not-unreasonable defaults.

I leave any "best of..." comparison for you to judge for your own use case. Both swift format and SwiftLint provide notable value "out of the box".

1 Like

SwiftLint is not primarily a formatter. It’s formatting is largely limited to correcting the issues it finds as a linter. SwiftFormat is really the best out there at the moment.

2 Likes

It should be noted that swift-format is the official one and will eventually be included in the Swift toolchain when it is more complete.

2 Likes

In Xcode:

Cmd-A
Ctrl-I

Is this not good enough?

Thank you for the replies!

SwiftFormat/SwiftLint seem to expose quite a few configuration knobs ... I'm not sure specifically what all those knobs are for -- but I'd be worried that some of those would be pretty unlikely to compose well if actually customized (in terms of producing code scenarios that look obviously poor after formatting) -- and just having all those options can mean more effort required to correctly synchronize the team onto the same rulesets ... -- https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md

It should be noted that swift-format is the official one and will > eventually be included in the Swift toolchain when it is more complete.

swift-format seems promising with only a small set of config options -- https://github.com/apple/swift-format/blob/main/Documentation/Configuration.md. Its not super obvious what the project goals are though -- I hope the philosophy driving that effort is solidified enough that it won't just grow an infinite set of poorly justified config knobs over time ... I hope that project is able to rally consensus as controversy arises rather than just growing config knobs on every disagreement ... Anybody know how complete/not complete swift-format is - is it considered ready to use?

Cmd-A
Ctrl-I

Is this not good enough?

IMO -- definitely not. All that does is adjust the leading spaces on the line, doesn't do anything to correct or normalize formatting choices made within the line ... Good formatter allows you to type whatever is easiest to type and then just auto-format that it into a 'good enough or best' format for readability while also minimizing diff noise and helping to reduce non-semantic version control conflicts ...

3 Likes

If you don’t care about configuration, don’t use it. All of the tools have reasonable defaults.

4 Likes

Hey! Do you have any sources about swift-format being included in Swift toolchain? Since I failed to find any sources.

It was the whole point of SE‐0250 and much work was done toward that goal (which you can see from PRs such as this one). Whether or not that is still the plan years later is a question you would have to ask its development team. @allevato, @ahoppen?

swift-format is included in the open source Swift toolchains that can be downloaded from Swift.org - Download Swift. It is currently not included in Xcode.

2 Likes

As of Xcode 16, swift-format is included: GitHub - swiftlang/swift-format: Formatting technology for Swift source code

1 Like

My current two main issues are:

  1. It's not clear how to easily enable "format file on save" like we can with prettier in VSCode. (it's literally just 1. install prettier extension 2. check "format on save")
    I have set up a 3rd party app to execute Xcode 16's Format File with 'swift-format' after hitting ⌘S, but ideally this is just a checkbox in Xcode settings.
  2. Adding a .swift-format file to an Xcode workspace root, doesn't seem to be picked up at all by Format File with 'swift-format' in Xcode 16. :thinking:
    This is necessary to keep the entire team on the same formatting rules. There's not much information out there on how to best share make sure that the entire team's Xcode uses the same swift-format rules.

As a side-note, I feel like SwiftFormat is not very opinionated compared to Prettier.

Is there a SwiftFormat ruleset that's highly opinionated, disallowing any personal "touch"?
(eg. adding a linebreak here or there to influence the formatter is in most cases a bad thing in my opinion, because it forces us back into the mindset of trying to make it look pretty.)

1 Like

You can make swift-format much more opinionated about line breaks by setting respectsExistingLineBreaks to false in your configuration. That will do as it says, ignoring most user-provided line breaks and only breaking where it deems necessary. The other configuration settings can be used to influence where it makes those decisions.

Thanks a lot allevato! Even if I change .swift-format in my Xcode workspace, it won't apply to the Format File with 'swift-format' menu option in Xcode.

Do you know how to change the swift format rules for the Xcode built-in Format File with 'swift-format' menu option? Or will this always only ever use the swift format default rules?

2 Likes