Skip to content

Commit

Permalink
FormatTokens: implement prevNonCommentBefore
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 14, 2022
1 parent 9daeb67 commit 0990bcd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class FormatOps(
prev,
next,
prevNonComment,
prevNonCommentBefore,
nextNonComment,
nextNonCommentSameLine
}
Expand Down Expand Up @@ -1752,7 +1753,7 @@ class FormatOps(
case t: Term.If if isKeep || ifWithoutElse(t) || hasStateColumn =>
val thenBeg = tokens.getHead(t.thenp)
val thenHasLB = thenBeg.left.is[T.LeftBrace]
val end = if (thenHasLB) thenBeg else prevNonComment(prev(thenBeg))
val end = if (thenHasLB) thenBeg else prevNonCommentBefore(thenBeg)
getSplits(getSlbSplit(end.left))
case _: Term.If => getSlbSplits()
case _: Term.Try | _: Term.TryWithHandler =>
Expand Down Expand Up @@ -1940,7 +1941,7 @@ class FormatOps(
soft: SoftKeywordClasses
): Boolean = ft.left match {
case _: T.KwImplicit => true
case soft.KwUsing() => prevNonComment(prev(ft)).left.is[T.LeftParen]
case soft.KwUsing() => prevNonCommentBefore(ft).left.is[T.LeftParen]
case _ => false
}

Expand Down Expand Up @@ -2071,7 +2072,7 @@ class FormatOps(
else {
val maybeClose = prevNonComment(end)
if (!tokens.isCloseMatchingHead(maybeClose.left)(tree)) None
else Some(prevNonComment(prev(maybeClose)).left)
else Some(prevNonCommentBefore(maybeClose).left)
}
def nlPolicy(implicit fileLine: FileLine) =
if (danglingKeyword)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class FormatTokens(leftTok2tok: Map[TokenOps.TokenHash, Int])(
final def prevNonComment(curr: FormatToken): FormatToken =
findToken(curr, prev)(!_.left.is[Token.Comment]).fold(identity, identity)

@inline
final def prevNonCommentBefore(curr: FormatToken): FormatToken =
prevNonComment(prev(curr))

def getHead(tree: Tree): FormatToken =
after(tree.tokens.head)
def getHeadOpt(tree: Tree): Option[FormatToken] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Router(formatOps: FormatOps) {
tokenBefore,
getLastNonTrivial,
prevNonComment,
prevNonCommentBefore,
nextNonComment,
prevNonCommentSameLine,
nextNonCommentSameLine
Expand Down Expand Up @@ -2339,7 +2340,7 @@ class Router(formatOps: FormatOps) {
val argsOrParamsOpt = formatToken.meta.leftOwner match {
// for the using argument list
case _: ApplyUsing => Some(getApplyArgs(formatToken, false).args)
case _ => opensImplicitParamList(prevNonComment(prev(formatToken)))
case _ => opensImplicitParamList(prevNonCommentBefore(formatToken))
}
argsOrParamsOpt.fold {
Seq(Split(Space, 0))
Expand Down

0 comments on commit 0990bcd

Please sign in to comment.