diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala index 14a58b0e73..519c1ecf7e 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/Newlines.scala @@ -1,8 +1,6 @@ package org.scalafmt.config -import scala.meta.Pkg -import scala.meta.Template -import scala.meta.Tree +import scala.meta._ import org.scalafmt.config.Newlines._ import org.scalafmt.internal.FormatToken @@ -451,8 +449,10 @@ object Newlines { def apply(tree: Tree): Boolean = true } case object `def` extends ForceBeforeMultilineAssign { - def apply(tree: Tree): Boolean = - TreeOps.splitAssignIntoParts.lift(tree).exists(_._2.isDefined) + def apply(tree: Tree): Boolean = tree match { + case _: Tree.WithParamClauses with Stat.WithMods => true + case _ => false + } } case object anyMember extends ForceBeforeMultilineAssign { def apply(tree: Tree): Boolean = tree.parent.exists(_.is[Template]) 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 8c113b3387..5864e31d49 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 @@ -620,9 +620,9 @@ class FormatOps( case _: Term.If | _: Term.While | _: Term.Do => true case _: Member.ArgClause => true case p: Term.Block => isSingleElement(p.stats, child) - case p: Term.ForYield => p.body eq child - case SplitAssignIntoParts(`child`, _) => true case SplitCallIntoParts(_) => true + case t: Tree.WithBody => t.body eq child + case t: Term.Param => t.default.contains(child) case _ => false } val allowNoIndent = style.indentOperator.getExemptScope match { @@ -2201,7 +2201,7 @@ class FormatOps( else getSplitsForStatsImpl(ft, nft, t.init, t.stats) def rightBrace = treeLast(t) }) - case t @ SplitAssignIntoParts((x: Term.PartialFunction, _)) => + case t @ Tree.WithBody(x: Term.PartialFunction) => Some(new OptionalBracesRegion { def owner = Some(t) def splits = getSplitsForStats(ft, nft, x.cases, nlOnly = true) @@ -2609,12 +2609,8 @@ class FormatOps( def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = ft.meta.leftOwner match { case t: Ctor.Secondary => Some((t, seq(all, t.init, t.stats))) - case t: Defn.Def => Some((t.body, Nil)) - case t: Defn.Macro => Some((t.body, Nil)) - case t: Term.Assign => Some((t.rhs, Nil)) - case t: Defn.Type => Some((t.body, Nil)) - case t: Defn.Val => Some((t.rhs, Nil)) case t: Defn.Var => t.rhs.map(_ -> Nil) + case t: Tree.WithBody => Some((t.body, Nil)) case _ => BlockImpl.getBlocks(ft, nft, all) } } 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 5deafad006..e58af3e707 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 @@ -841,11 +841,10 @@ class Router(formatOps: FormatOps) { // DefDef case FormatToken(_: T.KwDef, _: T.Ident, _) => Seq(Split(Space, 0)) - case ft @ FormatToken(_: T.Equals, _, SplitAssignIntoPartsLeft(parts)) => + case ft @ FormatToken(_: T.Equals, _, DefValAssignLeft(rhs)) => maybeGetInfixSplitsBeforeLhs(ft) { - val (rhs, paramss) = parts getSplitsDefValEquals(ft, rhs) { - if (paramss.isDefined) getSplitsDefEquals(ft, rhs) + if (leftOwner.is[Tree.WithParamClauses]) getSplitsDefEquals(ft, rhs) else getSplitsValEquals(ft, rhs)(getSplitsValEqualsClassic(ft, rhs)) } } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala index b359a98051..66ac029ac8 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala @@ -32,22 +32,13 @@ object RedundantParens extends Rewrite with FormatTokensRewrite.RuleFactory { object IsExprBody { def unapply(t: Tree): Option[Boolean] = { - @inline def iff(body: Tree) = Some(body eq t) @inline def okIf(body: Tree) = if (body eq t) Some(true) else None t.parent.flatMap { - case TreeOps.SplitAssignIntoParts((body, _)) => iff(body) case p: Case => okIf(p.body) - case p: Enumerator.CaseGenerator => iff(p.rhs) - case p: Enumerator.Generator => iff(p.rhs) - case p: Enumerator.Val => iff(p.rhs) - case p: Term.Do => iff(p.body) - case p: Term.For => iff(p.body) - case p: Term.ForYield => iff(p.body) - case p: Term.FunctionTerm => iff(p.body) case p: Term.If if p.cond ne t => Some(p.thenp.ne(t) || !TreeOps.ifWithoutElse(t)) - case p: Term.PolyFunction => iff(p.body) case p: Term.While => okIf(p.body) + case p: Tree.WithBody => Some(t eq p.body) case _: Term.Return | _: Term.Throw | _: Term.QuotedMacroExpr | _: Term.SplicedMacroExpr | _: Term.Block => Some(true) @@ -107,6 +98,7 @@ class RedundantParens(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { case _ if numParens >= 2 => true case _: Term.AnonymousFunction | _: Term.Param => false + case _: Type.FunctionType => false case t: Member.ArgClause => okToReplaceArgClause(t) case Term.ParamClause(t :: Nil, _) => 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 10dce51b27..694825dcfa 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 @@ -124,8 +124,7 @@ private class RemoveScala3OptionalBraces(ftoks: FormatTokens) if (tree ne t.body) null else if (ftoks.prevNonComment(ft).left.is[Token.Equals]) removeToken else null - case TreeOps.SplitAssignIntoParts(parts) => - if (tree.eq(parts._1)) removeToken else null + case p: Tree.WithBody => if (p.body eq tree) removeToken else null case _ => null } 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 4afd8b8f89..18bebcba66 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 @@ -418,10 +418,7 @@ object TreeOps { } def isTuple(tree: Tree): Boolean = - tree match { - case _: Pat.Tuple | _: Term.Tuple | _: Type.Tuple => true - case _ => false - } + tree.is[Member.Tuple] def noSpaceBeforeOpeningParen( tree: Tree @@ -468,24 +465,15 @@ object TreeOps { splitCallIntoParts.lift(tree) } - type AssignParts = (Tree, Option[Seq[Seq[Term.Param]]]) - val splitAssignIntoParts: PartialFunction[Tree, AssignParts] = { - case t: Defn.Def => (t.body, Some(t.paramss)) - case t: Defn.Macro => (t.body, Some(t.paramss)) - case t: Defn.GivenAlias => (t.body, Some(t.sparams)) - case t: Ctor.Secondary => (t.init, Some(t.paramss)) - case t: Term.Param if t.default.isDefined => (t.default.get, None) - case t: Term.Assign => (t.rhs, None) - case t: Defn.Type => (t.body, None) - case t: Defn.Val => (t.rhs, None) - case t: Defn.Var => (t.rhs.getOrElse(t), None) // var x: Int = _, no policy - } - object SplitAssignIntoParts { - def unapply(tree: Tree): Option[AssignParts] = - splitAssignIntoParts.lift(tree) - } - val SplitAssignIntoPartsLeft = - new FormatToken.ExtractFromMeta(x => splitAssignIntoParts.lift(x.leftOwner)) + val DefValAssignLeft = + new FormatToken.ExtractFromMeta(_.leftOwner match { + case _: Enumerator => None // it's WithBody + case t: Ctor.Secondary => Some(t.init) + case t: Defn.Var => t.rhs.orElse(Some(t)) // `_` replaced with None + case t: Tree.WithBody => Some(t.body) + case t: Term.Param => t.default + case _ => None + }) /** How many parents of tree are Term.Apply? */