diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 4d0358d80e..14f371b8dc 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -1661,12 +1661,6 @@ class Router(formatOps: FormatOps) { // This policy will apply to both the space and newline splits, otherwise // the newline is too cheap even it doesn't actually prevent other newlines. val penalizeBreaks = PenalizeAllNewlines(chainExpire, 2) - def slbPolicy(implicit fileLine: FileLine) = { - val exclude = // allow newlines in final {} block - if (chainExpire.is[T.RightBrace]) parensTuple(chainExpire) - else TokenRanges.empty - SingleLineBlock(chainExpire, exclude, noSyntaxNL = true) - } val newlinePolicy = breakOnNextDot & penalizeBreaks val ignoreNoSplit = nlOnly || hasBreak && @@ -1697,8 +1691,18 @@ class Router(formatOps: FormatOps) { if (style.optIn.breaksInsideChains) NoSplit.orNL(noBreak) else nlMod }.withPolicy(newlinePolicy).onlyFor(SplitTag.SelectChainSecondNL) - val slbSplit = Split(ignoreNoSplit, 0)(NoSplit) - .withPolicy(slbPolicy, prevChain).andPolicy(penalizeBreaks) + val slbSplit = + if (ignoreNoSplit) Split.ignored + else { + val noSplit = Split(NoSplit, 0) + if (prevChain) noSplit + else chainExpire match { // allow newlines in final {} block + case x: T.RightBrace => noSplit + .withSingleLine(matching(x), noSyntaxNL = true) + case x => noSplit + .withSingleLineNoOptimal(x, noSyntaxNL = true) + } + }.andPolicy(penalizeBreaks) val nlSplit = Split(if (ignoreNoSplit) Newline else nlMod, nlCost) .withPolicy(newlinePolicy) Seq(legacySplit, slbSplit, nlSplit) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Split.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Split.scala index a446dd50d6..f6a2f6c3f4 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Split.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Split.scala @@ -183,8 +183,9 @@ case class Split( killOnFail: Boolean = false, rank: Int = 0, extend: Boolean = false, + ignore: Boolean = false, )(implicit fileLine: FileLine, style: ScalafmtConfig): Split = - if (isIgnored) this + if (isIgnored || ignore) this else { val expire = fexpire withSingleLineAndOptimal( diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat index c1d272b5f9..cfd8491a49 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat @@ -5938,6 +5938,84 @@ object a { "only in hashbang classpath" ) } +<<< complex code with selects and brace lambda 1 +maxColumn = 81 +=== +object a: + private val packageIndex: scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection.mutable.AnyRefMap[String, collection.mutable.ListBuffer[Path]]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease.foreach(root => Files.walk(root).iterator().asScala.filter(Files.isDirectory(_)).foreach { p => + val moduleNamePathElementCount = if (isJava12OrHigher) 1 else 0 + if (p.getNameCount > root.getNameCount + moduleNamePathElementCount) { + val packageDotted = p.subpath(moduleNamePathElementCount + root.getNameCount, p.getNameCount).toString.replace('/', '.') + index.getOrElseUpdate(packageDotted, new collection.mutable.ListBuffer) += p + } + }) + index + } +>>> +object a: + private val packageIndex + : scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection.mutable + .AnyRefMap[String, collection.mutable.ListBuffer[Path]]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease + .foreach(root => + Files + .walk(root) + .iterator() + .asScala + .filter(Files.isDirectory(_)) + .foreach { p => + val moduleNamePathElementCount = if (isJava12OrHigher) 1 else 0 + if ( + p.getNameCount > root.getNameCount + moduleNamePathElementCount + ) { + val packageDotted = p + .subpath( + moduleNamePathElementCount + root.getNameCount, + p.getNameCount + ) + .toString + .replace('/', '.') + index.getOrElseUpdate( + packageDotted, + new collection.mutable.ListBuffer + ) += p + } + } + ) + index + } +<<< complex code with selects and brace lambda 2 +maxColumn = 79 +=== +object a: + rootsForRelease_foreach(root => Files.walkingIterator().useScalaFilter(Files__Is__Directory).foreach { p => + val_moduleNamePathElementCount_е_0 + if (p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate(packageDotted__new_collectionMutableListBuffer) + } + }) +>>> +object a: + rootsForRelease_foreach(root => + Files.walkingIterator().useScalaFilter(Files__Is__Directory).foreach { + p => + val_moduleNamePathElementCount_е_0 + if ( + p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount + ) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate( + packageDotted__new_collectionMutableListBuffer + ) + } + } + ) <<< braces to parens in one-line apply: overflow with braces, fit with parens maxColumn = 32 rewrite.rules = [RedundantBraces] diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat index 50ab761bb2..d411aea762 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat @@ -5681,6 +5681,76 @@ object a { "only in hashbang classpath" ) } +<<< complex code with selects and brace lambda 1 +maxColumn = 81 +=== +object a: + private val packageIndex: scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection.mutable.AnyRefMap[String, collection.mutable.ListBuffer[Path]]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease.foreach(root => Files.walk(root).iterator().asScala.filter(Files.isDirectory(_)).foreach { p => + val moduleNamePathElementCount = if (isJava12OrHigher) 1 else 0 + if (p.getNameCount > root.getNameCount + moduleNamePathElementCount) { + val packageDotted = p.subpath(moduleNamePathElementCount + root.getNameCount, p.getNameCount).toString.replace('/', '.') + index.getOrElseUpdate(packageDotted, new collection.mutable.ListBuffer) += p + } + }) + index + } +>>> +object a: + private val packageIndex + : scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection.mutable + .AnyRefMap[String, collection.mutable.ListBuffer[Path]]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease.foreach(root => + Files.walk(root).iterator().asScala.filter(Files.isDirectory(_)).foreach { + p => + val moduleNamePathElementCount = if (isJava12OrHigher) 1 else 0 + if ( + p.getNameCount > root.getNameCount + moduleNamePathElementCount + ) { + val packageDotted = p.subpath( + moduleNamePathElementCount + root.getNameCount, + p.getNameCount + ).toString.replace('/', '.') + index.getOrElseUpdate( + packageDotted, + new collection.mutable.ListBuffer + ) += p + } + } + ) + index + } +<<< complex code with selects and brace lambda 2 +maxColumn = 79 +=== +object a: + rootsForRelease_foreach(root => Files.walkingIterator().useScalaFilter(Files__Is__Directory).foreach { p => + val_moduleNamePathElementCount_е_0 + if (p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate(packageDotted__new_collectionMutableListBuffer) + } + }) +>>> +object a: + rootsForRelease_foreach(root => + Files.walkingIterator().useScalaFilter(Files__Is__Directory).foreach { + p => + val_moduleNamePathElementCount_е_0 + if ( + p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount + ) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate( + packageDotted__new_collectionMutableListBuffer + ) + } + } + ) <<< braces to parens in one-line apply: overflow with braces, fit with parens maxColumn = 32 rewrite.rules = [RedundantBraces] diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat index c2956e6fa8..d5a32495b0 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat @@ -5976,6 +5976,78 @@ object a { "only in hashbang classpath" ) } +<<< complex code with selects and brace lambda 1 +maxColumn = 81 +=== +object a: + private val packageIndex: scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection.mutable.AnyRefMap[String, collection.mutable.ListBuffer[Path]]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease.foreach(root => Files.walk(root).iterator().asScala.filter(Files.isDirectory(_)).foreach { p => + val moduleNamePathElementCount = if (isJava12OrHigher) 1 else 0 + if (p.getNameCount > root.getNameCount + moduleNamePathElementCount) { + val packageDotted = p.subpath(moduleNamePathElementCount + root.getNameCount, p.getNameCount).toString.replace('/', '.') + index.getOrElseUpdate(packageDotted, new collection.mutable.ListBuffer) += p + } + }) + index + } +>>> +object a: + private val packageIndex + : scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection.mutable.AnyRefMap[ + String, + collection.mutable.ListBuffer[Path] + ]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease.foreach(root => + Files.walk(root).iterator().asScala.filter(Files.isDirectory(_)).foreach { + p => + val moduleNamePathElementCount = if (isJava12OrHigher) 1 else 0 + if ( + p.getNameCount > root.getNameCount + moduleNamePathElementCount + ) { + val packageDotted = p.subpath( + moduleNamePathElementCount + root.getNameCount, + p.getNameCount + ).toString.replace('/', '.') + index.getOrElseUpdate( + packageDotted, + new collection.mutable.ListBuffer + ) += p + } + } + ) + index + } +<<< complex code with selects and brace lambda 2 +maxColumn = 79 +=== +object a: + rootsForRelease_foreach(root => Files.walkingIterator().useScalaFilter(Files__Is__Directory).foreach { p => + val_moduleNamePathElementCount_е_0 + if (p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate(packageDotted__new_collectionMutableListBuffer) + } + }) +>>> +object a: + rootsForRelease_foreach(root => + Files.walkingIterator().useScalaFilter(Files__Is__Directory).foreach { + p => + val_moduleNamePathElementCount_е_0 + if ( + p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount + ) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate( + packageDotted__new_collectionMutableListBuffer + ) + } + } + ) <<< braces to parens in one-line apply: overflow with braces, fit with parens maxColumn = 32 rewrite.rules = [RedundantBraces] diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat index b9fa72a93a..1fa883cd6c 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -6115,6 +6115,90 @@ object a { "only in hashbang classpath" ) } +<<< complex code with selects and brace lambda 1 +maxColumn = 81 +=== +object a: + private val packageIndex: scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection.mutable.AnyRefMap[String, collection.mutable.ListBuffer[Path]]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease.foreach(root => Files.walk(root).iterator().asScala.filter(Files.isDirectory(_)).foreach { p => + val moduleNamePathElementCount = if (isJava12OrHigher) 1 else 0 + if (p.getNameCount > root.getNameCount + moduleNamePathElementCount) { + val packageDotted = p.subpath(moduleNamePathElementCount + root.getNameCount, p.getNameCount).toString.replace('/', '.') + index.getOrElseUpdate(packageDotted, new collection.mutable.ListBuffer) += p + } + }) + index + } +>>> +object a: + private val packageIndex + : scala.collection.Map[String, scala.collection.Seq[Path]] = { + val index = collection + .mutable + .AnyRefMap[String, collection.mutable.ListBuffer[Path]]() + val isJava12OrHigher = scala.util.Properties.isJavaAtLeast("12") + rootsForRelease.foreach(root => + Files + .walk(root) + .iterator() + .asScala + .filter(Files.isDirectory(_)) + .foreach { p => + val moduleNamePathElementCount = + if (isJava12OrHigher) + 1 + else + 0 + if ( + p.getNameCount > root.getNameCount + moduleNamePathElementCount + ) { + val packageDotted = p + .subpath( + moduleNamePathElementCount + root.getNameCount, + p.getNameCount + ) + .toString + .replace('/', '.') + index.getOrElseUpdate( + packageDotted, + new collection.mutable.ListBuffer + ) += p + } + } + ) + index + } +<<< complex code with selects and brace lambda 2 +maxColumn = 79 +=== +object a: + rootsForRelease_foreach(root => Files.walkingIterator().useScalaFilter(Files__Is__Directory).foreach { p => + val_moduleNamePathElementCount_е_0 + if (p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate(packageDotted__new_collectionMutableListBuffer) + } + }) +>>> +object a: + rootsForRelease_foreach(root => + Files + .walkingIterator() + .useScalaFilter(Files__Is__Directory) + .foreach { p => + val_moduleNamePathElementCount_е_0 + if ( + p_getNameCount_g_root_getNameCount_p_moduleNamePathElementCount + ) { + val_packageDotted_e_p_subpath_moduleNamePathElementCountFooRoot__p__toString_replace + index_getOrElseUpdate( + packageDotted__new_collectionMutableListBuffer + ) + } + } + ) <<< braces to parens in one-line apply: overflow with braces, fit with parens maxColumn = 32 rewrite.rules = [RedundantBraces]