Skip to content

Commit

Permalink
FormatTokensRewrite: include all rules in Session
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 7, 2024
1 parent 42937e6 commit bc5fd14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.scalafmt.rewrite

import scala.annotation.tailrec
import scala.collection.mutable
import scala.reflect.ClassTag

import scala.meta.Tree
import scala.meta.tokens.{Token => T}

Expand Down Expand Up @@ -117,7 +119,7 @@ class FormatTokensRewrite(
* - for standalone tokens, simply invoke the rule and record any rewrites
*/
private def getRewrittenTokens: Iterable[Replacement] = {
implicit val session: Session = new Session
implicit val session: Session = new Session(rules)
val tokens = session.tokens
val leftDelimIndex = new mutable.ListBuffer[(Int, Option[Rule])]()
val formatOffStack = new mutable.ListBuffer[Boolean]()
Expand Down Expand Up @@ -222,11 +224,6 @@ object FormatTokensRewrite {
private[rewrite] trait RuleFactory {
def enabled(implicit style: ScalafmtConfig): Boolean
def create(implicit ftoks: FormatTokens): Rule
final def createIfRequested(implicit
ftoks: FormatTokens,
style: ScalafmtConfig
): Option[Rule] =
if (getFactories.contains(this) && enabled) Some(create) else None
}

private def getFactories(implicit style: ScalafmtConfig): Seq[RuleFactory] =
Expand All @@ -244,7 +241,7 @@ object FormatTokensRewrite {
else new FormatTokensRewrite(ftoks, styleMap, rules).rewrite
}

private[rewrite] class Session {
private[rewrite] class Session(rules: Seq[Rule]) {
private implicit val implicitSession: Session = this
private val claimed = new mutable.HashMap[Int, Rule]()
private[FormatTokensRewrite] val tokens =
Expand Down Expand Up @@ -276,6 +273,8 @@ object FormatTokensRewrite {
iter(rules)
}

def rule[A](implicit tag: ClassTag[A]): Option[Rule] =
rules.find(tag.runtimeClass.isInstance)
}

private[rewrite] class Replacement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,9 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case x: Token.LeftParen =>
ftoks.matchingOpt(x) match {
case Some(y) if y ne stat.tokens.last =>
RedundantParens.createIfRequested.exists {
_.onToken(ftoks(x, -1), session, style).exists {
_.how eq ReplacementType.Remove
}
session.rule[RedundantParens].exists {
_.onToken(ftoks(x, -1), session, style)
.exists(_.how eq ReplacementType.Remove)
}
case _ => true
}
Expand Down

0 comments on commit bc5fd14

Please sign in to comment.