Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatTokensRewrite: include all rules in Session #3816

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading