Skip to content

Commit

Permalink
Router: combine two case splits via policies
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Aug 10, 2024
1 parent 7296035 commit 7996a8a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2096,12 +2096,13 @@ class Router(formatOps: FormatOps) {
}
val arrow = arrowFt.left
val postArrowFt = nextNonCommentSameLine(arrowFt)
val postArrow = postArrowFt.left
val ownerEnd = getLastOpt(owner)
val expire = ownerEnd.fold(postArrowFt)(nextNonCommentSameLine).left

val bodyBlock = isCaseBodyABlock(arrowFt, owner)
def defaultPolicy = decideNewlinesOnlyAfterToken(postArrowFt.left)
val policy =
def defaultPolicy = decideNewlinesOnlyAfterToken(postArrow)
val postArrowPolicy =
if (bodyBlock || isAttachedCommentThenBreak(arrowFt)) NoPolicy
else if (isCaseBodyEnclosedAsBlock(postArrowFt, owner)) {
val postParenFt = nextNonCommentSameLineAfter(postArrowFt)
Expand Down Expand Up @@ -2135,12 +2136,23 @@ class Router(formatOps: FormatOps) {
Indent(arrowIndent, arrow, After),
)
val mod = ModExt(Space, indents)
val slbSplitOpt = (ownerEnd match {
val slbExpireOpt = ownerEnd match {
case None => Some(postArrowFt)
case Some(x) => prevNotTrailingComment(x).toOption
}).map(x => Split(mod, 0).withSingleLine(x.left, killOnFail = true))
val splits = Split(mod, 0, policy = policy) :: Nil
slbSplitOpt.fold(splits)(_ :: splits)
}
val policy = slbExpireOpt.fold(postArrowPolicy) { slbExpireFt =>
val slbExpire = slbExpireFt.left
val onArrowPolicy = Policy.End == arrow ==>
Policy.after(postArrow, "CASESLB>ARROW") { case Decision(_, ss) =>
ss.flatMap { s =>
val nonSlbSplit = s.andPolicy(postArrowPolicy)
if (s.isNL) Seq(nonSlbSplit)
else Seq(s.withSingleLine(slbExpire, extend = true), nonSlbSplit)
}
}
Policy.RelayOnSplit((s, _) => s.isNL)(onArrowPolicy, postArrowPolicy)
}
Seq(Split(mod, 0, policy = policy))

case FormatToken(_, cond: T.KwIf, _) if rightOwner.is[Case] =>
if (style.newlines.keepBreak(newlines)) Seq(Split(Newline, 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3752,7 +3752,7 @@ object a {
case None => Command.invalid
}
}
<<< ONLY complex match case triggering SLB precedence rule
<<< complex match case triggering SLB precedence rule
object a {
val breakpoints = (breaks.map {
case Break(point) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3437,7 +3437,7 @@ object a {
case None => Command.invalid
}
}
<<< ONLY complex match case triggering SLB precedence rule
<<< complex match case triggering SLB precedence rule
object a {
val breakpoints = (breaks.map {
case Break(point) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3556,7 +3556,7 @@ object a {
case None => Command.invalid
}
}
<<< ONLY complex match case triggering SLB precedence rule
<<< complex match case triggering SLB precedence rule
object a {
val breakpoints = (breaks.map {
case Break(point) =>
Expand Down
25 changes: 13 additions & 12 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3701,7 +3701,7 @@ object a {
Command.invalid
}
}
<<< ONLY complex match case triggering SLB precedence rule
<<< complex match case triggering SLB precedence rule
object a {
val breakpoints = (breaks.map {
case Break(point) =>
Expand All @@ -3713,17 +3713,18 @@ object a {
}).mkString("\n\n")
}
>>>
BestFirstSearch:289 Failed to format
UNABLE TO FORMAT,
tok #17/52: =>∙s: RightArrow [69..71) [LF] Interpolation.Id(s) [82..83)
policies:
NA:[Router:2096]>71d
(NB:[Router:229]@280d & NA:[Router:300]>71d)
SLB:[Router:2063]@281!d
NB:[Router:229]@300d
splits (before policy):
Newline:[Router:517](cost=0, indents=[], NoPolicy)
splits (after policy):
object a {
val breakpoints =
(
breaks.map { case Break(point) =>
s"""|send "stop at $MainObject$$:$point\\r"
|sleep $CommandWait
|expect "breakpoint $MainObject$$:$point"
|expect -re $$
""".stripMargin
}
).mkString("\n\n")
}
<<< #2137
pubSubMessage match {
case message: IngestionMessage => (
Expand Down

0 comments on commit 7996a8a

Please sign in to comment.