Skip to content

Commit

Permalink
fix: Fix scala-cli dep completions for scala-cli 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk authored and tgodzik committed Jul 11, 2023
1 parent 55113be commit 024d760
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ import scala.meta.internal.pc.MetalsGlobal
trait ScalaCliCompletions {
this: MetalsGlobal =>
class ScalaCliExtractor(pos: Position) {
def unapply(path: List[Tree]): Option[String] =
def unapply(path: List[Tree]): Option[String] = {
def scalaCliDep = CoursierComplete.isScalaCliDep(
pos.lineContent
.take(pos.column - 1)
.stripPrefix("/*<script>*/")
)

path match {
case Nil =>
CoursierComplete.isScalaCliDep(
pos.lineContent.replace(CURSOR, "").take(pos.column - 1)
)
// generated script file will end with .sc.scala
case (_: Template) :: (_: ModuleDef) :: _
case (_: Template) :: (_: ModuleDef) :: (_: PackageDef) :: Nil
if pos.source.file.path.endsWith(".sc.scala") =>
CoursierComplete.isScalaCliDep(
pos.lineContent
.stripPrefix("/*<script>*/")
.replace(CURSOR, "")
.take(pos.column - 1)
)
scalaCliDep
case (_: Template) :: (_: ClassDef) :: (_: PackageDef) :: Nil
if pos.source.file.path.endsWith(".sc.scala") =>
scalaCliDep
case _ => None
}
}
}

case class ScalaCliCompletion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class ScalaCliCompletions(
// generated script file will end with .sc.scala
case (_: TypeDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") =>
scalaCliDep
case (_: Template) :: (_: TypeDef) :: Nil
if pos.source.file.path.endsWith(".sc.scala") =>
scalaCliDep
case head :: next => None

def contribute(dependency: String) =
Expand Down
46 changes: 43 additions & 3 deletions tests/slow/src/test/scala/tests/scalacli/ScalaCliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
"utest/framework/Tree.scala",
)

completion <- server.completion(
"MyTests.scala",
"//> using lib \"com.lihao@@yi::utest",
)

_ = assertNoDiff(completion, "com.lihaoyi")

completion <- server.completion(
"MyTests.scala",
"//> using lib com.lihaoyi::pprin@@t",
)

_ = assertNoDiff(
completion,
"""|pprint
|pprint_native0.4
|pprint_sjs1
|""".stripMargin,
)

} yield ()

private def simpleScriptTest(useBsp: Boolean): Future[Unit] =
Expand All @@ -56,7 +76,7 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
|#!/usr/bin/env -S scala-cli shebang --java-opt -Xms256m --java-opt -XX:MaxRAMPercentage=80
|//> using scala "$scalaVersion"
|//> using lib "com.lihaoyi::utest::0.7.10"
|//> using lib "com.lihaoyi::pprint::0.6.6"
|//> using lib com.lihaoyi::pprint::0.6.6
|
|import foo.Foo
|import utest._
Expand Down Expand Up @@ -122,13 +142,33 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
s"Expected no scalameta errors, got: $parserDiagnostics",
)

completion <- server.completion(
"MyTests.sc",
"//> using lib \"com.lihao@@yi::utest",
)

_ = assertNoDiff(completion, "com.lihaoyi")

completion <- server.completion(
"MyTests.sc",
"//> using lib com.lihaoyi::pprin@@t",
)

_ = assertNoDiff(
completion,
"""|pprint
|pprint_native0.4
|pprint_sjs1
|""".stripMargin,
)

} yield ()

private val simpleFileLayout =
s"""|/MyTests.scala
|//> using scala "$scalaVersion"
|//> using lib "com.lihaoyi::utest::0.7.10"
|//> using lib "com.lihaoyi::pprint::0.6.6"
|//> using lib com.lihaoyi::pprint::0.6.6
|
|import foo.Foo
|import utest._
Expand Down Expand Up @@ -206,7 +246,7 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
s"""/scripts/MyTests.scala
|//> using scala "$scalaVersion"
|//> using lib "com.lihaoyi::utest::0.7.10"
|//> using lib "com.lihaoyi::pprint::0.6.6"
|//> using lib com.lihaoyi::pprint::0.6.6
|
|import foo.Foo
|import utest._
Expand Down

0 comments on commit 024d760

Please sign in to comment.