Skip to content

Commit

Permalink
TreeOps: add method isParentAnApply
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Meltzer authored and kitbellew committed Feb 18, 2024
1 parent 0d7584f commit f2b64cb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class FormatWriter(formatOps: FormatOps) {

def checkApply(t: Tree): Boolean = t.parent match {
case Some(p @ Term.ArgClause(`t` :: Nil, _)) =>
p.parent.exists(_.is[Term.Apply])
TreeOps.isParentAnApply(p)
case _ => false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
case ta @ Term.ArgClause((func: Term.Function) :: Nil, _) if {
val body = func.body
(body.is[Term.Block] || func.tokens.last.ne(body.tokens.last)) &&
ta.parent.exists(_.is[Term.Apply]) &&
okToRemoveAroundFunctionBody(body, true)
isParentAnApply(ta) && okToRemoveAroundFunctionBody(body, true)
} =>
getOpeningParen(ta).map((_, func))
case _ => None
Expand Down Expand Up @@ -341,7 +340,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
(p.body eq b) || shouldRemoveSingleStatBlock(b)
}

case t: Term.ArgClause if t.parent.exists(_.is[Term.Apply]) =>
case t: Term.ArgClause if isParentAnApply(t) =>
// Example: as.map { _.toString }
// Leave this alone for now.
// In future there should be an option to surround such expressions with parens instead of braces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ object TreeOps {
// handle rewritten apply { { x => b } } to a { x => b }
val parentApply = findTreeWithParent(t) {
case Term.Block(_) => None
case p @ Term.ArgClause(_ :: Nil, _)
if p.parent.exists(_.is[Term.Apply]) =>
Some(true)
case p @ Term.ArgClause(_ :: Nil, _) => Some(isParentAnApply(p))
case _ => Some(false)
}
if (parentApply.isDefined) addOne(arg)
Expand Down Expand Up @@ -1041,4 +1039,7 @@ object TreeOps {
case _ => false
}

def isParentAnApply(t: Tree): Boolean =
t.parent.exists(_.is[Term.Apply])

}

0 comments on commit f2b64cb

Please sign in to comment.