Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantBraces: remove braces around partial func #2705

Merged
merged 2 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,31 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
): Replacement = {
val rt = ft.right
val rtOwner = ft.meta.rightOwner
val ok = okToReplaceFunctionInSingleArgApply(rtOwner).exists {
case (lp, func) => lp.eq(rt) && func.tokens.last.is[Token.RightBrace]
def lpFunction = okToReplaceFunctionInSingleArgApply(rtOwner).map {
case (lp, f) if lp.eq(rt) && f.tokens.last.is[Token.RightBrace] =>
replaceToken("{", Some(rtOwner)) {
new Token.LeftBrace(rt.input, rt.dialect, rt.start)
}
case _ => null
}
if (ok) replaceToken("{", Some(rtOwner)) {
new Token.LeftBrace(rt.input, rt.dialect, rt.start)
// single-arg apply of a partial function
// a({ case b => c; d }) change to a { case b => c; d }
def lpPartialFunction = rtOwner match {
case ta @ Term.Apply(_, List(arg)) =>
getOpeningParen(ta).map { lp =>
val ko = lp.ne(rt) || getBlockNestedPartialFunction(arg).isEmpty
if (ko) null else removeToken
}
case _ => None
}
else null

lpFunction.orElse(lpPartialFunction).orNull
}

private def onRightParen(
left: Replacement
)(implicit ft: FormatToken): (Replacement, Replacement) =
if (left.exists(_.right.is[Token.LeftBrace])) (left, removeToken)
else null
(left, removeToken)

private def onLeftBrace(implicit
ft: FormatToken,
Expand All @@ -114,7 +125,8 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
else if (okToReplaceFunctionInSingleArgApply(t)) replace
else removeToken
case t: Term.Block =>
if (okToReplaceBlockInSingleArgApply(t)) replace
if (getBlockNestedPartialFunction(t).isDefined) removeToken
else if (okToReplaceBlockInSingleArgApply(t)) replace
else if (processBlock(t)) removeToken
else null
case _: Term.Interpolate =>
Expand Down Expand Up @@ -410,6 +422,23 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
case _ => okIfMultipleStats
})

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 _ => None
}

private def getSingleStatIfLineSpanOk(b: Term.Block)(implicit
style: ScalafmtConfig
): Option[Stat] =
Expand Down
60 changes: 60 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,63 @@ object a {
y
}
}
<<< single-block partial function with parens one-arg apply
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 } } }
)
}
>>>
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 }
}
<<< single-block partial function with block one-arg apply
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 } } }
}
}
>>>
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 }
}