From eb8f4144e09cd171614b56b007e397690b25b762 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 11 Sep 2024 12:12:57 -0700 Subject: [PATCH] TokenClasses: add matches/apply, to join unapply --- .../org/scalafmt/internal/FormatOps.scala | 4 +-- .../scala/org/scalafmt/internal/Router.scala | 9 +++--- .../org/scalafmt/util/TokenClasses.scala | 30 +++++++++++-------- 3 files changed, 24 insertions(+), 19 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 80635a045c..693013e6f5 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 @@ -867,7 +867,7 @@ class FormatOps( /** Works for `using` as well */ def opensConfigStyleImplicitParamList(formatToken: FormatToken)(implicit style: ScalafmtConfig, - ): Boolean = soft.ImplicitOrUsing.unapply(formatToken.right) && + ): Boolean = soft.ImplicitOrUsing(formatToken.right) && style.newlines.notBeforeImplicitParamListModifier && hasImplicitParamList(formatToken.meta.rightOwner) @@ -1160,7 +1160,7 @@ class FormatOps( val nlSplit = Split(Newline, 1, policy = policy).withIndent(firstIndent) Seq(slbSplit, noSplit.andPolicy(noSlbPolicy), nlSplit) } else { - val rightIsImplicit = soft.ImplicitOrUsing.unapply(r) + val rightIsImplicit = soft.ImplicitOrUsing(r) val implicitNL = rightIsImplicit && style.newlines.forceBeforeImplicitParamListModifier val implicitParams = 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 330354844c..f3c9c0a584 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 @@ -570,7 +570,7 @@ class Router(formatOps: FormatOps) { _: T.RightParen, _, ParamClauseParentLeft(extGroup: Defn.ExtensionGroup), - ) if !LeftParenOrBrace.unapply(nextNonComment(ft).right) => + ) if !LeftParenOrBrace(nextNonComment(ft).right) => if (dialect.allowSignificantIndentation) { val expireToken = getLastToken(extGroup) def nlSplit(cost: Int = 0)(implicit fileLine: FileLine) = @@ -600,10 +600,9 @@ class Router(formatOps: FormatOps) { case Newlines.unfold => right.is[T.Comment] || !style.optIn.annotationNewlines && annoRight case Newlines.fold => right.is[T.Comment] || annoRight || - !style.optIn.annotationNewlines && Reserved.unapply(right) - case Newlines.keep => noBreak() && - (annoRight || Reserved.unapply(right)) - case _ => noBreak() && Reserved.unapply(right) + !style.optIn.annotationNewlines && Reserved(right) + case Newlines.keep => noBreak() && (annoRight || Reserved(right)) + case _ => noBreak() && Reserved(right) }) def expire = (rightOwner match { case Tree.WithBody(body) => tokenBeforeOpt(body) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenClasses.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenClasses.scala index cce04ab805..c367ffe8fc 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenClasses.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenClasses.scala @@ -5,32 +5,38 @@ import scala.meta.internal.parsers.SoftKeywords import scala.meta.tokens.Token import scala.meta.tokens.Token._ -object Reserved { - def unapply(token: Token): Boolean = token match { +sealed trait TokenClassifier extends Function[Token, Boolean] { + def matches(token: Token): Boolean + def apply(token: Token): Boolean = matches(token) + def unapply(token: Token): Boolean = matches(token) +} + +object Reserved extends TokenClassifier { + def matches(token: Token): Boolean = token match { case _: Keyword | _: KwFalse | _: KwNull | _: KwTrue => true case _ => false } } -object LeftParenOrBracket { - def unapply(tok: Token): Boolean = tok.is[LeftParen] || tok.is[LeftBracket] +object LeftParenOrBracket extends TokenClassifier { + def matches(tok: Token): Boolean = tok.isAny[LeftParen, LeftBracket] } -object RightParenOrBracket { - def unapply(tok: Token): Boolean = tok.is[RightParen] || tok.is[RightBracket] +object RightParenOrBracket extends TokenClassifier { + def matches(tok: Token): Boolean = tok.isAny[RightParen, RightBracket] } -object LeftParenOrBrace { - def unapply(tok: Token): Boolean = tok.is[LeftParen] || tok.is[LeftBrace] +object LeftParenOrBrace extends TokenClassifier { + def matches(tok: Token): Boolean = tok.isAny[LeftParen, LeftBrace] } class SoftKeywordClasses(dialect: Dialect) extends SoftKeywords(dialect) { - object ImplicitOrUsing { - def unapply(tok: Token): Boolean = tok.is[KwImplicit] || KwUsing.unapply(tok) + object ImplicitOrUsing extends TokenClassifier { + def matches(tok: Token): Boolean = tok.is[KwImplicit] || KwUsing.unapply(tok) } - object ExtendsOrDerives { - def unapply(tok: Token): Boolean = tok.is[KwExtends] || + object ExtendsOrDerives extends TokenClassifier { + def matches(tok: Token): Boolean = tok.is[KwExtends] || KwDerives.unapply(tok) } }