v2.5.0
Major changes
Managed formatting modes
We are defining the newlines.source parameter (see https://scalameta.org/scalafmt/docs/configuration.html#newlinessource), which introduces managed, wholesale changes to how code is formatted. One of the options (keep) is intended to hand control over line breaks to the user, while the other two (fold and unfold) will in fact completely ignore user input.
These modes are new and will potentially have a few bugs here and there (especially keep), in addition to, possibly, causing mixed sentiment of the "this looks almost good, if only we could do this differently" type. That is expected and likely par for the course for an opinionated formatter that scalafmt was intended to be.
However, please submit your bugs and opinions via https://github.com/scalameta/scalafmt/issues/new or comment on existing ones, so that if there are ideas on how to modify the behaviour in a way that resonates with the community, without violating the constraints (for fold/unfold, user input must be ignored), we'll implement them.
Editions
Editions were envisioned as a way to allow formatter upgrades while preserving formatting behaviour, but this turned out to be substantially harder to do, so edition 2020-01 is the last one. Also, the same result can easily be accomplished by not upgrading the formatter.
File overrides
One can customize configuration to apply to a subset of files, based on a file pattern. Cases can include special formatting rules for sbt files, or others for test suites (for instance, using AvoidInfix rewrite for main code with an empty rewrite.neverInfix.excludeFilters while allowing special scalatest DSL for tests with rewrite.neverInfix.excludeFilters containing should etc.).
See https://scalameta.org/scalafmt/docs/configuration.html#fileoverride
Infix formatting
Formatting line breaks around infix expressions turned out to be a difficult problem, and a couple of years ago scalafmt gave up on that and let the user choose where to break. Here, we are definining a couple of parameters to try and take that control back, by adding newlines after an infix operator (as the only universally safe place; there are some cases when a break can happen before an infix operator, but those are rare, and this logic doesn't attempt to discern them).
See https://scalameta.org/scalafmt/docs/configuration.html#newlinesafterinfix
Presets
This is not a new setting but a new, more general way to specify groups of parameters. Previously, you'd use style = defaultWithAlign or align = most or danglingParentheses = true. However, because configuration is represented using HOCON/json, doing it like that meant one couldn't partially override these presets.
The new approach is to define preset = defaultWithAlign, or align.preset = most, or danglingParentheses.preset = true. This way, naming is standardized, and partial overrides are possible.
Please see https://scalameta.org/scalafmt/docs/configuration.html#presets.
Alignment
The major additions, apart from several bug fixes, are
- right-aligning of infix operators
- ability to align lines with different number of columns
- ability to disable alignment within strip-margin multi-line strings; please see https://scalameta.org/scalafmt/docs/configuration.html#alignstripmargin
- ability to align multi-line blocks (previously, only consecutive, single-line statements we considered); please see https://scalameta.org/scalafmt/docs/configuration.html#alignmultiline.
Formatting around implicit and dangling
This was handled in fixed way in classic formatting mode while had some control in verticalMultiline mode. We brought those styles together, by adding a few parameters to control the outcome.
Please see:
- https://scalameta.org/scalafmt/docs/configuration.html#newlines-around-implicit-parameter-list-modifier
- https://scalameta.org/scalafmt/docs/configuration.html#danglingparentheses (
excludeandctrlSiteare new)
Indentation
We added several new options to control indentation:
- within constructors:
continuationIndent.ctorSite - in
withexpressions following anextends:continuationIndent.withSiteRelativeToExtends
Please see https://scalameta.org/scalafmt/docs/configuration.html#indentation - indentation of case clauses has been changed to be more consistent, no parameter available
Top-level statements
In addition to enforcing breaks before top-level statements, we now allow the same after the top-level statements. Also, there's a more granular control of which top-level statements are considered. Please see https://scalameta.org/scalafmt/docs/configuration.html#newlinestoplevelstatements
Rewrites
- trailing commas: a new
multipleparameter (see https://scalameta.org/scalafmt/docs/configuration.html#trailing-commas) - multiple bugfixes for
AvoidInfix RedundantParensexpanded to more cases
Literals
A number of new parameters is available to control formatting of numeric literals. Please see https://scalameta.org/scalafmt/docs/configuration.html#literals.
Defaults
The following are changes in default behaviour:
newlines.afterCurlyLambda = never: the parameter itself doesn't change, but its behaviour changed since 2.4.2 (a newsquashsetting mimics the oldnever); see https://scalameta.org/scalafmt/docs/configuration.html#newlinesaftercurlylambdarewrite.redundantBraces.generalExpressions = true; previously, it wasfalsealign.multiline = true: this new setting modifies the behaviour ofalign.preset = mostdanglingParentheses.ctrlSite = true: this is a new setting; set tofalseto get previous behaviour; see https://scalameta.org/scalafmt/docs/configuration.html#danglingparenthesesctrlsitenewlines.alwaysBeforeMultilineDef: there is a change in behaviour due to a bugfix, making this option apply consistently.
Pull Requests since v2.5.0-RC3
- FormatWriter: optionally support multiline align (#1934) @kitbellew
- FormatWriter: no arbitrary depth to match align (#1933) @kitbellew
- Self-formatting with 2.5.0-RC3 (#1932) @poslegm
- Update sbt-scalafmt to 2.3.4 (#1928) @scala-steward
- Update scalameta, testkit to 4.3.10 (#1930) @scala-steward
- Update sbt to 1.3.10 (#1927) @scala-steward
- Update scalatags to 0.9.0 (#1926) @scala-steward
- Update sbt-ci-release to 1.5.3 (#1925) @scala-steward
- Documentation: move alwaysBeforeMultilineDef (#1931) @kitbellew
- State bugfix: filter new indents the same as old (#1922) @kitbellew
- Documentation: add newlines.alwaysBeforeMultilineDef (#1923) @ljwagerfield
- Documentation: add version to newer parameters (#1920) @kitbellew