Skip to content

Commit

Permalink
TreeOps: replace SplitAssignIntoParts
Browse files Browse the repository at this point in the history
Use the new Tree interfaces.
  • Loading branch information
kitbellew committed Dec 27, 2022
1 parent 3052edf commit 5641e0e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,11 +831,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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,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)
Expand Down Expand Up @@ -108,6 +99,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, _) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
32 changes: 10 additions & 22 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,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
Expand Down Expand Up @@ -469,24 +466,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?
*/
Expand Down

0 comments on commit 5641e0e

Please sign in to comment.