Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: use optimal token for brace-ending chains #4159

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
78 changes: 78 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
70 changes: 70 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
72 changes: 72 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading