Skip to content

Commit

Permalink
TokenClasses: add matches/apply, to join unapply
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 12, 2024
1 parent a5e3ee1 commit eb8f414
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit eb8f414

Please sign in to comment.