Skip to content

Commit

Permalink
Router: keep NL policy before if guard if keep
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 16, 2024
1 parent ee0ed1e commit 459d3bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2237,11 +2237,11 @@ class Router(formatOps: FormatOps) {
Seq(Split(mod, 0, policy = policy))

case FormatToken(_, cond: T.KwIf, _) if rightOwner.is[Case] =>
if (style.newlines.keepBreak(newlines)) Seq(Split(Newline, 0))
else {
val arrow = getCaseArrow(rightOwner.asInstanceOf[Case]).left
val afterIf = nextNonCommentSameLineAfter(ft)
val noSplit =
val arrow = getCaseArrow(rightOwner.asInstanceOf[Case]).left
val noSplit =
if (style.newlines.keepBreak(newlines)) Split.ignored
else {
val afterIf = nextNonCommentSameLineAfter(ft)
if (style.newlines.keepBreak(afterIf)) {
val indent = Indent(style.indent.main, arrow, ExpiresOn.Before)
Split(Space, 0).withSingleLine(afterIf.left).withIndent(indent)
Expand All @@ -2253,11 +2253,11 @@ class Router(formatOps: FormatOps) {
recurseOnly = !exclude.isEmpty,
)
}
Seq(
noSplit,
Split(Newline, 1).withPolicy(penalizeNewlineByNesting(cond, arrow)),
)
}
}
Seq(
noSplit,
Split(Newline, 1).withPolicy(penalizeNewlineByNesting(cond, arrow)),
)

case FormatToken(_: T.KwIf, _, _) if leftOwner.is[Case] =>
val useNL = style.newlines.keepBreak(nextNonCommentSameLine(ft))
Expand Down
30 changes: 22 additions & 8 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9117,11 +9117,25 @@ trait Infer {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
case _: SuperType
- if !sym.isConstructor && !owntype.isInstanceOf[
- OverloadedType
- ] =>
+ if !sym.isConstructor && !owntype.isInstanceOf[OverloadedType] =>
owntype map ((tp: Type) =>
trait Infer {
class Inferencer {
def checkAccessible() = {
if (sym.isError)
tree setSymbol sym setType ErrorType
else accessible match {
case sym =>
tree setSymbol sym1 setType (
pre match {
// OPT: avoid lambda allocation and Type.map for super constructor calls
case _: SuperType
if !sym.isConstructor && !owntype.isInstanceOf[OverloadedType] =>
owntype map ((tp: Type) =>
if (tp eq pre) site.symbol.thisType else tp
)
case _ => owntype
}
)
}
}
}
}

0 comments on commit 459d3bc

Please sign in to comment.