diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index e07a34a186..50cdd4d23f 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -624,7 +624,7 @@ class FormatOps( } def isOldTopLevel(child: Tree) = child.parent.exists { case _: Term.Block | _: Term.If | _: Term.While | _: Source => true - case fun: Term.FunctionTerm if isBlockFunction(fun) => true + case fun: Term.FunctionTerm => isBlockFunction(fun) case t: Case => t.pat.eq(child) || t.body.eq(child) case _ => false } @@ -921,7 +921,7 @@ class FormatOps( def isEmptyFunctionBody(tree: Tree): Boolean = tree match { - case function: Term.Function => + case function: Term.FunctionTerm => function.body match { case b: Term.Block => b.stats.isEmpty case _ => false @@ -2676,7 +2676,7 @@ class FormatOps( private object RightArrowImpl extends Factory { def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = ft.meta.leftOwner match { - case t: Term.Function => + case t: Term.FunctionTerm => val skip = t.parent.exists(_.is[Term.Block]) if (skip) None else Some((t.body, seq(all, t.paramClause.values))) case _ => None diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index 46dd89e98e..174057f41d 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -163,7 +163,7 @@ class FormatWriter(formatOps: FormatOps) { case b: Term.Block => checkApply(b) && RedundantBraces.canRewriteWithParens(b) && b.parent.exists(_.tokens.last.start == rb.start) - case f: Term.Function => + case f: Term.FunctionTerm => checkApply(f) && RedundantBraces.canRewriteWithParens(f) case _ => false } @@ -1258,7 +1258,7 @@ class FormatWriter(formatOps: FormatOps) { def unapply(tree: Tree): Option[Tree] = tree match { case _: Source | _: Template | _: Term.Block | _: Term.Match | - _: Type.Match | _: Term.Function | _: Term.PartialFunction => + _: Type.Match | _: Term.FunctionTerm | _: Term.PartialFunction => Some(tree) case _ => None } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index bb14137233..70b0c99477 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -1486,7 +1486,7 @@ class Router(formatOps: FormatOps) { else Split(Space, 0).onlyIf(useSpace).withSingleLine(close) val otherSplits = rightOwner match { case _: Term.PartialFunction | Term.Block( - List(_: Term.Function | _: Term.PartialFunction) + List(_: Term.FunctionTerm | _: Term.PartialFunction) ) => Seq(Split(Newline, 0)) case _ => 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 8dde68a845..f27a816922 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 @@ -17,7 +17,7 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory { override def create(ftoks: FormatTokens): Rule = new RedundantBraces(ftoks) - def needParensAroundParams(f: Term.Function): Boolean = + def needParensAroundParams(f: Term.FunctionTerm): Boolean = /* either we have parens or no type; multiple params or * no params guarantee parens, so we look for type and * parens only for a single param */ @@ -29,7 +29,7 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory { def canRewriteWithParens(b: Term.Block): Boolean = getBlockSingleStat(b).exists { - case f: Term.Function => canRewriteWithParens(f) + case f: Term.FunctionTerm => canRewriteWithParens(f) case _: Term.Assign => false // disallowed in 2.13 case _: Defn => false case _ => true @@ -38,9 +38,12 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory { /* guard for statements requiring a wrapper block * "foo { x => y; z }" can't become "foo(x => y; z)" */ @tailrec - def canRewriteWithParens(f: Term.Function, nested: Boolean = false): Boolean = + def canRewriteWithParens( + f: Term.FunctionTerm, + nested: Boolean = false + ): Boolean = !needParensAroundParams(f) && (getTreeSingleStat(f.body) match { - case Some(t: Term.Function) => canRewriteWithParens(t, true) + case Some(t: Term.FunctionTerm) => canRewriteWithParens(t, true) case Some(_: Defn) => false case x => nested || x.isDefined }) @@ -153,7 +156,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { case arg :: Nil if t.pos.start == arg.pos.start => onLeftBrace(arg) case _ => null } - case t: Term.Function if t.tokens.last.is[Token.RightBrace] => + case t: Term.FunctionTerm if t.tokens.last.is[Token.RightBrace] => if (!okToRemoveFunctionInApplyOrInit(t)) null else if (okToReplaceFunctionInSingleArgApply(t)) replaceWithLeftParen else removeToken @@ -237,12 +240,12 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { b: Term.Block )(implicit style: ScalafmtConfig): Boolean = b.parent.exists { - case f: Term.Function => + case f: Term.FunctionTerm => okToReplaceFunctionInSingleArgApply(f) case _ => false } - private def okToReplaceFunctionInSingleArgApply(f: Term.Function)(implicit + private def okToReplaceFunctionInSingleArgApply(f: Term.FunctionTerm)(implicit style: ScalafmtConfig ): Boolean = f.parent.flatMap(okToReplaceFunctionInSingleArgApply).exists(_._2 eq f) @@ -255,11 +258,11 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { // single-arg apply of a lambda // a(b => { c; d }) change to a { b => c; d } - private def okToReplaceFunctionInSingleArgApply( - tree: Tree - )(implicit style: ScalafmtConfig): Option[(Token.LeftParen, Term.Function)] = + private def okToReplaceFunctionInSingleArgApply(tree: Tree)(implicit + style: ScalafmtConfig + ): Option[(Token.LeftParen, Term.FunctionTerm)] = tree match { - case ta @ Term.ArgClause((func: Term.Function) :: Nil, _) if { + case ta @ Term.ArgClause((func: Term.FunctionTerm) :: Nil, _) if { val body = func.body (body.is[Term.Block] || func.tokens.last.ne(body.tokens.last)) && isParentAnApply(ta) && okToRemoveAroundFunctionBody(body, true) @@ -273,7 +276,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { // a single-stat lambda with braces can be converted to one without braces, // but the reverse conversion isn't always possible private def okToRemoveFunctionInApplyOrInit( - t: Term.Function + t: Term.FunctionTerm )(implicit style: ScalafmtConfig): Boolean = t.parent match { case Some(p: Term.ArgClause) => @@ -365,7 +368,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { case d: Defn.GivenAlias => checkBlockAsBody(b, d.body, noParams(d.paramClauseGroup)) - case p: Term.Function if isFunctionWithBraces(p) => + case p: Term.FunctionTerm if isFunctionWithBraces(p) => okToRemoveAroundFunctionBody(b, true) case _: Term.If => @@ -409,20 +412,22 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { !b.parent.exists(_.is[Term.Select]) || t.templ.inits.lengthCompare(1) <= 0 || t.templ.stats.nonEmpty || t.tokens.last.is[Token.RightBrace] - case tree => tree.is[Term] && tree.isNot[Term.Function] + case tree => tree.is[Term] && tree.isNot[Term.FunctionTerm] } private def okToRemoveBlockWithinApply( b: Term.Block )(implicit style: ScalafmtConfig): Boolean = getSingleStatIfLineSpanOk(b).exists { - case f: Term.Function if needParensAroundParams(f) => false - case f: Term.Function if f.body.is[Term.Block] => - val fb = f.body - // don't rewrite block if the inner block will be rewritten, too - // sometimes a function body block doesn't have braces - fb.tokens.headOption.exists(_.is[Token.LeftBrace]) && - !okToRemoveAroundFunctionBody(fb, true) + case f: Term.FunctionTerm => + !needParensAroundParams(f) && { + val fb = f.body + !fb.is[Term.Block] || + // don't rewrite block if the inner block will be rewritten, too + // sometimes a function body block doesn't have braces + fb.tokens.headOption.exists(_.is[Token.LeftBrace]) && + !okToRemoveAroundFunctionBody(fb, true) + } case _: Term.Assign => false // f({ a = b }) is not the same as f(a = b) case _ => true } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala index 715b9ad1e1..b5d2b613d3 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala @@ -130,7 +130,7 @@ private class RemoveScala3OptionalBraces(ftoks: FormatTokens) case _: Term.Throw => removeToken case _: Term.Return => removeToken case _: Defn.ExtensionGroup => removeToken - case _: Term.Function => removeToken + case _: Term.FunctionTerm => removeToken case t: Defn.Def => if (tree ne t.body) null else if (ftoks.prevNonComment(ft).left.is[Token.Equals]) removeToken diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index d8e6bc8091..b82cab45aa 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -56,7 +56,7 @@ object TreeOps { case _ => false } - def isFunctionWithBraces(fun: Term.Function): Boolean = + def isFunctionWithBraces(fun: Term.FunctionTerm): Boolean = fun.parent match { case Some(Term.Block(`fun` :: Nil)) => true case _ => false @@ -154,7 +154,7 @@ object TreeOps { case t @ Term.Block(arg :: Nil) // single-stat block if t.tokens.headOption // see if opening brace was removed .exists(x => x.is[Token.LeftBrace] && ftoks(x).left.ne(x)) => - if (arg.is[Term.Function]) { + if (arg.is[Term.FunctionTerm]) { // handle rewritten apply { { x => b } } to a { x => b } val parentApply = findTreeWithParent(t) { case Term.Block(_) => None @@ -167,9 +167,10 @@ object TreeOps { case t: Term.Apply => val ac = t.argClause ac.values match { - case (f: Term.Function) :: Nil if ac.tokens.lastOption.exists { x => - x.is[Token.RightParen] && // see if closing paren is now brace - ftoks.prevNonComment(ftoks(x)).left.is[Token.RightBrace] + case (f: Term.FunctionTerm) :: Nil if ac.tokens.lastOption.exists { + x => // see if closing paren is now brace + x.is[Token.RightParen] && + ftoks.prevNonComment(ftoks(x)).left.is[Token.RightBrace] } => addOne(f) case _ =>