Skip to content

Commit

Permalink
Newlines: add {type,pat}Site to infix
Browse files Browse the repository at this point in the history
With one already present for Term.ApplyInfix, let's add additional ones
for Type.ApplyInfix and Pat.ExtractInfix.
  • Loading branch information
kitbellew committed Jan 1, 2025
1 parent 71d0225 commit b87d175
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 75 deletions.
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,12 @@ expressions. It contains the following parameter groups:

- `termSite`
- primarily applies to ordinary infix expressions (`Term.ApplyInfix`)
- `typeSite`:
- applies to infix expressions within types (`Type.ApplyInfix`) only
- defaults to `termSite` if _no fields_ are specified
- `patSite`:
- applies to infix expressions within patterns (`Pat.ExtractInfix`) only
- defaults to `termSite` if _no fields_ are specified

Each of these groups has several parameters of its own (replacing deprecated
`newlines.afterInfixXxx`, originally added in v2.5.0, which moved to `termSite`):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ case class Newlines(
@inline
def keepBreak(implicit ft: FT): Boolean = keepBreak(ft.hasBreak)

def checkInfixConfig(termCnt: Int)(implicit
cfg: ScalafmtConfig, // TODO: add typeSite, patSite
): Newlines = copy(infix = infix.checkInfixCounts(termCnt))
def checkInfixConfig(termCnt: Int, typeCnt: Int, patCnt: Int)(implicit
cfg: ScalafmtConfig,
): Newlines = copy(infix = infix.checkInfixCounts(termCnt, typeCnt, patCnt))

lazy val forceBeforeImplicitParamListModifier: Boolean =
implicitParamListModifierForce.contains(before)
Expand Down Expand Up @@ -382,22 +382,27 @@ object Newlines {

case class Infix(
private val termSite: Infix.Site = Infix.Site.default,
// TODO: add typeSite and patSite
private val typeSite: Option[Infix.Site] = None,
private val patSite: Option[Infix.Site] = None,
) {
def checkInfixCounts(termCnt: Int)(implicit
cfg: ScalafmtConfig, // TODO: add typeSite, patSite
def checkInfixCounts(termCnt: Int, typeInfix: Int, patInfix: Int)(implicit
cfg: ScalafmtConfig,
): Infix = copy(
// TODO: add typeSite, patSite
termSite = termSite.checkConfig(termCnt)(None),
typeSite = Some(typeSite.getOrElse(termSite).checkConfig(typeInfix) {
val useSome = !cfg.newlines.keep && cfg.dialect.useInfixTypePrecedence
if (useSome) Some(Infix.some) else None
}),
patSite = Some(patSite.getOrElse(termSite).checkConfig(patInfix)(None)),
)

def get(tree: Tree)(implicit cfg: ScalafmtConfig) = {
val useSome = (termSite.style eq null) && !cfg.newlines.keep &&
tree.is[Type] && cfg.dialect.useInfixTypePrecedence
if (useSome) termSite.copy(style = Infix.some) else termSite
def get(tree: Tree): Infix.Site = tree match {
case _: Type => typeSite.getOrElse(termSite)
case _: Pat => patSite.getOrElse(termSite)
case _ => termSite
}

def keep(tree: Tree)(implicit cfg: ScalafmtConfig) = get(tree).isKeep
def keep(tree: Tree): Boolean = get(tree).isKeep
}

object Infix {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ class Router(formatOps: FormatOps) {
if (noSplitMod == null) Seq.empty
else if (onlyConfigStyle) Seq(
Split(noSplitMod, 0).withPolicy(oneArgOneLine & implicitPolicy)
.withOptimalToken(afterOpen, killOnFail = true)
.withOptimalToken(afterscalaOpen, killOnFail = true)
.withIndents(extraOneArgPerLineIndent, indent),
)
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,9 @@ object TreeOps {
topSourceTree: Tree,
baseStyle: ScalafmtConfig,
): (ScalafmtConfig, collection.Map[TokenHash, Tree]) = {
var infixCount = 0
var termInfixCount = 0
var typeInfixCount = 0
var patInfixCount = 0
// Creates lookup table from token offset to its closest scala.meta tree
val ownersMap = HashMap.newBuilder[TokenHash, Tree]
@inline
Expand All @@ -789,7 +791,12 @@ object TreeOps {
elemEnd: T,
outerPrevLPs: Int,
): Int = {
if (TreeOps.isInfixApp(elem)) infixCount += 1
elem match {
case _: Term.ApplyInfix => termInfixCount += 1
case _: Type.ApplyInfix => typeInfixCount += 1
case _: Pat.ExtractInfix => patInfixCount += 1
case _ =>
}

val treeBeg = elemBeg.start
val treeEnd = elemEnd.end
Expand Down Expand Up @@ -888,7 +895,7 @@ object TreeOps {
treeAt(0, topSourceTree, allTokens.head, allTokens.last, 0)

val checkedNewlines = baseStyle.newlines
.checkInfixConfig(infixCount)(baseStyle)
.checkInfixConfig(termInfixCount, typeInfixCount, patInfixCount)(baseStyle)
val initStyle =
if (checkedNewlines eq baseStyle.newlines) baseStyle
else baseStyle.copy(newlines = checkedNewlines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class CommunityScala2Suite(name: String)

class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") {

override protected def totalStatesVisited: Option[Int] = Some(42512477)
override protected def totalStatesVisited: Option[Int] = Some(42520687)

override protected def builds =
Seq(getBuild("v2.12.20", dialects.Scala212, 1277))
Expand All @@ -18,7 +18,7 @@ class CommunityScala2_12Suite extends CommunityScala2Suite("scala-2.12") {

class CommunityScala2_13Suite extends CommunityScala2Suite("scala-2.13") {

override protected def totalStatesVisited: Option[Int] = Some(52980238)
override protected def totalStatesVisited: Option[Int] = Some(52986574)

override protected def builds =
Seq(getBuild("v2.13.14", dialects.Scala213, 1287))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ abstract class CommunityScala3Suite(name: String)

class CommunityScala3_2Suite extends CommunityScala3Suite("scala-3.2") {

override protected def totalStatesVisited: Option[Int] = Some(39189350)
override protected def totalStatesVisited: Option[Int] = Some(39189914)

override protected def builds = Seq(getBuild("3.2.2", dialects.Scala32, 791))

}

class CommunityScala3_3Suite extends CommunityScala3Suite("scala-3.3") {

override protected def totalStatesVisited: Option[Int] = Some(42295896)
override protected def totalStatesVisited: Option[Int] = Some(42296538)

override protected def builds = Seq(getBuild("3.3.3", dialects.Scala33, 861))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11671,6 +11671,8 @@ object ops {
} = macro foo
}
<<< #4705 term=src, type=many
newlines.infix.typeSite.style = many
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
???
Expand All @@ -11688,18 +11690,11 @@ object Foo {
with Align[Option] =
???

def newStyle: Traverse[
Option
] & MonadError[
Option,
Unit
] & Alternative[
Option
] & CommutativeMonad[
Option
] & CoflatMap[Option] & Align[
Option
] =
def newStyle: Traverse[Option] &
MonadError[Option, Unit] &
Alternative[Option] &
CommutativeMonad[Option] &
CoflatMap[Option] & Align[Option] =
Traverse[Option] & MonadError[
Option,
Unit
Expand All @@ -11714,6 +11709,7 @@ object Foo {
<<< #4705 term=some, type=keep
maxColumn = 80
newlines.infix.termSite.style = some
newlines.infix.typeSite.style = keep
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -11732,15 +11728,16 @@ object Foo {
with Align[Option] =
???

def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] =
def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[Option] &
CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
}
<<< #4705 term=keep, type=some
maxColumn = 80
newlines.infix.termSite.style = keep
newlines.infix.typeSite.style = some
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -11759,9 +11756,9 @@ object Foo {
with Align[Option] =
???

def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
Expand Down
23 changes: 13 additions & 10 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -10896,6 +10896,8 @@ object ops {
} = macro foo
}
<<< #4705 term=src, type=many
newlines.infix.typeSite.style = many
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
???
Expand Down Expand Up @@ -10926,6 +10928,7 @@ object Foo {
<<< #4705 term=some, type=keep
maxColumn = 80
newlines.infix.termSite.style = some
newlines.infix.typeSite.style = keep
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -10943,15 +10946,16 @@ object Foo {
with CoflatMap[Option]
with Align[Option] = ???

def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] = Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option]
def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[Option] &
CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
}
<<< #4705 term=keep, type=some
maxColumn = 80
newlines.infix.termSite.style = keep
newlines.infix.typeSite.style = some
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -10969,10 +10973,9 @@ object Foo {
with CoflatMap[Option]
with Align[Option] = ???

def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] = Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
}
32 changes: 15 additions & 17 deletions scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -11432,6 +11432,8 @@ object ops {
} = macro foo
}
<<< #4705 term=src, type=many
newlines.infix.typeSite.style = many
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
???
Expand All @@ -11449,17 +11451,11 @@ object Foo {
with Align[Option] =
???

def newStyle
: Traverse[Option] & MonadError[
Option,
Unit
] & Alternative[
Option
] & CommutativeMonad[
Option
] & CoflatMap[Option] & Align[
Option
] =
def newStyle: Traverse[Option] &
MonadError[Option, Unit] &
Alternative[Option] &
CommutativeMonad[Option] &
CoflatMap[Option] & Align[Option] =
Traverse[Option] & MonadError[
Option,
Unit
Expand All @@ -11472,6 +11468,7 @@ object Foo {
<<< #4705 term=some, type=keep
maxColumn = 80
newlines.infix.termSite.style = some
newlines.infix.typeSite.style = keep
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -11490,15 +11487,16 @@ object Foo {
with Align[Option] =
???

def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] =
def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[Option] &
CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
}
<<< #4705 term=keep, type=some
maxColumn = 80
newlines.infix.termSite.style = keep
newlines.infix.typeSite.style = some
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -11517,9 +11515,9 @@ object Foo {
with Align[Option] =
???

def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11875,6 +11875,8 @@ object ops {
} = macro foo
}
<<< #4705 term=src, type=many
newlines.infix.typeSite.style = many
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
???
Expand Down Expand Up @@ -11905,6 +11907,7 @@ object Foo {
<<< #4705 term=some, type=keep
maxColumn = 80
newlines.infix.termSite.style = some
newlines.infix.typeSite.style = keep
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -11922,15 +11925,16 @@ object Foo {
with CoflatMap[Option]
with Align[Option] = ???

def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] =
def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[Option] &
CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
}
<<< #4705 term=keep, type=some
maxColumn = 80
newlines.infix.termSite.style = keep
newlines.infix.typeSite.style = some
===
object Foo {
def oldStyle: Traverse[Option] with MonadError[Option, Unit] with Alternative[Option] with CommutativeMonad[Option] with CoflatMap[Option] with Align[Option] =
Expand All @@ -11948,9 +11952,9 @@ object Foo {
with CoflatMap[Option]
with Align[Option] = ???

def newStyle: Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option] =
def newStyle: Traverse[Option] & MonadError[Option, Unit] &
Alternative[Option] & CommutativeMonad[Option] & CoflatMap[Option] &
Align[Option] =
Traverse[Option] & MonadError[Option, Unit] & Alternative[
Option
] & CommutativeMonad[Option] & CoflatMap[Option] & Align[Option]
Expand Down
Loading

0 comments on commit b87d175

Please sign in to comment.