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

BestFirstSearch: use visual token width to recurse #4488

Merged
merged 1 commit into from
Oct 28, 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 @@ -41,7 +41,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit
): Option[Token] = getEndOfBlock(ft, parensToo = true).collect {
case close if close.left != stop && {
// Block must span at least 3 lines to be worth recursing.
tokens.span(ft, close) > style.maxColumn * 3
tokens.width(ft, close) > style.maxColumn * 3
} => close.left
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,21 +394,31 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(val arr: Array[FormatToken]
}

// Maps token to number of non-whitespace bytes before the token's position.
private final lazy val nonWhitespaceOffset: Array[Int] = {
val result = new Array[Int](arr.length)
var curr = 0
private final lazy val nonWhitespaceOffset: Array[(Int, Int)] = {
val result = new Array[(Int, Int)](arr.length)
var currLength = 0
var currWidth = 0
arr.foreach { t =>
result(t.idx) = curr
curr += t.left.len
result(t.idx) = (currLength, currWidth)
val (head, last) = State
.getColumns(t.left, t.meta.left, 0)(identity)(identity)
currLength += t.left.len
currWidth += head.max(last)
}
result
}

def span(left: FormatToken, right: FormatToken): Int = {
private def spanOrWidth(left: FormatToken, right: FormatToken)(
f: ((Int, Int)) => Int,
): Int = {
val lastIdx = nonWhitespaceOffset.length - 1
val rightIdx = if (right.idx >= lastIdx) lastIdx else right.idx + 1
nonWhitespaceOffset(rightIdx) - nonWhitespaceOffset(left.idx)
f(nonWhitespaceOffset(rightIdx)) - f(nonWhitespaceOffset(left.idx))
}
def width(left: FormatToken, right: FormatToken): Int =
spanOrWidth(left, right)(_._2)
def span(left: FormatToken, right: FormatToken): Int =
spanOrWidth(left, right)(_._1)
def span(tokens: Tokens): Int =
if (tokens.isEmpty) 0 else span(getHeadImpl(tokens), getLastImpl(tokens))
@inline
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(35259833)
override protected def totalStatesVisited: Option[Int] = Some(35259819)

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

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

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

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

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

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 @@ -137,7 +137,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, 1487968, "total explored")
assertEquals(explored, 1488002, "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