Skip to content

Commit

Permalink
FormatTokens: add nextNonCommentAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 16, 2024
1 parent 342ffa6 commit 48c599f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ class FormatOps(
private def getArrowAfter(ft: FormatToken): FormatToken = {
val maybeArrow = next(ft)
if (maybeArrow.left.is[T.RightArrow]) maybeArrow
else next(nextNonComment(maybeArrow))
else tokens.nextAfterNonComment(maybeArrow)
}

def getArgs(owner: Tree, orNil: Boolean = false): Seq[Tree] =
Expand Down Expand Up @@ -1914,7 +1914,7 @@ class FormatOps(
} else None

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

object GetSelectLike {
def unapply(tree: Tree): Option[SelectLike] = tree match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(
final def nextNonComment(curr: FormatToken.Meta): FormatToken =
nextNonComment(arr(curr.idx))

@inline
final def nextNonCommentAfter(curr: FormatToken): FormatToken =
nextNonComment(next(curr))

@inline
final def nextAfterNonComment(curr: FormatToken): FormatToken =
next(nextNonComment(curr))

final def prevNonCommentSameLine(curr: FormatToken): FormatToken =
findToken(curr, prev)(ft => ft.hasBreak || !ft.left.is[Token.Comment])

Expand All @@ -194,7 +202,7 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(

@tailrec
final def getOnOrAfterOwned(ft: FormatToken, tree: Tree): FormatToken = {
val nextFt = next(nextNonComment(ft))
val nextFt = nextAfterNonComment(ft)
if (nextFt == ft || nextFt.meta.leftOwner != tree) ft
else getOnOrAfterOwned(nextFt, tree)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Router(formatOps: FormatOps) {
getLastNonTrivial,
prevNonComment,
nextNonComment,
nextAfterNonComment,
nextNonCommentAfter,
prevNonCommentSameLine,
nextNonCommentSameLine
}
Expand Down Expand Up @@ -300,7 +302,7 @@ class Router(formatOps: FormatOps) {
val indent = style.indent.main
val annoFT = tokens(anno)
val arrow = annoFT.left.is[T.RightArrow]
val expire = if (arrow) annoFT else next(nextNonComment(annoFT))
val expire = if (arrow) annoFT else nextAfterNonComment(annoFT)
(expire, Some(expire.left), indent, Some(isSelfAnnotationNL))
}
annoOpt.getOrElse { (null, None, 0, None) }
Expand Down Expand Up @@ -1436,7 +1438,7 @@ class Router(formatOps: FormatOps) {
val expire = expireFt.left
val sameLineSplit = Space(endsWithSymbolIdent(left))
val bopSplits = style.newlines.getBeforeOpenParenDefnSite.map { x =>
val ob = OptionalBraces.get(next(nextNonComment(expireFt))).nonEmpty
val ob = OptionalBraces.get(nextAfterNonComment(expireFt)).nonEmpty
def extraIfBody = style.indent.extraBeforeOpenParenDefnSite
val indent =
if (ob) style.indent.getSignificant + extraIfBody
Expand Down Expand Up @@ -2442,7 +2444,7 @@ class Router(formatOps: FormatOps) {
val baseSplit = Split(mod, 0)
rightOwner match {
case GetSelectLike(t) =>
val expire = nextNonComment(next(formatToken)).left
val expire = nextNonCommentAfter(formatToken).left
val indent = Indent(style.indent.main, expire, ExpiresOn.After)
val split = baseSplit.withIndent(indent)
if (findPrevSelect(t, style.encloseSelectChains).isEmpty) Seq(split)
Expand Down

0 comments on commit 48c599f

Please sign in to comment.