Skip to content

Commit

Permalink
Token: import it everywhere as {T}
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 12, 2024
1 parent c72e614 commit 31756ba
Show file tree
Hide file tree
Showing 28 changed files with 408 additions and 455 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.scalafmt.config

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

import metaconfig._

Expand Down Expand Up @@ -51,16 +51,14 @@ case class BinPack(
style.newlines.keep && parentConstructors.eq(BinPack.ParentCtors.source)

@inline
def callSiteFor(open: Token): BinPack.Site =
callSiteFor(open.is[Token.LeftBracket])
def callSiteFor(open: T): BinPack.Site = callSiteFor(open.is[T.LeftBracket])
def callSiteFor(isBracket: Boolean): BinPack.Site =
(if (isBracket) bracketCallSite else None).getOrElse(callSite)
def callSiteFor(owner: Tree): BinPack.Site =
callSiteFor(owner.is[Type.ArgClause])

@inline
def defnSiteFor(open: Token): BinPack.Site =
defnSiteFor(open.is[Token.LeftBracket])
def defnSiteFor(open: T): BinPack.Site = defnSiteFor(open.is[T.LeftBracket])
def defnSiteFor(isBracket: Boolean): BinPack.Site =
(if (isBracket) bracketDefnSite else None).getOrElse(defnSite)
def defnSiteFor(owner: Tree): BinPack.Site =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.scalafmt.config

import org.scalafmt.util.TokenOps

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

import metaconfig._

Expand Down Expand Up @@ -65,8 +65,8 @@ case class Spaces(
private val afterColonInMatchPattern: Spaces.AfterColonInMatchPattern =
Spaces.AfterColonInMatchPattern.Always,
) {
def isSpaceAfterKeyword(tokenAfter: Token): Boolean =
afterKeywordBeforeParen || !tokenAfter.is[Token.LeftParen]
def isSpaceAfterKeyword(tokenAfter: T): Boolean = afterKeywordBeforeParen ||
!tokenAfter.is[T.LeftParen]

def notAfterColon(owner: meta.Tree): Boolean = owner match {
case x: meta.Pat.Typed => afterColonInMatchPattern match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.scalafmt.config._
import org.scalafmt.util._

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

import scala.annotation.tailrec
import scala.collection.mutable
Expand Down Expand Up @@ -36,9 +36,9 @@ private class BestFirstSearch private (range: Set[Range])(implicit

var stats = new StateStats(tokens, initStyle.runner)

private def getBlockCloseToRecurse(ft: FormatToken, stop: Token)(implicit
private def getBlockCloseToRecurse(ft: FormatToken, stop: T)(implicit
style: ScalafmtConfig,
): Option[Token] = getEndOfBlock(ft, parensToo = true).collect {
): Option[T] = getEndOfBlock(ft, parensToo = true).collect {
case close if close.left != stop && {
// Block must span at least 3 lines to be worth recursing.
tokens.width(ft, close) > style.maxColumn * 3
Expand All @@ -49,7 +49,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit

def shortestPathMemo(
start: State,
stop: Token,
stop: T,
depth: Int,
isOpt: Boolean,
): Option[Option[State]] = {
Expand All @@ -69,7 +69,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit
*/
def shortestPath(
start: State,
stop: Token,
stop: T,
depth: Int = 0,
isOpt: Boolean = false,
): Either[State, State] = {
Expand Down Expand Up @@ -315,22 +315,22 @@ object BestFirstSearch {
new BestFirstSearch(range).getBestPath

private def getNoOptZones(tokens: FormatTokens)(implicit styleMap: StyleMap) = {
val result = mutable.Map.empty[Token, Boolean]
val result = mutable.Map.empty[T, Boolean]
var expire: FormatToken = null
@inline
def addRange(t: Token): Unit = expire = tokens.matching(t)
def addRange(t: T): Unit = expire = tokens.matching(t)
@inline
def addBlock(t: Token): Unit = result.getOrElseUpdate(t, false)
def addBlock(t: T): Unit = result.getOrElseUpdate(t, false)
tokens.foreach {
case ft if expire ne null =>
if (ft eq expire) expire = null else result.update(ft.left, true)
case FormatToken(t: Token.LeftParen, _, m) if (m.leftOwner match {
case FormatToken(t: T.LeftParen, _, m) if (m.leftOwner match {
case lo: Term.ArgClause => !lo.parent.is[Term.ApplyInfix] &&
!styleMap.at(t).newlines.keep
case _: Term.Apply => true // legacy: when enclosed in parens
case _ => false
}) => addRange(t)
case FormatToken(t: Token.LeftBrace, _, m) => m.leftOwner match {
case FormatToken(t: T.LeftBrace, _, m) => m.leftOwner match {
// Type compounds can be inside defn.defs
case lo: meta.Stat.Block if lo.parent.is[Type.Refine] => addRange(t)
case _: Type.Refine => addRange(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.scalafmt.util.LoggerOps
import org.scalafmt.util.TokenOps._

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

import scala.annotation.tailrec

Expand All @@ -21,7 +21,7 @@ import scala.annotation.tailrec
* @param meta
* Extra information about the token
*/
case class FormatToken(left: Token, right: Token, meta: FormatToken.Meta) {
case class FormatToken(left: T, right: T, meta: FormatToken.Meta) {

override def toString = {
val ws = newlinesBetween match {
Expand All @@ -46,8 +46,8 @@ case class FormatToken(left: Token, right: Token, meta: FormatToken.Meta) {
val nl = meta.newlinesBetween
// make sure to break before/after docstring
if (nl != 0) nl
else if (left.is[Token.Comment] && isDocstring(meta.left.text)) 1
else if (right.is[Token.Comment] && isDocstring(meta.right.text)) 1
else if (left.is[T.Comment] && isDocstring(meta.left.text)) 1
else if (right.is[T.Comment] && isDocstring(meta.right.text)) 1
else 0
}
@inline
Expand All @@ -62,11 +62,11 @@ case class FormatToken(left: Token, right: Token, meta: FormatToken.Meta) {
@inline
def rightHasNewline = meta.right.hasNL
@inline
def hasBreakOrEOF: Boolean = hasBreak || right.is[Token.EOF]
def hasBreakOrEOF: Boolean = hasBreak || right.is[T.EOF]

def hasCRLF: Boolean = between.exists {
case _: Token.CRLF => true
case t: Token.MultiNL => t.tokens.exists(_.is[Token.CRLF])
case _: T.CRLF => true
case t: T.MultiNL => t.tokens.exists(_.is[T.CRLF])
case _ => false
}

Expand All @@ -93,10 +93,10 @@ object FormatToken {
def hasBlankLine(newlines: Int): Boolean = newlines > 1

@inline
def isNL(token: Token): Boolean = token.is[Token.AtEOL]
def isNL(token: T): Boolean = token.is[T.AtEOL]
@inline
def newlines(token: Token): Int = token match {
case t: Token.AtEOL => t.newlines
def newlines(token: T): Int = token match {
case t: T.AtEOL => t.newlines
case _ => 0
}

Expand All @@ -108,7 +108,7 @@ object FormatToken {
* if true, between and right should not be formatted
*/
case class Meta(
between: Array[Token],
between: Array[T],
idx: Int,
formatOff: Boolean,
left: TokenMeta,
Expand Down
Loading

0 comments on commit 31756ba

Please sign in to comment.