Skip to content

Commit

Permalink
bugfix: Dependency completions with double colon before version
Browse files Browse the repository at this point in the history
Double colon before version is allowed only if there is also double colon before artifact
  • Loading branch information
jkciesluk committed Oct 9, 2023
1 parent 4bb3aeb commit 7632899
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,28 @@ class CoursierComplete(scalaVersion: String) {
dependency: String,
supportNonJvm: Boolean
): List[String] = {
val (adjusted, hasDoubleColon) = adjustDoubleColon(dependency)
val adjusted = adjustDoubleColon(dependency)
val sortedCompletions = completions(adjusted).sortWith(
Version.fromString(_) >= Version.fromString(_)
)

def addDoubleColon: Boolean =
supportNonJvm && !hasSpecifiedPlatform(dependency) &&
dependency.count(_ == ':') == 3

// If dependency name doesn't end with `sjs` or `native` and we can default to `::` before version
if (!hasDoubleColon && supportNonJvm && !hasSpecifiedPlatform(dependency))
if (addDoubleColon)
sortedCompletions.map(":" + _)
else sortedCompletions

}

private def adjustDoubleColon(dependency: String): (String, Boolean) = {
private def adjustDoubleColon(dependency: String): String = {
val doubleColon = dependency.lastIndexOf("::")
val firstColon = dependency.indexOf(":")

val firstColon = dependency.indexOf("::")
if (doubleColon > firstColon) {
val depString = (dependency.substring(0, doubleColon) +
dependency.substring(doubleColon + 1, dependency.length()))
(depString, true)
} else (dependency, false)
dependency.take(doubleColon) + dependency.drop(doubleColon + 1)
} else dependency
}

private def hasSpecifiedPlatform(dependency: String): Boolean = {
Expand Down
24 changes: 14 additions & 10 deletions tests/cross/src/test/scala/tests/pc/CompletionMillIvySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class CompletionMillIvySuite extends BaseCompletionSuite {
)

check(
"version-double-colon",
"""|val dependency = ivy"org.typelevel:cats-core_2.11::@@"
"version2",
"""|val dependency = ivy"org.typelevel:cats-core_2.11:@@"
|""".stripMargin,
"""|1.0.1
|1.0.0
Expand All @@ -86,23 +86,27 @@ class CompletionMillIvySuite extends BaseCompletionSuite {

check(
"version-double-colon2",
"""|val dependency = ivy"org.typelevel:cats-core_2.11::1.0.@@"
"""|val dependency = ivy"org.typelevel::cats-core::2.10@@"
|""".stripMargin,
"""|1.0.1
|1.0.0
|1.0.0-RC2
|1.0.0-RC1
|1.0.0-MF
"""|2.10.0
|""".stripMargin,
filename = "build.sc",
)

checkEdit(
"version-double-colon-edit",
"""|val dependency = ivy"org.typelevel:cats-core_2.11::1.0.1@@"
"""|val dependency = ivy"org.typelevel::cats-core::2.10@@"
|""".stripMargin,
"""|val dependency = ivy"org.typelevel:cats-core_2.11::1.0.1"
"""|val dependency = ivy"org.typelevel::cats-core::2.10.0"
|""".stripMargin,
filename = "build.sc",
)

check(
"version-double-colon3",
"""|val dependency = ivy"org.typelevel:cats-core_2.11::1.0.@@"
|""".stripMargin,
"",
filename = "build.sc",
)

Expand Down
19 changes: 19 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionScalaCliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ class CompletionScalaCliSuite extends BaseCompletionSuite {
filter = _.startsWith("3.7.1"),
)

check(
"version-double-colon3",
"""|//> using lib "com.outr:scribe-cats_3::@@"
|package A
|""".stripMargin,
"",
)

checkEdit(
"version-double-colon-edit2",
"""|//> using lib "com.outr:scribe-cats_3:3.7@@"
|package A
|""".stripMargin,
"""|//> using lib "com.outr:scribe-cats_3:3.7.1"
|package A
|""".stripMargin,
filter = _.startsWith("3.7.1"),
)

private def scriptWrapper(code: String, filename: String): String =
// Vaguely looks like a scala file that ScalaCLI generates
// from a sc file.
Expand Down

0 comments on commit 7632899

Please sign in to comment.