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

Router: slightly penalize interpolation newlines #4442

Merged
merged 3 commits into from
Oct 14, 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 @@ -107,13 +107,18 @@ class FormatOps(
) start
else null
}
case _: T.LeftBrace
if start.left.is[T.RightParen] &&
start.leftOwner.is[Member.SyntaxValuesClause] &&
start.rightOwner.parent.exists {
case p: Tree.WithBody => p.body eq start.rightOwner
case _ => false
} => null
case _: T.LeftBrace => start.left match {
case _: T.RightParen
if start.leftOwner.is[Member.SyntaxValuesClause] &&
start.rightOwner.parent.exists {
case p: Tree.WithBody => p.body eq start.rightOwner
case _ => false
} => null
case _: T.Interpolation.SpliceStart
if style.newlines.inInterpolation eq
Newlines.InInterpolation.allow => null
case _ => start
}
case _: T.RightBracket if start.left.is[T.RightBracket] => null
case _: T.LeftBracket => null
case _: T.Dot => start.rightOwner match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ class Router(formatOps: FormatOps) {
else s.copy(optimalAt = None)
}
}
else Policy ?
(style.newlines.sourceIgnored || isTripleQuote(m.left.text)) ||
else if (!style.newlines.sourceIgnored && !isTripleQuote(m.left.text))
Policy.on(end, "INTERP-KEEP-NONL") {
case Decision(x, ss) if x.noBreak =>
ss.map(s => if (s.isNL) s.withPenalty(penalty) else s)
}
else Policy ?
(style.newlines.inInterpolation eq
Newlines.InInterpolation.allow) &&
Policy.on(end, "INTERP-ALLOW-NL", rank = -1) {
case Decision(_, ss) => ss
.map(s => if (s.isNL) s.withPenalty(1) else s)
}
}
val split = Split(NoSplit, 0, policy = policy)
val alignIndents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class CommunityIntellijScalaSuite(name: String)
class CommunityIntellijScala_2024_2_Suite
extends CommunityIntellijScalaSuite("intellij-scala-2024.2") {

override protected def totalStatesVisited: Option[Int] = Some(48186930)
override protected def totalStatesVisited: Option[Int] = Some(48190466)

override protected def builds = Seq(getBuild(
"2024.2.28",
Expand Down Expand Up @@ -51,7 +51,7 @@ class CommunityIntellijScala_2024_2_Suite
class CommunityIntellijScala_2024_3_Suite
extends CommunityIntellijScalaSuite("intellij-scala-2024.3") {

override protected def totalStatesVisited: Option[Int] = Some(48366702)
override protected def totalStatesVisited: Option[Int] = Some(48370242)

override protected def builds = Seq(getBuild(
"2024.3.4",
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(35850116)
override protected def totalStatesVisited: Option[Int] = Some(35856475)

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(44610131)
override protected def totalStatesVisited: Option[Int] = Some(44618135)

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(33372940)
override protected def totalStatesVisited: Option[Int] = Some(33385186)

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(36020956)
override protected def totalStatesVisited: Option[Int] = Some(36033019)

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 @@ -9,7 +9,7 @@ abstract class CommunitySparkSuite(name: String)

class CommunitySpark3_4Suite extends CommunitySparkSuite("spark-3.4") {

override protected def totalStatesVisited: Option[Int] = Some(72459436)
override protected def totalStatesVisited: Option[Int] = Some(72480315)

override protected def builds =
Seq(getBuild("v3.4.1", dialects.Scala213, 2585))
Expand All @@ -18,7 +18,7 @@ class CommunitySpark3_4Suite extends CommunitySparkSuite("spark-3.4") {

class CommunitySpark3_5Suite extends CommunitySparkSuite("spark-3.5") {

override protected def totalStatesVisited: Option[Int] = Some(76628137)
override protected def totalStatesVisited: Option[Int] = Some(76649844)

override protected def builds =
Seq(getBuild("v3.5.3", dialects.Scala213, 2756))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10503,3 +10503,19 @@ val output = evaluated match {
|"$s"""".stripMargin
case x => x.toString
}
<<< #4133 breaks in overflowing interpolation, allow
maxColumn = 76
===
object a {
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options.common.debug.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
}
>>>
object a {
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options.common.debug.print(
s"Files to be formatted:\n${projectFiles.mkString("\n")}\n"
)
}
15 changes: 15 additions & 0 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9811,3 +9811,18 @@ val output = evaluated match {
|"$s"""".stripMargin
case x => x.toString
}
<<< #4133 breaks in overflowing interpolation, allow
maxColumn = 76
===
object a {
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options.common.debug.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
}
>>>
object a {
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
}
16 changes: 16 additions & 0 deletions scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -10252,3 +10252,19 @@ val output = evaluated match {
|"$s"""".stripMargin
case x => x.toString
}
<<< #4133 breaks in overflowing interpolation, allow
maxColumn = 76
===
object a {
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options.common.debug.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
}
>>>
object a {
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options.common.debug.print(
s"Files to be formatted:\n${projectFiles.mkString("\n")}\n"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10629,3 +10629,22 @@ val output =
case x =>
x.toString
}
<<< #4133 breaks in overflowing interpolation, allow
maxColumn = 76
===
object a {
options.common.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options.common.debug.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
}
>>>
object a {
options
.common
.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
options
.common
.debug
.print(s"Files to be formatted:\n${projectFiles.mkString("\n")}\n")
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1495532, "total explored")
assertEquals(explored, 1496399, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down
Loading