Skip to content

Commit

Permalink
RedundantBraces: replace paren w/ brace if comment
Browse files Browse the repository at this point in the history
If we simply remove the parenthesis, then the comment will end up before
the brace, unlike originally. Let's replace instead.
  • Loading branch information
kitbellew committed Oct 16, 2024
1 parent d99f67a commit 0e49db1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ object FormatTokensRewrite {
def claimedRule(implicit ft: FormatToken): Option[Replacement] =
claimedRule(ft.meta.idx)

@inline
def claimedRuleOnLeft(ft: FormatToken): Option[Replacement] =
claimedRule(ft.meta.idx - 1)

@inline
private[rewrite] def claimedRule(ftIdx: Int): Option[Replacement] = claimed
.get(ftIdx).map(tokens.apply).filter(_ ne null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {
Some(rb).filter(canRewriteWithParensOnRightBrace)
else None

private[scalafmt] def isLeftParenReplacedWithBraceOnLeft(pft: FormatToken)(
implicit session: Session,
): Boolean = session.claimedRuleOnLeft(pft).exists { x =>
(x.how eq ReplacementType.Replace) && x.ft.right.is[Token.LeftBrace]
}

}

/** Removes/adds curly braces where desired.
Expand Down Expand Up @@ -128,9 +134,8 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case (`rt`, f) => f.body match {
case b: Term.Block => ftoks.getHead(b) match {
case FormatToken(_: Token.LeftBrace, _, lbm) =>
replaceToken("{", claim = lbm.idx - 1 :: Nil) {
new Token.LeftBrace(rt.input, rt.dialect, rt.start)
}
val lb = new Token.LeftBrace(rt.input, rt.dialect, rt.start)
replaceToken("{", claim = lbm.idx - 1 :: Nil)(lb)
case _ => null
}
case _ => null
Expand All @@ -144,8 +149,13 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
getOpeningParen(ta).map { lp =>
if (lp.ne(rt) || getBlockNestedPartialFunction(arg).isEmpty) null
else ftoks.nextNonCommentAfter(ft) match {
case FormatToken(_, _: Token.LeftBrace, lbm) =>
removeToken(claim = lbm.idx :: Nil)
case FormatToken(lt, _: Token.LeftBrace, lbm) =>
if (lt eq rt) removeToken(claim = lbm.idx :: Nil)
else {
val lbo = Some(lbm.rightOwner)
val lb = new Token.LeftBrace(rt.input, rt.dialect, rt.start)
replaceToken("{", owner = lbo, claim = lbm.idx :: Nil)(lb)
}
case _ => null
}
}
Expand Down Expand Up @@ -180,17 +190,14 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
(left, removeToken)
}.orNull

case ReplacementType.Replace if {
val lft = left.ft
val ro = ft.meta.rightOwner
(lft.meta.rightOwner eq ro) && lft.right.is[Token.LeftBrace]
} =>
case ReplacementType.Replace if left.ft.right.is[Token.LeftBrace] =>
val pftOpt = getRightBraceBeforeRightParen(shouldBeRemoved = true)
def replaceIfAfterRightBrace = pftOpt.map { pft =>
val rb = pft.left
// move right to the end of the function
val rType = new ReplacementType.RemoveAndResurrect(ftoks.prev(pft))
left -> replaceToken("}", rtype = rType) {
val rbo = Some(left.ft.rightOwner)
left -> replaceToken("}", owner = rbo, rtype = rType) {
// create a shifted token so that any child tree wouldn't own it
new Token.RightBrace(rb.input, rb.dialect, rb.start + 1)
}
Expand Down Expand Up @@ -260,7 +267,13 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
owner match {
case t: Term.FunctionTerm if t.tokens.last.is[Token.RightBrace] =>
if (!okToRemoveFunctionInApplyOrInit(t)) null else removeToken
case _: Term.PartialFunction => null
case _: Term.PartialFunction =>
val pft = ftoks.prevNonComment(ft)
pft.left match {
case _: Token.LeftParen if isLeftParenReplacedWithBraceOnLeft(pft) =>
removeToken
case _ => null
}
case t: Term.Block => t.parent match {
case Some(f: Term.FunctionTerm)
if okToReplaceFunctionInSingleArgApply(f) => removeToken
Expand Down Expand Up @@ -450,7 +463,9 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case lb: Token.LeftBrace =>
// either arg clause has separate braces, or we guarantee braces
(ft.right ne lb) && !isArgDelimRemoved || hasExpressionWithBraces
case _: Token.LeftParen => hasExpressionWithBraces
case _: Token.LeftParen =>
isLeftParenReplacedWithBraceOnLeft(ldelim) ||
hasExpressionWithBraces
case _ => false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1297,41 +1297,41 @@ object a {
}
>>>
object a {
val foo = bar // c1
{ // c2
val foo = bar { // c1
// c2
case x => y
// c3
} // c4
val foo = bar // c1
{ // c2
val foo = bar { // c1
// c2
{ // c3
case x => y
// c4
} // c5
} // c6
val foo = bar // c1
{ // c2
val foo = bar { // c1
// c2
// c3
{ // c4
case x => y
// c5
} // c6
// c7
} // c8
val foo = bar.baz // c1
{ // c2
val foo = bar.baz { // c1
// c2
case x => y
// c3
} // c4
val foo = bar.baz // c1
{ // c2
val foo = bar.baz { // c1
// c2
{ // c3
case x => y
// c4
} // c5
} // c6
val foo = bar.baz // c1
{ // c2
val foo = bar.baz { // c1
// c2
// c3
{ // c4
case x => y
Expand Down

0 comments on commit 0e49db1

Please sign in to comment.