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: consistent if-else block format for fold #4581

Merged
merged 2 commits into from
Nov 22, 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 @@ -315,25 +315,6 @@ class Router(formatOps: FormatOps) {
val lambdaExpire =
if (lambdaArrow eq null) null else nextNonCommentSameLine(lambdaArrow)

def getSingleLinePolicy = {
val needBreak = close.right match {
case _: T.KwElse => close.rightOwner match {
case p: Term.If => p.thenp eq leftOwner
case _ => false
}
case _: T.KwCatch => close.rightOwner match {
case p: Term.TryClause => p.expr eq leftOwner
case _ => false
}
case _: T.KwFinally => close.rightOwner match {
case p: Term.TryClause => (p.expr eq leftOwner) ||
p.catchClause.contains(leftOwner)
case _ => false
}
case _ => false
}
Policy ? needBreak && decideNewlinesOnlyAfterClose(close)
}
def getClassicSingleLineDecisionOpt =
if (noBreak()) Some(true) else None

Expand Down Expand Up @@ -381,11 +362,28 @@ class Router(formatOps: FormatOps) {
val singleLineSplitOpt = {
if (slbParensExclude eq null) None else singleLineDecisionOpt
}.map { sld =>
val sldPolicy = getSingleLinePolicy
val ownerIfNeedBreakAfterClose = close.right match {
case _: T.KwElse => close.rightOwner match {
case p: Term.If if p.thenp eq leftOwner => Some(p)
case _ => None
}
case _: T.KwCatch => close.rightOwner match {
case p: Term.TryClause if p.expr eq leftOwner => Some(p)
case _ => None
}
case _: T.KwFinally => close.rightOwner match {
case p: Term.TryClause
if (p.expr eq leftOwner) ||
p.catchClause.contains(leftOwner) => Some(p)
case _ => None
}
case _ => None
}
val expire = leftOwner.parent match {
case Some(p: Term.ForYield)
if !sld && sldPolicy.isEmpty && style.newlines.fold &&
leftOwner.is[Term.EnumeratorsBlock] => getLast(p)
if !sld && ownerIfNeedBreakAfterClose.isEmpty &&
style.newlines.fold && leftOwner.is[Term.EnumeratorsBlock] =>
getLast(p)
case _ if style.newlines.isBeforeOpenParenCallSite => close
case _ => getSlbEndOnLeft(close)
}
Expand All @@ -404,6 +402,22 @@ class Router(formatOps: FormatOps) {
else Policy.RelayOnSplit((s, _) => s.isNL)(slbParensPolicy)(
Policy.onLeft(close, "BracesToParensFailed") { case _ => Nil },
)
val sldPolicy = ownerIfNeedBreakAfterClose.map { p =>
if (style.newlines.fold) {
val pend = getSlbEndOnLeft(getLast(p))
def pendSlb(s: Split) = s
.withSingleLine(pend, noSyntaxNL = true, extend = true)
Policy.End <= close ==>
Policy.onRight(close, s"RB-ELSE[${pend.idx}]") {
case Decision(`close`, ss) =>
if (ss.exists(_.isNL)) ss
.map(s => if (s.isNL) s else pendSlb(s))
else ss.flatMap { s =>
Seq(pendSlb(s), s.withMod(Newline).withPenalty(1))
}
}
} else decideNewlinesOnlyAfterClose(close)
}
Split(slbMod, 0).withSingleLine(
expire,
exclude = exclude,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class CommunityIntellijScalaSuite(name: String)
class CommunityIntellijScala_2024_2_Suite
extends CommunityIntellijScalaSuite("intellij-scala-2024.2") {

override protected def totalStatesVisited: Option[Int] = Some(57952816)
override protected def totalStatesVisited: Option[Int] = Some(57957808)

override protected def builds = Seq(getBuild(
"2024.2.28",
Expand Down Expand Up @@ -51,7 +51,7 @@ class CommunityIntellijScala_2024_2_Suite
class CommunityIntellijScala_2024_3_Suite
extends CommunityIntellijScalaSuite("intellij-scala-2024.3") {

override protected def totalStatesVisited: Option[Int] = Some(58169519)
override protected def totalStatesVisited: Option[Int] = Some(58174471)

override protected def builds = Seq(getBuild(
"2024.3.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class CommunityScala2Suite(name: String)

class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") {

override protected def totalStatesVisited: Option[Int] = Some(42556059)
override protected def totalStatesVisited: Option[Int] = Some(42560182)

override protected def builds =
Seq(getBuild("v2.12.20", dialects.Scala212, 1277))
Expand All @@ -18,7 +18,7 @@ class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") {

class CommunityScala2_13Suite extends CommunityScala2Suite("scala-2.13") {

override protected def totalStatesVisited: Option[Int] = Some(53056214)
override protected def totalStatesVisited: Option[Int] = Some(53060139)

override protected def builds =
Seq(getBuild("v2.13.14", dialects.Scala213, 1287))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ abstract class CommunityScala3Suite(name: String)

class CommunityScala3_2Suite extends CommunityScala3Suite("scala-3.2") {

override protected def totalStatesVisited: Option[Int] = Some(39245403)
override protected def totalStatesVisited: Option[Int] = Some(39246875)

override protected def builds = Seq(getBuild("3.2.2", dialects.Scala32, 791))

}

class CommunityScala3_3Suite extends CommunityScala3Suite("scala-3.3") {

override protected def totalStatesVisited: Option[Int] = Some(42431405)
override protected def totalStatesVisited: Option[Int] = Some(42432863)

override protected def builds = Seq(getBuild("3.3.3", dialects.Scala33, 861))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class CommunitySparkSuite(name: String)

class CommunitySpark3_4Suite extends CommunitySparkSuite("spark-3.4") {

override protected def totalStatesVisited: Option[Int] = Some(86350328)
override protected def totalStatesVisited: Option[Int] = Some(86399848)

override protected def builds =
Seq(getBuild("v3.4.1", dialects.Scala213, 2585))
Expand All @@ -18,7 +18,7 @@ class CommunitySpark3_4Suite extends CommunitySparkSuite("spark-3.4") {

class CommunitySpark3_5Suite extends CommunitySparkSuite("spark-3.5") {

override protected def totalStatesVisited: Option[Int] = Some(91352348)
override protected def totalStatesVisited: Option[Int] = Some(91404596)

override protected def builds =
Seq(getBuild("v3.5.3", dialects.Scala213, 2756))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11201,3 +11201,51 @@ extends SomeBaseClass
with SomeTrait
>>>
class SomeClass(a: Int, b: Double, c: String) extends SomeBaseClass with SomeTrait
<<< if-else with one block, short
maxColumn = 40
===
object a {
if (s.costWithPenalty > 0) { preFork = false; false } else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
>>>
object a {
if (s.costWithPenalty > 0) {
preFork = false; false
} else true

if (s.costWithPenalty <= 0) true
else { preFork = false; false }
}
<<< if-else with one block, medium
maxColumn = 60
===
object a {
if (s.costWithPenalty > 0) { preFork = false; false } else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
>>>
object a {
if (s.costWithPenalty > 0) { preFork = false; false }
else true

if (s.costWithPenalty <= 0) true
else { preFork = false; false }
}
<<< if-else with one block, long
maxColumn = 80
===
object a {
if (s.costWithPenalty > 0) { preFork = false; false } else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
>>>
object a {
if (s.costWithPenalty > 0) { preFork = false; false }
else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
91 changes: 59 additions & 32 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ println("bbb") } else c
}
>>>
object a {
if (a) { println("bbb") }
else c
if (a) { println("bbb") } else c
}
<<< 1.3: block, if, non-egyptian curlies
object a {
Expand All @@ -31,8 +30,7 @@ c
}}
>>>
object a {
if (a) { println("bbb") }
else { c }
if (a) { println("bbb") } else { c }
}
<<< 1.4: block #1043
object a {
Expand Down Expand Up @@ -252,12 +250,8 @@ object a {
>>>
object a {
val ok1 = if (a > 10) Some(a) else None
val ok2 =
if (a > 10) { Some(a) }
else { None }
val ok3 =
if (aaaa > 10000) { Some(aaaa) }
else { None }
val ok2 = if (a > 10) { Some(a) } else { None }
val ok3 = if (aaaa > 10000) { Some(aaaa) } else { None }
}
<<< 2.7 #1747: one line without else
maxColumn = 60
Expand Down Expand Up @@ -296,12 +290,8 @@ object a {
>>>
object a {
val ok1 = if (a > 10) Some(a) else None
val ok2 =
if (a > 10) { Some(a) }
else { None }
val ok3 =
if (aaaa > 10000) { Some(aaaa) }
else { None }
val ok2 = if (a > 10) { Some(a) } else { None }
val ok3 = if (aaaa > 10000) { Some(aaaa) } else { None }
}
<<< 2.9 #1747: split on else
maxColumn = 60
Expand All @@ -319,12 +309,8 @@ object a {
>>>
object a {
val ok1 = if (a > 10) Some(a) else None
val ok2 =
if (a > 10) { Some(a) }
else { None }
val ok3 =
if (aaaa > 10000) { Some(aaaa) }
else { None }
val ok2 = if (a > 10) { Some(a) } else { None }
val ok3 = if (aaaa > 10000) { Some(aaaa) } else { None }
}
<<< 2.10 #1747: split on then without else
maxColumn = 60
Expand Down Expand Up @@ -357,12 +343,8 @@ object a {
>>>
object a {
val ok1 = if (a > 10) Some(a) else None
val ok2 =
if (a > 10) { Some(a) }
else { None }
val ok3 =
if (aaaa > 10000) { Some(aaaa) }
else { None }
val ok2 = if (a > 10) { Some(a) } else { None }
val ok3 = if (aaaa > 10000) { Some(aaaa) } else { None }
}
<<< 2.12 #1747: split on if without else
maxColumn = 60
Expand Down Expand Up @@ -6008,8 +5990,7 @@ class Foo {
>>>
class Foo {
val foo =
if (true) { "" }
else { "a" }
if (true) { "" } else { "a" }
val foo = if (true) "" else "a"
val foo = if (true) "" else "a"
val foo =
Expand All @@ -6035,8 +6016,7 @@ class Foo() {
>>>
class Foo() {
def ok: Boolean =
if (1 == 1) { true }
else false
if (1 == 1) { true } else false

def notOK: Boolean =
if (1 == 1) {
Expand Down Expand Up @@ -10467,3 +10447,50 @@ extends SomeBaseClass
with SomeTrait
>>>
class SomeClass(a: Int, b: Double, c: String) extends SomeBaseClass with SomeTrait
<<< if-else with one block, short
maxColumn = 40
===
object a {
if (s.costWithPenalty > 0) { preFork = false; false } else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
>>>
object a {
if (s.costWithPenalty > 0) {
preFork = false; false
} else true

if (s.costWithPenalty <= 0) true
else { preFork = false; false }
}
<<< if-else with one block, medium
maxColumn = 60
===
object a {
if (s.costWithPenalty > 0) { preFork = false; false } else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
>>>
object a {
if (s.costWithPenalty > 0) { preFork = false; false }
else true

if (s.costWithPenalty <= 0) true
else { preFork = false; false }
}
<<< if-else with one block, long
maxColumn = 80
===
object a {
if (s.costWithPenalty > 0) { preFork = false; false } else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
>>>
object a {
if (s.costWithPenalty > 0) { preFork = false; false } else true

if (s.costWithPenalty <= 0) true else { preFork = false; false }
}
Loading