From 607b8148ebd01ca5e7185851d9ef86b92645eabd Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 5 May 2024 06:29:41 -0700 Subject: [PATCH] Config-style: control over forcing vs optimization Currently, the `runner.optimizer` parameters always force config-style formatting when `newlines.configStyle.xxxSite.prefer` is true, even though they were intended primarily to affect the route search. On the other hand, there could be cases when a user wouldn't enable the `newlines.configStyle.xxxSite.prefer` flag for regular operation (for instance, it makes no sense for `newlines.source=fold/unfold`) but would still be interested in forcing config-style. Therefore, let's add the sibling `xxxSite.forceConfigStyleIfOptimized` flag to decide when to force. --- docs/configuration.md | 8 ++++++-- .../src/main/scala/org/scalafmt/config/Newlines.scala | 8 +++++++- .../src/main/scala/org/scalafmt/internal/FormatOps.scala | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 917dc7f614..cc4221c56c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2821,12 +2821,16 @@ object a { ### Forcing config style -When `optIn.configStyleArguments` is enabled and +When `newlines.configStyleXxxSite.forceIfOptimized` is enabled and [call-site clause optimization](#route-search-optimizations-call-site-clause) is applicable to a clause, the formatter will force config-style formatting. +By default, takes the same value as +[`newlines.configStyleXxxSite.prefer`](#newlinesconfigstylexxxsiteprefer). + ```scala mdoc:scalafmt -optIn.configStyleArguments = true +newlines.configStyleCallSite.forceIfOptimized = true +newlines.configStyleDefnSite.forceIfOptimized = false runner.optimizer.forceConfigStyleMinSpan = 5 runner.optimizer.forceConfigStyleMinArgCount = 2 maxColumn = 60 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala index b6994cc81a..53d6c8b50d 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala @@ -545,7 +545,13 @@ object Newlines { * closing `)`. If true, preserves the newlines and keeps one line per * argument. */ - case class ConfigStyleElement(prefer: Boolean = true) + case class ConfigStyleElement( + prefer: Boolean = true, + private val forceIfOptimized: Option[Boolean] = None, + ) { + @inline + def getForceIfOptimized: Boolean = forceIfOptimized.getOrElse(prefer) + } private[config] object ConfigStyleElement { private val default = ConfigStyleElement() implicit val surface: Surface[ConfigStyleElement] = generic diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index 701deb9e99..6717f27b16 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -871,7 +871,7 @@ class FormatOps( def mustForceConfigStyle(ft: FormatToken)(implicit cfg: Newlines.ConfigStyleElement, - ): Boolean = cfg.prefer && forceConfigStyle(ft.meta.idx) + ): Boolean = cfg.getForceIfOptimized && forceConfigStyle(ft.meta.idx) def preserveConfigStyle( ft: FormatToken,