Skip to content

Commit

Permalink
RedundantBraces: cosmetic, minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 15, 2024
1 parent 2559975 commit f4bf129
Showing 1 changed file with 6 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
style: ScalafmtConfig,
): (Replacement, Replacement) = left.how match {
case ReplacementType.Remove =>
val resOpt = getRightBraceBeforeRightParen(false).map { rb =>
getRightBraceBeforeRightParen(shouldBeRemoved = false).map { rb =>
ft.meta.rightOwner match {
case ac: Term.ArgClause => ftoks.matchingOpt(rb.left).map(ftoks.prev)
.foreach { lb =>
Expand All @@ -178,15 +178,14 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case _ =>
}
(left, removeToken)
}
resOpt.orNull
}.orNull

case ReplacementType.Replace if {
val lft = left.ft
val ro = ft.meta.rightOwner
(lft.meta.rightOwner eq ro) && lft.right.is[Token.LeftBrace]
} =>
val pftOpt = getRightBraceBeforeRightParen(true)
val pftOpt = getRightBraceBeforeRightParen(shouldBeRemoved = true)
def replaceIfAfterRightBrace = pftOpt.map { pft =>
val rb = pft.left
// move right to the end of the function
Expand Down Expand Up @@ -456,7 +455,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
checkBlockAsBody(b, d.body, noParams = d.paramClauseGroup.isEmpty)

case p: Term.FunctionTerm if isFunctionWithBraces(p) =>
okToRemoveAroundFunctionBody(b, true)
okToRemoveAroundFunctionBody(b, okIfMultipleStats = true)

case _: Term.If => settings.ifElseExpressions &&
shouldRemoveSingleStatBlock(b)
Expand Down Expand Up @@ -607,20 +606,12 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case _ => okIfMultipleStats
})

@tailrec
private def getBlockNestedPartialFunction(
tree: Tree,
): Option[Term.PartialFunction] = tree match {
case x: Term.PartialFunction => Some(x)
case x: Term.Block => getBlockNestedPartialFunction(x)
case _ => None
}

@tailrec
private def getBlockNestedPartialFunction(
tree: Term.Block,
): Option[Term.PartialFunction] = getBlockSingleStat(tree) match {
case Some(x: Term.PartialFunction) => Some(x)
case Some(x: Term.Block) => getBlockNestedPartialFunction(x)
case Term.Block(x :: Nil) => getBlockNestedPartialFunction(x)
case _ => None
}

Expand Down

0 comments on commit f4bf129

Please sign in to comment.