Swiftformat: Support line length limits

Created on 18 Mar 2017  路  9Comments  路  Source: nicklockwood/SwiftFormat

Apologies if this has been requested this before, I couldn't find any issues on it.

In our style guide we limit the maximum number of columns on a line to 110. Would it make sense for SwiftFormat to be able to handle a value for this and format within those constraints?

Most helpful comment

It's certainly within scope for SwiftFormat. Tricky to implement, but I'll consider it.

All 9 comments

It's certainly within scope for SwiftFormat. Tricky to implement, but I'll consider it.

@nicklockwood I look forward to this feature!

Hi, I would like this feature too.
Just for consideration, clang-format uses a set of penalties to decide where to break the line eg PenaltyBreakAssignment, PenaltyBreakBeforeFirstCallParameter, etc..
An advantage I saw was that it can leave things that fit the column limit and others that are too long get wraparguments/wrapcollections
clang options link

any news on this?

bump

+1 for this, it seems like one of the most basic features for a formatter, and is clearly sorely wanted by many people. one thing it could maybe do is just call into swift-format's formatter with the line length requirement before applying its own rules. it might end up making the code longer than the requirement inadvertently due to other rules afterward, but at least it might be within a certain tolerance

Sorry for not responding sooner. I am still working on this, but it's much more complicated than you might imagine, especially in terms of how it interacts with other rules.

I don't have a lot of time to work on SwiftFormat these days (I'm not paid for it), and I'm essentially the only contributor, so as you may have noticed most recent updates have primarily been fixes rather than new features.

I've decided to not let perfection be the enemy of progress, so 0.41.0 includes some minimal support for line wrapping. Specifically, if you set a --maxwidth option, the wrapArguments rule will wrap collection literals and function arguments to fit that width if possible.

I'll expand the line-wrapping support to other statement types in future releases.

I've landed extended wrapping support to all statement types in version 0.42.0. A bunch of cases such as long comments or string literals won't be wrapped automatically, but for the most part I consider this feature to now be implemented.

Please open new issues for any bugs or enhancements.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cyberbeni picture Cyberbeni  路  4Comments

DagAgren picture DagAgren  路  4Comments

MaxDesiatov picture MaxDesiatov  路  4Comments

dcramps picture dcramps  路  3Comments

MaxDesiatov picture MaxDesiatov  路  3Comments