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

FormatTokens: add nextNonCommentAfter #3833

Merged
merged 1 commit into from
Mar 16, 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 @@ -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 @@ -153,6 +153,14 @@ class FormatTokens(leftTok2tok: Map[TokenOps.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 @@ -176,7 +184,7 @@ class FormatTokens(leftTok2tok: Map[TokenOps.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
Loading