Skip to content

Commit

Permalink
RedundantBraces: remove inner braces, not outer
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Meltzer authored and kitbellew committed Feb 18, 2024
1 parent 178e15e commit 5046b8d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ class FormatWriter(formatOps: FormatOps) {
b.parent.exists(_.tokens.last.start == rb.start)
case f: Term.FunctionTerm =>
checkApply(f) && RedundantBraces.canRewriteFuncWithParens(f)
case t @ TreeOps.SingleArgInBraces(arg) =>
TreeOps.isParentAnApply(t) &&
RedundantBraces.canRewriteStatWithParens(arg)
case _ => false
}
if (ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,12 @@ class Router(formatOps: FormatOps) {
else Some(false)
(expire, arrow.map(_.left), 0, nlOnly)
case Some(t: Case) if t.cond.isEmpty && (leftOwner match {
case Term.PartialFunction(`t` :: Nil) => true
case x: Term.PartialFunction =>
isSingleElement(x.cases, t)
case x: Term.Match =>
isSingleElement(x.cases, t) && getMatchDot(x).isDefined
case _ => false
case _: Term.Try => false
case _ => tokens.tokenAfter(t).right eq close
}) =>
val arrow = getCaseArrow(t)
val nlOnly =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,29 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
onLeftBrace(ft.meta.rightOwner)
}

@tailrec
private def onLeftBrace(owner: Tree)(implicit
ft: FormatToken,
session: Session,
style: ScalafmtConfig
): Replacement = {
owner match {
case t: Term.ArgClause =>
t.values match {
case arg :: Nil if t.pos.start == arg.pos.start => onLeftBrace(arg)
case _ => null
}
case t: Term.FunctionTerm if t.tokens.last.is[Token.RightBrace] =>
if (!okToRemoveFunctionInApplyOrInit(t)) null
else if (okToReplaceFunctionInSingleArgApply(t)) replaceWithLeftParen
else removeToken
case t: Term.PartialFunction if t.parent.exists { p =>
SingleArgInBraces.orBlock(p).contains(t) &&
t.pos.start != p.pos.start
} =>
removeToken
case t: Term.Block =>
if (getBlockNestedPartialFunction(t).isDefined) removeToken
else if (okToReplaceBlockInSingleArgApply(t)) replaceWithLeftParen
else if (processBlock(t)) removeToken
else null
t.parent match {
case Some(f: Term.FunctionTerm)
if okToReplaceFunctionInSingleArgApply(f) =>
replaceWithLeftParen
case _ =>
if (processBlock(t)) removeToken else null
}
case _: Term.Interpolate
if style.rewrite.redundantBraces.stringInterpolation &&
processInterpolation =>
Expand Down Expand Up @@ -239,15 +241,6 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
})
}

private def okToReplaceBlockInSingleArgApply(
b: Term.Block
)(implicit style: ScalafmtConfig): Boolean =
b.parent.exists {
case f: Term.FunctionTerm =>
okToReplaceFunctionInSingleArgApply(f)
case _ => false
}

private def okToReplaceFunctionInSingleArgApply(f: Term.FunctionTerm)(implicit
style: ScalafmtConfig
): Boolean =
Expand Down Expand Up @@ -346,7 +339,8 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
// Example: as.map { _.toString }
// Leave this alone for now.
// In future there should be an option to surround such expressions with parens instead of braces
isSeqMulti(t.values) && okToRemoveBlockWithinApply(b)
if (isSeqMulti(t.values)) okToRemoveBlockWithinApply(b)
else (t.pos.start != b.pos.start) && SingleArgInBraces.inBraces(t)

case d: Defn.Def =>
def disqualifiedByUnit =
Expand Down
24 changes: 18 additions & 6 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1135,12 +1135,24 @@ object a {
}
>>>
object a {
val foo = bar { case x => y }
val foo = bar { case x => y }
val foo = bar { case x => y }
val foo = bar.baz { case x => y }
val foo = bar.baz { case x => y }
val foo = bar.baz { case x => y }
val foo = bar { case x =>
y
}
val foo = bar { case x =>
y
}
val foo = bar { case x =>
y
}
val foo = bar.baz { case x =>
y
}
val foo = bar.baz { case x =>
y
}
val foo = bar.baz { case x =>
y
}
}
<<< init-only secondary ctors
class a(vi: Int, vs: String) {
Expand Down

0 comments on commit 5046b8d

Please sign in to comment.