Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix scala-cli dep completions for scala-cli 1.0.1 #5335

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -46,6 +46,26 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
"utest/framework/Tree.scala",
)

completion <- server.completion(
"MyTests.scala",
"//> using lib \"com.lihao@@yi::utest",
jkciesluk marked this conversation as resolved.
Show resolved Hide resolved
)

_ = 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 @@ -55,7 +75,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 @@ -121,13 +141,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 @@ -189,7 +229,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
Loading