Skip to content

Commit

Permalink
bugfix: Adjust auto import position for shebang
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk authored and tgodzik committed Dec 2, 2023
1 parent 69f0306 commit 1870e89
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ import scala.meta._
object ScriptFirstImportPosition {

val usingDirectives: List[String] = List("// using", "//> using")
val shebang = "#!"
val ammHeaders: List[String] = List("// scala", "// ammonite")

private def adjustShebang(text: String): String =
text.replaceFirst(shebang, s"//$shebang")

def ammoniteScStartOffset(text: String): Option[Int] = {
val it = tokenize(text).iterator
val it = tokenize(adjustShebang(text)).iterator
startMarkerOffset(it, "/*<start>*/").map { startOffset =>
val offset =
skipPrefixesOffset(ammHeaders, it, None)
Expand All @@ -41,7 +45,7 @@ object ScriptFirstImportPosition {
}

def scalaCliScStartOffset(text: String): Option[Int] = {
val iterator = tokenize(text).iterator
val iterator = tokenize(adjustShebang(text)).iterator
startMarkerOffset(iterator, "/*<script>*/").map { startOffset =>
val offset =
skipPrefixesOffset(usingDirectives, iterator, None)
Expand All @@ -52,7 +56,7 @@ object ScriptFirstImportPosition {
}

def skipUsingDirectivesOffset(text: String): Int =
skipPrefixesOffset(usingDirectives, text)
skipPrefixesOffset(usingDirectives, adjustShebang(text))

def skipPrefixesOffset(prefixes: List[String], text: String): Int = {
val it = tokenize(text).iterator
Expand Down Expand Up @@ -86,17 +90,20 @@ object ScriptFirstImportPosition {
private def skipPrefixesOffset(
prefixes: List[String],
it: Iterator[Token],
lastOffset: Option[Int]
lastOffset: Option[Int],
foundShebang: Boolean = false
): Option[Int] = {
if (it.hasNext) {
it.next match {
case t: Token.Comment
if prefixes.exists(prefix => t.text.startsWith(prefix)) =>
skipPrefixesOffset(prefixes, it, Some(t.pos.end))
skipPrefixesOffset(prefixes, it, Some(t.pos.end), foundShebang)
case t: Token.Comment if t.value.startsWith(shebang) =>
skipPrefixesOffset(prefixes, it, Some(t.pos.end), foundShebang = true)
case t if isWhitespace(t) =>
skipPrefixesOffset(prefixes, it, lastOffset)
case _ =>
lastOffset
skipPrefixesOffset(prefixes, it, lastOffset, foundShebang)
case _ if foundShebang => lastOffset.map(_ - 2)
case _ => lastOffset
}
} else lastOffset
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,34 @@ class ScalaCliActionsSuite
fileName = "A.sc",
)

checkScalaCLI(
"auto-import-shebang",
s"""|#!/usr/bin/env -S scala-cli shebang
|
|//> using scala "${BuildInfo.scala213}"
|//> using lib "org.typelevel::cats-core:2.9.0"
|
|object A {
| <<Future>>.successful(2)
|}
|""".stripMargin,
s"""|${ImportMissingSymbol.title("Future", "scala.concurrent")}
|${ImportMissingSymbol.title("Future", "java.util.concurrent")}
|${CreateNewSymbol.title("Future")}
|""".stripMargin,
s"""|#!/usr/bin/env -S scala-cli shebang
|
|//> using scala "${BuildInfo.scala213}"
|//> using lib "org.typelevel::cats-core:2.9.0"
|import scala.concurrent.Future
|
|object A {
| Future.successful(2)
|}
|""".stripMargin,
scalaCliOptions = List("--actions", "-S", scalaVersion),
expectNoDiagnostics = false,
fileName = "A.sc",
)

}

0 comments on commit 1870e89

Please sign in to comment.