Skip to content

Commit

Permalink
Router: fold else only after non-empty block
Browse files Browse the repository at this point in the history
Check for empty blocks, or for non-blocks.
  • Loading branch information
kitbellew committed Oct 22, 2024
1 parent 38d52a2 commit 90adc91
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,14 @@ class FormatOps(
term: Term.If,
): Option[(FormatToken, Option[T.KwElse])] = getHeadOpt(term.elsep)
.map { ftElsep =>
val beforeElsep = prevNonCommentBefore(ftElsep)
val elsOpt = beforeElsep.left match {
case els: T.KwElse
if initStyle.newlines.alwaysBeforeElseAfterCurlyIf ||
!prev(beforeElsep).left.is[T.RightBrace] => Some(els)
val elsOpt = prevNonCommentBefore(ftElsep) match {
case ft @ FormatToken(els: T.KwElse, _, _) =>
val ok = initStyle.newlines.alwaysBeforeElseAfterCurlyIf || ! {
val pft = prev(ft)
pft.leftOwner.is[Term.Block] && pft.left.is[T.RightBrace] &&
!prevNonCommentSameLineBefore(pft).left.is[T.LeftBrace]
}
if (ok) Some(els) else None
case _ => None
}
(ftElsep, elsOpt)
Expand Down
3 changes: 2 additions & 1 deletion scalafmt-tests/shared/src/test/resources/default/String.stat
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ object a {
object a {
def b = {
try {
if (false) {} else {
if (false) {}
else {
info("Uber JAR disabled, but current working directory does not look " +
s"like an engine project directory. Please delete lib/${core.getName} manually.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ object a {
if (
template.isEmpty || hasModifiers || definition
.exists(it => it.contains(CASE) && !it.contains(OBJECT))
) {} else {
) {}
else {
if (
sb.length >= 2 && sb.substring(
sb.length - 2,
Expand All @@ -317,7 +318,8 @@ object a {
if (
template.isEmpty || hasModifiers || definition
.exists(it => it.contains(CASE) && !it.contains(OBJECT))
) {} else if (
) {}
else if (
sb.length >= 2 && sb.substring(
sb.length - 2,
sb.length()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1484614, "total explored")
assertEquals(explored, 1484618, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit 90adc91

Please sign in to comment.