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: replace paren w/ brace if comment #4449

Merged
merged 2 commits into from
Oct 16, 2024
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 @@ -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
198 changes: 198 additions & 0 deletions scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,204 @@ object a {
val foo = bar.baz { case x => y }
val foo = bar.baz { case x => y }
}
<<< single-block partial function with block one-arg apply, with comments
object a {
val foo = bar { // c1
{ // c2
case x => y
// c3
} // c4
}
val foo = bar { // c1
{ // c2
{ // c3
case x => y
// c4
} // c5
} // c6
}
val foo = bar { // c1
{ // c2
{ // c3
{ // c4
case x => y
// c5
} // c6
} // c7
} // c8
}
val foo = bar.baz { // c1
{ // c2
case x => y
// c3
} // c4
}
val foo = bar.baz { // c1
{ // c2
{ // c3
case x => y
// c4
} // c5
} // c6
}
val foo = bar.baz { // c1
{ // c2
{ // c3
{ // c4
case x => y
// c5
} // c6
} // c7
} // c8
}
}
>>>
object a {
val foo = bar { // c1
{ // c2
case x => y
// c3
} // c4
}
val foo = bar { // c1
// c2
{ // c3
case x => y
// c4
} // c5
// c6
}
val foo = bar { // c1
// c2
// c3
{ // c4
case x => y
// c5
} // c6
// c7
// c8
}
val foo = bar.baz { // c1
{ // c2
case x => y
// c3
} // c4
}
val foo = bar.baz { // c1
// c2
{ // c3
case x => y
// c4
} // c5
// c6
}
val foo = bar.baz { // c1
// c2
// c3
{ // c4
case x => y
// c5
} // c6
// c7
// c8
}
}
<<< single-block partial function with block one-arg apply, with comments and parens
object a {
val foo = bar ( // c1
{ // c2
case x => y
// c3
} // c4
)
val foo = bar ( // c1
{ // c2
{ // c3
case x => y
// c4
} // c5
} // c6
)
val foo = bar ( // c1
{ // c2
{ // c3
{ // c4
case x => y
// c5
} // c6
} // c7
} // c8
)
val foo = bar.baz ( // c1
{ // c2
case x => y
// c3
} // c4
)
val foo = bar.baz ( // c1
{ // c2
{ // c3
case x => y
// c4
} // c5
} // c6
)
val foo = bar.baz ( // c1
{ // c2
{ // c3
{ // c4
case x => y
// c5
} // c6
} // c7
} // c8
)
}
>>>
object a {
val foo = bar { // c1
// c2
case x => y
// c3
} // c4
val foo = bar { // c1
// c2
{ // c3
case x => y
// c4
} // c5
} // c6
val foo = bar { // c1
// c2
// c3
{ // c4
case x => y
// c5
} // c6
// c7
} // c8
val foo = bar.baz { // c1
// c2
case x => y
// c3
} // c4
val foo = bar.baz { // c1
// c2
{ // c3
case x => y
// c4
} // c5
} // c6
val foo = bar.baz { // c1
// c2
// c3
{ // c4
case x => y
// c5
} // c6
// c7
} // c8
}
<<< init-only secondary ctors
class a(vi: Int, vs: String) {
def this() = {
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, 1496759, "total explored")
assertEquals(explored, 1497319, "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
Loading