Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeOps: use numParents for nestedApplies/Select #1859

Merged
merged 2 commits into from Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -514,9 +514,9 @@ class Router(formatOps: FormatOps) {
// Opening [ with no leading space.
// Opening ( with no leading space.
case FormatToken(
T.KwSuper() | T.KwThis() | T.Ident(_) | T.RightBracket() |
T.RightBrace() | T.RightParen() | T.Underscore(),
T.LeftParen() | T.LeftBracket(),
RightParenOrBracket() | T.KwSuper() | T.KwThis() | T.Ident(_) |
T.RightBrace() | T.Underscore(),
LeftParenOrBracket(),
_
) if noSpaceBeforeOpeningParen(rightOwner) && {
leftOwner.parent.forall {
Expand Down Expand Up @@ -569,7 +569,7 @@ class Router(formatOps: FormatOps) {

// Parameter opening for one parameter group. This format works
// on the WHOLE defnSite (via policies)
case ft @ FormatToken((T.LeftParen() | T.LeftBracket()), _, _)
case ft @ FormatToken(LeftParenOrBracket(), _, _)
if style.verticalMultiline.atDefnSite &&
isDefnSiteWithParams(leftOwner) =>
verticalMultiline(leftOwner, ft)(style)
Expand Down Expand Up @@ -615,7 +615,7 @@ class Router(formatOps: FormatOps) {
)
}

case FormatToken(T.LeftParen() | T.LeftBracket(), right, between)
case FormatToken(LeftParenOrBracket(), right, between)
if style.optIn.configStyleArguments && isDefnOrCallSite(leftOwner) &&
(opensConfigStyle(formatToken, false) || {
forceConfigStyle(leftOwner) && !styleMap.forcedBinPack(leftOwner)
Expand Down Expand Up @@ -646,7 +646,7 @@ class Router(formatOps: FormatOps) {
.withIndent(extraIndent, right, Before)
)

case FormatToken(open @ (T.LeftParen() | T.LeftBracket()), right, between)
case FormatToken(open @ LeftParenOrBracket(), right, between)
if style.binPack.unsafeDefnSite && isDefnSite(leftOwner) =>
val close = matching(open)
val isBracket = open.is[T.LeftBracket]
Expand Down Expand Up @@ -691,7 +691,7 @@ class Router(formatOps: FormatOps) {
.withIndent(indent, close, After)
)
}
case FormatToken(T.LeftParen() | T.LeftBracket(), _, _)
case FormatToken(LeftParenOrBracket(), _, _)
if style.binPack.unsafeCallSite && isCallSite(leftOwner) =>
val open = formatToken.left
val close = matching(open)
Expand Down Expand Up @@ -733,11 +733,10 @@ class Router(formatOps: FormatOps) {
if !style.spaces.afterKeywordBeforeParen =>
Seq(Split(NoSplit, 0))

case tok @ FormatToken(T.LeftParen() | T.LeftBracket(), right, between)
case tok @ FormatToken(open @ LeftParenOrBracket(), right, _)
if !isSuperfluousParenthesis(formatToken.left, leftOwner) &&
(!style.binPack.unsafeCallSite && isCallSite(leftOwner)) ||
(!style.binPack.unsafeDefnSite && isDefnSite(leftOwner)) =>
val open = tok.left
val close = matching(open)
val (lhs, args) = getApplyArgs(formatToken, false)
// In long sequence of select/apply, we penalize splitting on
Expand Down
Expand Up @@ -214,10 +214,10 @@ object TreeOps {
childOf(owners(hash(tok)), tree)

@tailrec
final def numParents(tree: Tree, accum: Int = 0): Int =
final def numParents(tree: Tree, cnt: Int = 0)(f: Tree => Boolean): Int =
tree.parent match {
case Some(parent) => numParents(parent, 1 + accum)
case _ => accum
case Some(p) => numParents(p, if (f(p)) 1 + cnt else cnt)(f)
case _ => cnt
}

/**
Expand Down Expand Up @@ -430,23 +430,12 @@ object TreeOps {
/**
* How many parents of tree are Term.Apply?
*/
def nestedApplies(tree: Tree): Int = {
// TODO(olafur) optimize?
tree.parent.fold(0) {
case parent @ (_: Term.Apply | _: Term.ApplyInfix | _: Type.Apply) =>
1 + nestedApplies(parent)
case parent => nestedApplies(parent)
}
def nestedApplies(tree: Tree): Int = numParents(tree) {
case _: Term.Apply | _: Term.ApplyInfix | _: Type.Apply => true
case _ => false
}

// TODO(olafur) abstract with [[NestedApplies]]

def nestedSelect(tree: Tree): Int = {
tree.parent.fold(0) {
case parent: Term.Select => 1 + nestedSelect(parent)
case parent => nestedSelect(parent)
}
}
def nestedSelect(tree: Tree): Int = numParents(tree)(_.is[Term.Select])

// TODO(olafur) scala.meta should make this easier.
def findSiblingGuard(
Expand Down