From 0990bcd8fbd5aea194226290f58cb9a9eb2dfd81 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 13 May 2022 14:23:42 -0700 Subject: [PATCH] FormatTokens: implement prevNonCommentBefore --- .../src/main/scala/org/scalafmt/internal/FormatOps.scala | 7 ++++--- .../main/scala/org/scalafmt/internal/FormatTokens.scala | 4 ++++ .../src/main/scala/org/scalafmt/internal/Router.scala | 3 ++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index 7b09920baf..deff4c07d6 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -85,6 +85,7 @@ class FormatOps( prev, next, prevNonComment, + prevNonCommentBefore, nextNonComment, nextNonCommentSameLine } @@ -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 => @@ -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 } @@ -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) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala index 2e9dee73fe..9e33a3179c 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala @@ -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] = diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index e748d9ab6a..a8bb41b7da 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -72,6 +72,7 @@ class Router(formatOps: FormatOps) { tokenBefore, getLastNonTrivial, prevNonComment, + prevNonCommentBefore, nextNonComment, prevNonCommentSameLine, nextNonCommentSameLine @@ -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))