forked from scalameta/metals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't break using-directives by an auto-import for
.sc
in scal…
…a-cli (scalameta#4291) Scala-cli wraps `.sc` files in a similar way as ammonite - puts it in object so the resulting code in pc is: ``` object main { /*script*///> using directive //> using directive ... } ```
- Loading branch information
Showing
5 changed files
with
235 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
mtags/src/main/scala/scala/meta/internal/pc/ScriptFirstImportPosition.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package scala.meta.internal.pc | ||
|
||
import scala.annotation.tailrec | ||
|
||
import scala.meta._ | ||
|
||
/** | ||
* Used to determine the position for the first import for scala-cli `.scala` and `.sc` files. | ||
* For scala-cli sources we need to skip `//> using` comments. | ||
* | ||
* For `.sc` Ammonite and Scala-Cli wraps the code for such files. | ||
* The following code: | ||
* ```scala | ||
* val a = 1 | ||
* ``` | ||
* Is trasnformed and passed into PC as: | ||
* ```scala | ||
* ${tool-defauls-imports} | ||
* object ${wrapperObject} { | ||
* /*<${scriptMarker}>*/ | ||
* val a = 1 <-- actual code | ||
* } | ||
* ``` | ||
* To find the proper position we need to find the object that contains `/*<${scriptMarker}>*/` | ||
*/ | ||
object ScriptFirstImportPosition { | ||
|
||
val usingDirectives: List[String] = List("// using", "//> using") | ||
|
||
def ammoniteScStartOffset(text: String): Option[Int] = { | ||
val it = tokenize(text).iterator | ||
startMarkerOffset(it, "/*<start>*/").map(_ + 1) | ||
} | ||
|
||
def scalaCliScStartOffset(text: String): Option[Int] = { | ||
val iterator = tokenize(text).iterator | ||
startMarkerOffset(iterator, "/*<script>*/").map { startOffset => | ||
val offset = | ||
skipUsingDirectivesOffset(iterator, None) | ||
.getOrElse(startOffset) | ||
|
||
offset + 1 | ||
} | ||
} | ||
|
||
def skipUsingDirectivesOffset(text: String): Int = { | ||
val it = tokenize(text).iterator | ||
if (it.hasNext) { | ||
it.next() match { | ||
case _: Token.BOF => | ||
skipUsingDirectivesOffset(it, None) | ||
.map(_ + 1) | ||
.getOrElse(0) | ||
case _ => 0 | ||
} | ||
} else 0 | ||
} | ||
|
||
@tailrec | ||
private def startMarkerOffset( | ||
it: Iterator[Token], | ||
comment: String | ||
): Option[Int] = { | ||
if (it.hasNext) { | ||
it.next() match { | ||
case t: Token.Comment => | ||
if (t.text == comment) Some(t.pos.end) | ||
else startMarkerOffset(it, comment) | ||
case _ => startMarkerOffset(it, comment) | ||
} | ||
} else None | ||
} | ||
|
||
@tailrec | ||
private def skipUsingDirectivesOffset( | ||
it: Iterator[Token], | ||
lastOffset: Option[Int] | ||
): Option[Int] = { | ||
if (it.hasNext) { | ||
it.next match { | ||
case t: Token.Comment | ||
if usingDirectives.exists(prefix => t.text.startsWith(prefix)) => | ||
skipUsingDirectivesOffset(it, Some(t.pos.end)) | ||
case t if isWhitespace(t) => | ||
skipUsingDirectivesOffset(it, lastOffset) | ||
case _ => | ||
lastOffset | ||
} | ||
} else lastOffset | ||
} | ||
|
||
private def tokenize(text: String): Tokens = { | ||
val tokenized = text.tokenize.toOption | ||
tokenized match { | ||
case None => Tokens(Array.empty) | ||
case Some(v) => v | ||
} | ||
} | ||
|
||
private def isWhitespace(t: Token): Boolean = | ||
t.is[Token.Space] || t.is[Token.Tab] || t.is[Token.CR] || | ||
t.is[Token.LF] || t.is[Token.FF] || t.is[Token.LFLF] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.