From 1870e8987b99860a49209f9a66ec51406a1ae880 Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Mon, 20 Nov 2023 13:51:55 +0100 Subject: [PATCH] bugfix: Adjust auto import position for shebang --- .../pc/ScriptFirstImportPosition.scala | 23 +++++++++----- .../tests/scalacli/ScalaCliActionsSuite.scala | 30 +++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/mtags/src/main/scala/scala/meta/internal/pc/ScriptFirstImportPosition.scala b/mtags/src/main/scala/scala/meta/internal/pc/ScriptFirstImportPosition.scala index 3eff79e84ea..10cef33ed76 100644 --- a/mtags/src/main/scala/scala/meta/internal/pc/ScriptFirstImportPosition.scala +++ b/mtags/src/main/scala/scala/meta/internal/pc/ScriptFirstImportPosition.scala @@ -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, "/**/").map { startOffset => val offset = skipPrefixesOffset(ammHeaders, it, None) @@ -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, "/*