Skip to content

Commit

Permalink
Merge pull request #3154 from tgodzik/fix-deduplicate2
Browse files Browse the repository at this point in the history
bugfix: Also deduplicate if options split by space
  • Loading branch information
tgodzik authored Sep 6, 2024
2 parents 932866d + 2663a98 commit e790867
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
}

test("cli scalac options shadowing using directives") {
val cliScalacOptions = Seq("-Xmaxwarns", "4", "-g:source", "-language:no2AutoTupling")
val cliScalacOptions =
Seq("-Xmaxwarns", "4", "-g:source", "-language:no2AutoTupling", "-language", "no2AutoTupling")
val usingDirectiveScalacOptions = Seq(
"-nobootcp",
"-Xmaxwarns",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object Positioned {
underlying: ShadowingSeq.KeyOf[T]
): ShadowingSeq.KeyOf[Positioned[T]] =
ShadowingSeq.KeyOf(
p => underlying.get(p.value),
pSeq => underlying.makeKey(pSeq.map(_.value)),
seq => underlying.groups(seq.map(_.value))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final case class JavaOpt(value: String) {
else if (value.startsWith("@")) Some("@")
else None
}

}

object JavaOpt {
Expand All @@ -29,7 +30,7 @@ object JavaOpt {
}
implicit val keyOf: ShadowingSeq.KeyOf[JavaOpt] =
ShadowingSeq.KeyOf(
_.key,
opts => opts.headOption.flatMap(_.key).orElse(Some(opts.map(_.value).mkString(":"))),
seq => ScalacOpt.groupCliOptions(seq.map(_.value))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ final case class ScalacOpt(value: String) {

/** @return raw key for the option (only if the key can be shadowed from the CLI) */
private[options] def shadowableKey: Option[String] = key match
case Some(key) if ScalacOpt.repeatingKeys.exists(_.startsWith(key)) => Some(value)
case otherwise => otherwise
case Some(key)
if ScalacOpt.repeatingKeys.exists(rKey => rKey.startsWith(key + ":") || rKey == key) => None
case otherwise => otherwise
}

object ScalacOpt {
private val repeatingKeys = Set(
"-Xplugin:",
"-Xplugin",
"-P", // plugin options
"-language:"
"-language"
)

implicit val hashedType: HashedType[ScalacOpt] = {
opt => opt.value
}
implicit val keyOf: ShadowingSeq.KeyOf[ScalacOpt] =
ShadowingSeq.KeyOf(
_.shadowableKey,
opts =>
opts.headOption.flatMap(_.shadowableKey).orElse(Some(opts.map(_.value).mkString(":"))),
seq => groupCliOptions(seq.map(_.value))
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ final case class ShadowingSeq[T] private (values: Seq[Seq[T]]) {
else {
val l = new mutable.ListBuffer[Seq[T]]
val seen = new mutable.HashSet[String]

for (group <- values.iterator ++ other.iterator) {
assert(group.nonEmpty)
val keyOpt = key.get(group.head)
val keyOpt = key.makeKey(group)
if (!keyOpt.exists(seen.contains)) {
l += group
for (key <- keyOpt)
Expand All @@ -58,13 +57,13 @@ final case class ShadowingSeq[T] private (values: Seq[Seq[T]]) {
object ShadowingSeq {

final case class KeyOf[T](
get: T => Option[String],
makeKey: Seq[T] => Option[String],
/** The indices at which sub-groups of values start */
groups: Seq[T] => Seq[Int]
)
object KeyOf {
implicit val keyOfAnyDependency: KeyOf[AnyDependency] =
KeyOf(dep => Some(dep.module.render), _.indices)
KeyOf(deps => deps.headOption.map(_.module.render), _.indices)
}

implicit def monoid[T](implicit key: KeyOf[T]): ConfigMonoid[ShadowingSeq[T]] =
Expand Down

0 comments on commit e790867

Please sign in to comment.