Skip to content

Commit

Permalink
Use Tree.WithBody instead of SplitAssignIntoParts
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 23, 2022
1 parent 4e1ad69 commit 505dbaf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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 @@ -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)
Expand Down Expand Up @@ -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, _) =>
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

0 comments on commit 505dbaf

Please sign in to comment.