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

FormatOps: pass FormatTokens as implicit #3807

Merged
merged 1 commit into from
Mar 3, 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 @@ -27,7 +27,6 @@ private class BestFirstSearch private (
import formatOps.runner.optimizer._

implicit val stateOrdering: Ordering[State] = State.Ordering
implicit val tokens: FormatTokens = formatOps.tokens

/** Precomputed table of splits for each token.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class FormatOps(

val runner: ScalafmtRunner = initStyle.runner
implicit val dialect: Dialect = initStyle.dialect
val (tokens, styleMap) = FormatTokens(topSourceTree.tokens, owners)(initStyle)
implicit val (tokens: FormatTokens, styleMap: StyleMap) =
FormatTokens(topSourceTree.tokens, owners)(initStyle)
import tokens.{
matching,
matchingOpt,
Expand All @@ -60,7 +61,7 @@ class FormatOps(

private[internal] val soft = new SoftKeywordClasses(dialect)
private[internal] val statementStarts =
getStatementStarts(topSourceTree, tokens, soft)
getStatementStarts(topSourceTree, soft)
// Maps token to number of non-whitespace bytes before the token's position.
private final val nonWhitespaceOffset: Map[T, Int] = {
val resultB = Map.newBuilder[T, Int]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object ConvertToNewScala3Syntax extends FormatTokensRewrite.RuleFactory {
override def enabled(implicit style: ScalafmtConfig): Boolean =
style.rewrite.scala3.convertToNewSyntax

override def create(ftoks: FormatTokens): FormatTokensRewrite.Rule =
override def create(implicit ftoks: FormatTokens): FormatTokensRewrite.Rule =
new ConvertToNewScala3Syntax(ftoks)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ object FormatTokensRewrite {

private[rewrite] trait RuleFactory {
def enabled(implicit style: ScalafmtConfig): Boolean
def create(ftoks: FormatTokens): Rule
def create(implicit ftoks: FormatTokens): Rule
}

private def getFactories(implicit style: ScalafmtConfig): Seq[RuleFactory] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object PreferCurlyFors extends Rewrite with FormatTokensRewrite.RuleFactory {

override def enabled(implicit style: ScalafmtConfig): Boolean = true

override def create(ftoks: FormatTokens): FormatTokensRewrite.Rule =
override def create(implicit ftoks: FormatTokens): FormatTokensRewrite.Rule =
new PreferCurlyFors(ftoks)

@inline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {

override def enabled(implicit style: ScalafmtConfig): Boolean = true

override def create(ftoks: FormatTokens): Rule =
new RedundantBraces(ftoks)
override def create(implicit ftoks: FormatTokens): Rule =
new RedundantBraces

def needParensAroundParams(f: Term.FunctionTerm): Boolean =
/* either we have parens or no type; multiple params or
Expand Down Expand Up @@ -66,7 +66,8 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {

/** Removes/adds curly braces where desired.
*/
class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
class RedundantBraces(implicit val ftoks: FormatTokens)
extends FormatTokensRewrite.Rule {

import FormatTokensRewrite._
import RedundantBraces._
Expand Down Expand Up @@ -216,7 +217,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
style: ScalafmtConfig
): Option[Rule] =
if (!style.rewrite.rules.contains(RedundantParens)) None
else Some(RedundantParens.create(ftoks))
else Some(RedundantParens.create)

private def processInterpolation(implicit ft: FormatToken): Boolean = {
def isIdentifierAtStart(value: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object RedundantParens extends Rewrite with FormatTokensRewrite.RuleFactory {

override def enabled(implicit style: ScalafmtConfig): Boolean = true

override def create(ftoks: FormatTokens): FormatTokensRewrite.Rule =
override def create(implicit ftoks: FormatTokens): FormatTokensRewrite.Rule =
new RedundantParens(ftoks)

private def infixNeedsParens(outer: Member.Infix, inner: Tree): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ object RemoveEmptyDocstrings
override def enabled(implicit style: ScalafmtConfig): Boolean =
style.docstrings.removeEmpty

override def create(ftoks: FormatTokens): FormatTokensRewrite.Rule = this
override def create(implicit ftoks: FormatTokens): FormatTokensRewrite.Rule =
this

override def onToken(implicit
ft: FormatToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ object RemoveScala3OptionalBraces extends FormatTokensRewrite.RuleFactory {
style.rewrite.scala3.removeOptionalBraces
.ne(RewriteScala3Settings.RemoveOptionalBraces.no)

override def create(ftoks: FormatTokens): FormatTokensRewrite.Rule =
new RemoveScala3OptionalBraces(ftoks)
override def create(implicit ftoks: FormatTokens): FormatTokensRewrite.Rule =
new RemoveScala3OptionalBraces

}

private class RemoveScala3OptionalBraces(ftoks: FormatTokens)
private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens)
extends FormatTokensRewrite.Rule {

import FormatTokensRewrite._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object RewriteTrailingCommas extends FormatTokensRewrite.RuleFactory {
style.dialect.allowTrailingCommas &&
style.getTrailingCommas.ne(TrailingCommas.keep)

override def create(ftoks: FormatTokens): FormatTokensRewrite.Rule =
override def create(implicit ftoks: FormatTokens): FormatTokensRewrite.Rule =
new RewriteTrailingCommas(ftoks)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ object TreeOps {

def getStatementStarts(
tree: Tree,
ftoks: FormatTokens,
soft: SoftKeywordClasses
): Map[TokenHash, Tree] = {
)(implicit ftoks: FormatTokens): Map[TokenHash, Tree] = {
val ret = Map.newBuilder[TokenHash, Tree]
ret.sizeHint(tree.tokens.length)

Expand Down
Loading