Skip to content

Commit

Permalink
Remove ordering via hardcoded names
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Sep 19, 2023
1 parent 5ed359c commit f1108b9
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.build.errors.{BuildException, CompositeBuildException}
import scala.build.input.*
import scala.build.internal.Constants
import scala.build.options.{BuildOptions, Scope, SuppressWarningOptions}
import scala.build.preprocessing.directives.{DirectivesPreprocessingUtils, StrictDirective}
import scala.build.preprocessing.directives.*
import scala.build.preprocessing.{ExtractedDirectives, SheBang}
import scala.build.{CrossSources, Logger, Position, Sources}
import scala.cli.commands.shared.SharedOptions
Expand Down Expand Up @@ -331,33 +331,40 @@ object Fix extends ScalaCommand[FixOptions] {
}

private val directivesOrdering: Ordering[String] = {
val directivesOrder: Map[String, Int] = HashMap(
"scala" -> 0,
"platforms" -> 1,
"jvm" -> 2,
"native" -> 3,
"js" -> 4,
"options" -> 5,
"java" -> 6,
"mainClass" -> 7,
"files" -> 8,
"objectWrapper" -> 9,
"toolkit" -> 10,
"dependency" -> 11,
"publish" -> 20
)
def directivesOrder(key: String): Int = {
val handlersOrder = Seq(
ScalaVersion.handler.keys,
Platform.handler.keys,
Jvm.handler.keys,
JavaHome.handler.keys,
ScalaNative.handler.keys,
ScalaJs.handler.keys,
ScalacOptions.handler.keys,
JavaOptions.handler.keys,
JavacOptions.handler.keys,
JavaProps.handler.keys,
MainClass.handler.keys,
scala.build.preprocessing.directives.Sources.handler.keys,
ObjectWrapper.handler.keys,
Toolkit.handler.keys,
Dependency.handler.keys
)

handlersOrder.zipWithIndex
.find(_._1.flatMap(_.nameAliases).contains(key))
.map(_._2)
.getOrElse(if key.startsWith("publish") then 20 else 15)
}

Ordering.by { directiveLine =>
val key = directiveLine
.stripPrefix("//> using")
.stripLeading()
.stripPrefix("test.")
// separate key from value
.takeWhile(!_.isWhitespace)
val orderFromMap = directivesOrder.get(key).orElse {
directivesOrder.find((keyPrefix, _) => key.startsWith(keyPrefix)).map(_._2)
}

orderFromMap.getOrElse(15)
directivesOrder(key)
}
}
}

0 comments on commit f1108b9

Please sign in to comment.