diff --git a/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala b/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala index 2ea47bd72c..49244fe82d 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala @@ -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 @@ -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) } } }