From efc1bbc903563f8d774f9b1eb2cc3fda1c2b281c Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 25 Mar 2024 09:03:56 -0700 Subject: [PATCH] RedundantBraces: remove parens and trailing comma --- .../scalafmt/rewrite/RedundantBraces.scala | 45 ++++++++++-- .../resources/rewrite/RedundantBraces.stat | 33 +++++---- .../test/resources/scala3/FewerBraces.stat | 62 ++++++++-------- .../resources/scala3/FewerBraces_fold.stat | 36 +++++----- .../resources/scala3/FewerBraces_keep.stat | 70 ++++++++++--------- .../resources/scala3/FewerBraces_unfold.stat | 54 +++++++------- 6 files changed, 166 insertions(+), 134 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala index 5c01da426d..437b58ae0e 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala @@ -144,29 +144,60 @@ class RedundantBraces(implicit val ftoks: FormatTokens) private def onRightParen(left: Replacement)(implicit ft: FormatToken, + session: Session, style: ScalafmtConfig ): (Replacement, Replacement) = left.how match { - case ReplacementType.Remove => (left, removeToken) + case ReplacementType.Remove => + val resOpt = getRightBraceBeforeRightParen(false).map { rb => + // we'll use right brace later, when applying fewer-braces rewrite + (left, removeToken) + } + resOpt.orNull + case ReplacementType.Replace if { val lft = left.ft val ro = ft.meta.rightOwner (lft.meta.rightOwner eq ro) && - lft.right.is[Token.LeftBrace] && - okToReplaceFunctionInSingleArgApply(ro).isDefined + lft.right.is[Token.LeftBrace] } => - val pft = ftoks.prevNonComment(ft) - val rb = pft.left - if (rb.is[Token.RightBrace]) { + val pftOpt = getRightBraceBeforeRightParen(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) { // create a shifted token so that any child tree wouldn't own it new Token.RightBrace(rb.input, rb.dialect, rb.start + 1) } - } else null // don't know how to Replace + } + replaceIfAfterRightBrace.orNull // don't know how to Replace case _ => null } + private def getRightBraceBeforeRightParen(shouldBeRemoved: Boolean)(implicit + ft: FormatToken, + session: Session, + style: ScalafmtConfig + ): Option[FormatToken] = { + val pft = ftoks.prevNonComment(ft) + val ok = pft.left match { + case _: Token.Comma => // looks like trailing comma + val pft2 = ftoks.prevNonCommentBefore(pft) + pft2.left.is[Token.RightBrace] && + session.isRemovedOnLeft(pft2, true) == shouldBeRemoved && + session.isRemovedOnLeftOpt(pft).getOrElse { + val crt = ftoks.prev(pft) + val crepl = Replacement(this, crt, ReplacementType.Remove, style) + session.claim(crepl)(crt) + true + } + case _: Token.RightBrace => + !session.isRemovedOnLeft(pft, !shouldBeRemoved) + case _ => false + } + if (ok) Some(pft) else None + } + private def onLeftBrace(implicit ft: FormatToken, session: Session, diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat index 1f181c001e..c74e7f06e1 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat @@ -1477,15 +1477,14 @@ object a { ) } >>> -Idempotency violated -=> Diff (- obtained, + expected) - object a { -- mtd1(x => x + 1) + mtd2(x => x + 1 x +2) -+ mtd1(x => x + 1) + mtd2(x => -+ x + 1 -+ x + 2, -+ ) - } +object a { + mtd1 { x => + x + 1 + } + mtd2 { x => + x + 1 + x + 2 + } +} <<< rewrite with trailing commas: func in parens, infix after, !allowFolding rewrite.trailingCommas.style = keep rewrite.trailingCommas.allowFolding = false @@ -1503,14 +1502,14 @@ object a { ) } >>> -Idempotency violated -=> Diff (- obtained, + expected) - ) + mtd2( -- x => x + 1 x +2, -+ x => -+ x + 1 -+ x + 2, - ) +object a { + mtd1 { x => + x + 1 + } + mtd2 { x => + x + 1 + x + 2 + } +} <<< rewrite with trailing commas: func in parens and braces, allowFolding rewrite.rules = [RedundantBraces, RedundantParens] rewrite.trailingCommas.style = keep diff --git a/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat b/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat index 83ad131015..79b61a784d 100644 --- a/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat @@ -1934,18 +1934,18 @@ foo ) >>> foo - .mtd1(x => x + 1) - .mtd2(x => + .mtd1 { x => + x + 1 + } + .mtd2 { x => x + 1 x + 2 - , - ) - .mtd3(x => + } + .mtd3 { x => x + 1 x + 2 x + 3 - , - ) + } <<< rewrite to fewer braces: func in parens, trailing comma, !allowFolding rewrite.rules = [RedundantBraces] rewrite.trailingCommas.style = keep @@ -1975,22 +1975,18 @@ foo ) >>> foo - .mtd1( - x => x + 1, - ) - .mtd2( - x => - x + 1 - x + 2 - , - ) - .mtd3( - x => - x + 1 - x + 2 - x + 3 - , - ) + .mtd1 { x => + x + 1 + } + .mtd2 { x => + x + 1 + x + 2 + } + .mtd3 { x => + x + 1 + x + 2 + x + 3 + } <<< rewrite to fewer braces: func in parens, infix after rewrite.rules = [RedundantBraces] rewrite.scala3.removeOptionalBraces = { @@ -2017,14 +2013,18 @@ object a { } >>> object a: - mtd1(x => x + 1) + mtd2(x => - x + 1 - x + 2 - ) + mtd3(x => - x + 1 - x + 2 - x + 3 - ) + mtd1 { x => + x + 1 + } + + mtd2 { x => + x + 1 + x + 2 + } + + mtd3 { x => + x + 1 + x + 2 + x + 3 + } <<< rewrite to fewer braces: func in parens and braces rewrite.rules = [RedundantBraces, RedundantParens] rewrite.scala3.removeOptionalBraces = { diff --git a/scalafmt-tests/src/test/resources/scala3/FewerBraces_fold.stat b/scalafmt-tests/src/test/resources/scala3/FewerBraces_fold.stat index 6fd7ecf593..43ee068b56 100644 --- a/scalafmt-tests/src/test/resources/scala3/FewerBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/FewerBraces_fold.stat @@ -1693,14 +1693,14 @@ foo }, ) >>> -foo.mtd1(x => x + 1).mtd2(x => +foo.mtd1(x => x + 1).mtd2 { x => x + 1 - x + 2, -).mtd3(x => + x + 2 +}.mtd3 { x => x + 1 x + 2 - x + 3, -) + x + 3 +} <<< rewrite to fewer braces: func in parens, trailing comma, !allowFolding rewrite.rules = [RedundantBraces] rewrite.trailingCommas.style = always @@ -1729,18 +1729,14 @@ foo }, ) >>> -foo.mtd1( - x => x + 1, -).mtd2( - x => - x + 1 - x + 2, -).mtd3( - x => - x + 1 - x + 2 - x + 3, -) +foo.mtd1 { x => x + 1 }.mtd2 { x => + x + 1 + x + 2 +}.mtd3 { x => + x + 1 + x + 2 + x + 3 +} <<< rewrite to fewer braces: func in parens, infix after rewrite.rules = [RedundantBraces] rewrite.scala3.removeOptionalBraces = { @@ -1767,14 +1763,14 @@ object a { } >>> object a: - mtd1(x => x + 1) + mtd2(x => + mtd1(x => x + 1) + mtd2 { x => x + 1 x + 2 - ) + mtd3(x => + } + mtd3 { x => x + 1 x + 2 x + 3 - ) + } <<< rewrite to fewer braces: func in parens and braces rewrite.rules = [RedundantBraces, RedundantParens] rewrite.scala3.removeOptionalBraces = { diff --git a/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat index a80b517e08..66f163498b 100644 --- a/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat @@ -1905,20 +1905,21 @@ foo ) >>> foo - .mtd1(x => - x + 1, - ) - .mtd2(x => - x + 1 - x + 2 - , - ) - .mtd3(x => - x + 1 - x + 2 - x + 3 - , - ) + .mtd1 { + x => + x + 1 + } + .mtd2 { + x => + x + 1 + x + 2 + } + .mtd3 { + x => + x + 1 + x + 2 + x + 3 + } <<< rewrite to fewer braces: func in parens, trailing comma, !allowFolding rewrite.rules = [RedundantBraces] rewrite.trailingCommas.style = keep @@ -1948,23 +1949,21 @@ foo ) >>> foo - .mtd1( + .mtd1 { x => - x + 1, - ) - .mtd2( + x + 1 + } + .mtd2 { x => x + 1 x + 2 - , - ) - .mtd3( + } + .mtd3 { x => x + 1 x + 2 x + 3 - , - ) + } <<< rewrite to fewer braces: func in parens, infix after rewrite.rules = [RedundantBraces] rewrite.scala3.removeOptionalBraces = { @@ -1991,16 +1990,21 @@ object a { } >>> object a: - mtd1(x => - x + 1 - ) + mtd2(x => - x + 1 - x + 2 - ) + mtd3(x => - x + 1 - x + 2 - x + 3 - ) + mtd1 { + x => + x + 1 + } + + mtd2 { + x => + x + 1 + x + 2 + } + + mtd3 { + x => + x + 1 + x + 2 + x + 3 + } <<< rewrite to fewer braces: func in parens and braces rewrite.rules = [RedundantBraces, RedundantParens] rewrite.scala3.removeOptionalBraces = { diff --git a/scalafmt-tests/src/test/resources/scala3/FewerBraces_unfold.stat b/scalafmt-tests/src/test/resources/scala3/FewerBraces_unfold.stat index ec5c88fa69..e67e3736f5 100644 --- a/scalafmt-tests/src/test/resources/scala3/FewerBraces_unfold.stat +++ b/scalafmt-tests/src/test/resources/scala3/FewerBraces_unfold.stat @@ -1925,16 +1925,18 @@ foo ) >>> foo - .mtd1(x => x + 1) - .mtd2(x => + .mtd1 { x => + x + 1 + } + .mtd2 { x => x + 1 - x + 2, - ) - .mtd3(x => + x + 2 + } + .mtd3 { x => x + 1 x + 2 - x + 3, - ) + x + 3 + } <<< rewrite to fewer braces: func in parens, trailing comma, !allowFolding rewrite.rules = [RedundantBraces] rewrite.trailingCommas.style = always @@ -1964,20 +1966,18 @@ foo ) >>> foo - .mtd1( - x => x + 1, - ) - .mtd2( - x => - x + 1 - x + 2, - ) - .mtd3( - x => - x + 1 - x + 2 - x + 3, - ) + .mtd1 { x => + x + 1 + } + .mtd2 { x => + x + 1 + x + 2 + } + .mtd3 { x => + x + 1 + x + 2 + x + 3 + } <<< rewrite to fewer braces: func in parens, infix after rewrite.rules = [RedundantBraces] rewrite.scala3.removeOptionalBraces = { @@ -2004,16 +2004,18 @@ object a { } >>> object a: - mtd1(x => x + 1) + - mtd2(x => + mtd1 { x => + x + 1 + } + + mtd2 { x => x + 1 x + 2 - ) + - mtd3(x => + } + + mtd3 { x => x + 1 x + 2 x + 3 - ) + } <<< rewrite to fewer braces: func in parens and braces rewrite.rules = [RedundantBraces, RedundantParens] rewrite.scala3.removeOptionalBraces = {