Skip to content

Commit

Permalink
Error: simplify parameters of SearchStateExploded
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jun 8, 2024
1 parent 7e11d3f commit 45dc931
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
31 changes: 19 additions & 12 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Error.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.scalafmt

import org.scalafmt.internal.Decision
import org.scalafmt.internal.FormatToken
import org.scalafmt.internal.State
import org.scalafmt.internal._
import org.scalafmt.util.LoggerOps

import scala.meta.Case
Expand Down Expand Up @@ -78,17 +76,26 @@ object Error {
case class MisformattedFile(file: Path, customMessage: String)
extends Error(s"$file is mis-formatted. $customMessage")

case class SearchStateExploded(
case class SearchStateExploded private (
deepestState: State,
partialOutput: String,
ft: FormatToken,
) extends Error({
val tok = LoggerOps.log2(ft)
val line = ft.left.pos.endLine
val frag = "#search-state-exploded"
s"Search state exploded on '$tok', line $line [see $cfgUrl$frag]"
}) {
def line: Int = ft.left.pos.endLine
tok: String,
line: Int,
) extends Error(
s"Search state exploded on '$tok', line $line [see $cfgUrl#search-state-exploded]",
) {
def this(deepestState: State, ft: FormatToken)(implicit
formatWriter: FormatWriter,
) = this(
deepestState,
formatWriter.mkString(deepestState),
LoggerOps.log2(ft),
ft.left.pos.endLine + 1,
)
def this(
deepestState: State,
)(implicit tokens: FormatTokens, formatWriter: FormatWriter) =
this(deepestState, tokens(deepestState.depth))
}

case class InvalidScalafmtConfiguration(throwable: Throwable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ object Scalafmt {
case ed => Failure(ed)
},
tree => {
val formatOps = new FormatOps(tree, style, file)
implicit val formatOps = new FormatOps(tree, style, file)
runner.event(CreateFormatOps(formatOps))
val formatWriter = new FormatWriter(formatOps)
Try(BestFirstSearch(formatOps, range, formatWriter)).flatMap { res =>
implicit val formatWriter = new FormatWriter(formatOps)
Try(BestFirstSearch(range)).flatMap { res =>
val formattedString = formatWriter.mkString(res.state)
if (res.reachedEOF) Success(formattedString)
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import scala.collection.mutable

/** Implements best first search to find optimal formatting.
*/
private class BestFirstSearch private (
range: Set[Range],
private class BestFirstSearch private (range: Set[Range])(implicit
val formatOps: FormatOps,
formatWriter: FormatWriter,
)(implicit val formatOps: FormatOps) {
) {
import BestFirstSearch._
import LoggerOps._
import TreeOps._
Expand Down Expand Up @@ -124,11 +124,8 @@ private class BestFirstSearch private (
if (noOptZone || shouldEnterState(curr)) {
trackState(curr, depth, Q.length)

if (explored > style.runner.maxStateVisits) throw SearchStateExploded(
deepestYet,
formatWriter.mkString(deepestYet),
tokens(deepestYet.depth),
)
if (explored > style.runner.maxStateVisits)
throw new SearchStateExploded(deepestYet)

if (curr.split != null && curr.split.isNL)
if (
Expand All @@ -149,11 +146,7 @@ private class BestFirstSearch private (
visits(curr.depth) > maxVisitsPerToken
) {
complete(deepestYet)
throw SearchStateExploded(
deepestYet,
formatWriter.mkString(deepestYet),
splitToken,
)
throw new SearchStateExploded(deepestYet, splitToken)
} else {
val actualSplit = getActiveSplits(curr, maxCost)
val allAltAreNL = actualSplit.forall(_.isNL)
Expand Down Expand Up @@ -301,11 +294,9 @@ case class SearchResult(state: State, reachedEOF: Boolean)
object BestFirstSearch {

def apply(
formatOps: FormatOps,
range: Set[Range],
formatWriter: FormatWriter,
): SearchResult =
new BestFirstSearch(range, formatWriter)(formatOps).getBestPath
)(implicit formatOps: FormatOps, formatWriter: FormatWriter): SearchResult =
new BestFirstSearch(range).getBestPath

private def getNoOptZones(tokens: FormatTokens) = {
val result = Set.newBuilder[Token]
Expand Down

0 comments on commit 45dc931

Please sign in to comment.