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

FormatOps: refactor GetSelectLike #4042

Merged
merged 1 commit into from
Jun 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,11 @@ class FormatOps(

def canStartSelectChain(
thisSelectLike: SelectLike,
nextSelect: Option[Term],
nextSelectLike: Option[SelectLike],
lastApply: Tree,
)(implicit style: ScalafmtConfig): Boolean = {
val thisTree = thisSelectLike.tree
val nextSelect = nextSelectLike.map(_.tree)
val ok = thisTree.ne(lastApply) &&
!cannotStartSelectChainOnExpr(thisSelectLike.qual)
def checkParent = thisTree.parent match {
Expand Down Expand Up @@ -1372,8 +1373,7 @@ class FormatOps(
lastApply: Tree,
)(implicit style: ScalafmtConfig): Boolean = prevSelect match {
case None => false
case Some(p) if canStartSelectChain(p, Some(thisSelect.tree), lastApply) =>
true
case Some(p) if canStartSelectChain(p, Some(thisSelect), lastApply) => true
case Some(p) =>
val prevPrevSelect = findPrevSelect(p, style.encloseSelectChains)
inSelectChain(prevPrevSelect, p, lastApply)
Expand Down Expand Up @@ -1697,22 +1697,29 @@ class FormatOps(
case _ => isEnclosedInParens(body)
}

def getMatchDot(tree: Term.Match): Option[FormatToken] =
if (dialect.allowMatchAsOperator) {
val ft = tokenAfter(tree.expr)
if (ft.right.is[T.Dot]) Some(ft) else None
} else None

def getKwMatchAfterDot(ft: FormatToken): T.KwMatch = tokens
.nextNonCommentAfter(ft).right.asInstanceOf[T.KwMatch]

object GetSelectLike {
def unapply(tree: Tree): Option[SelectLike] = tree match {
case t: Term.Select => Some(SelectLike(t))
case t: Term.Match => getMatchDot(t)
.map(ft => SelectLike(t, getKwMatchAfterDot(ft)))
val OnRight =
new ExtractFromMeta(m => onRightOpt(m.rightOwner, tokens(m.idx)))

private[FormatOps] def onRightOpt(
ro: Tree,
ftOrNull: => FormatToken,
): Option[SelectLike] = ro match {
case x: Term.Select => Some(SelectLike(x))
case x: Term.Match if dialect.allowMatchAsOperator =>
val ft = Option(ftOrNull).getOrElse(tokenAfter(x.expr))
if (!ft.right.is[T.Dot]) None
else nextNonCommentAfter(ft).right match {
case kw: T.KwMatch => Some(SelectLike(x, kw))
case _ => None
}
case _ => None
}

def onRightOpt(ft: FormatToken): Option[SelectLike] =
onRightOpt(ft.meta.rightOwner, ft)

def unapply(tree: Tree): Option[SelectLike] = onRightOpt(tree, null)
}

def getSplitsForTypeBounds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ class Router(formatOps: FormatOps) {
(leftOwner match {
case x: Term.PartialFunction => isSingleElement(x.cases, t)
case x: Term.Match => isSingleElement(x.cases, t) &&
getMatchDot(x).isDefined
dialect.allowMatchAsOperator &&
tokenAfter(x.expr).right.is[T.Dot]
case _: Term.Try => false
case _ => tokenAfter(t).right eq close
}) =>
Expand Down Expand Up @@ -1563,16 +1564,10 @@ class Router(formatOps: FormatOps) {
case _ => Some(false)
}.isDefined => Seq(Split(NoSplit, 0))

case FormatToken(left, _: T.Dot, _)
if rightOwner.is[Term.Select] ||
(rightOwner.is[Term.Match] && dialect.allowMatchAsOperator) =>
case FormatToken(left, _: T.Dot, GetSelectLike.OnRight(thisSelect)) =>
val enclosed = style.encloseSelectChains
val (expireTree, nextSelect) =
findLastApplyAndNextSelect(rightOwner, enclosed)
val thisSelect = rightOwner match {
case x: Term.Select => SelectLike(x)
case x: Term.Match => SelectLike(x, getKwMatchAfterDot(ft))
}
val (prevSelect, prevApply) =
findPrevSelectAndApply(thisSelect.qual, enclosed)
val nlOnly = prevApply.exists(_.argClause.tokens.head.is[T.Colon])
Expand Down Expand Up @@ -1637,8 +1632,7 @@ class Router(formatOps: FormatOps) {
}

val prevChain = inSelectChain(prevSelect, thisSelect, expireTree)
val nextSelectTree = nextSelect.map(_.tree)
if (canStartSelectChain(thisSelect, nextSelectTree, expireTree)) {
if (canStartSelectChain(thisSelect, nextSelect, expireTree)) {
val chainExpire =
if (nextSelect.isEmpty) thisSelect.nameToken else expire
val nestedPenalty = nestedSelect(rightOwner) +
Expand Down
Loading