Skip to content

Commit

Permalink
fix: skip using directives for auto import position when missing newline
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jan 16, 2025
1 parent aa3bebf commit 59750ce
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scala.meta.internal.pc

import scala.annotation.tailrec
import scala.util.matching.Regex

import scala.meta._
import scala.meta.internal.mtags.MtagsEnrichments._
Expand Down Expand Up @@ -85,55 +86,89 @@ object ScriptFirstImportPosition {
beforeComment: Int,
lastOffset: Int,
newLines: Int,
foundShebang: Boolean
foundShebang: Boolean,
foundCommentThatIsNotUsingDirective: Boolean = false
): Int = {
if (it.hasNext) {
it.next match {
val n = it.next()
n match {
case t: Token.Comment if t.value.startsWith(shebang) =>
skipComments(
it,
beforeComment,
t.pos.end,
newLines = 0,
foundShebang = true
foundShebang = true,
foundCommentThatIsNotUsingDirective
)
case t: Token.Comment if newLines > 1 =>
skipComments(
it,
lastOffset,
t.pos.end,
newLines = 0,
foundShebang
foundShebang,
foundCommentThatIsNotUsingDirective =
foundCommentThatIsNotUsingDirective || !isScalaCliUsingDirectiveComment(
t
)
)
case t: Token.Comment =>
skipComments(
it,
beforeComment,
t.pos.end,
newLines = 0,
foundShebang
foundShebang,
foundCommentThatIsNotUsingDirective =
foundCommentThatIsNotUsingDirective || !isScalaCliUsingDirectiveComment(
t
)
)
case t: Token.AtEOL =>
skipComments(
it,
beforeComment,
lastOffset,
newLines + t.newlines,
foundShebang
foundShebang,
foundCommentThatIsNotUsingDirective
)
case _: Token.Whitespace =>
skipComments(it, beforeComment, lastOffset, newLines, foundShebang)
skipComments(
it,
beforeComment,
lastOffset,
newLines,
foundShebang,
foundCommentThatIsNotUsingDirective
)
case _: Token.BOF =>
skipComments(it, beforeComment, lastOffset, newLines, foundShebang)
skipComments(
it,
beforeComment,
lastOffset,
newLines,
foundShebang,
foundCommentThatIsNotUsingDirective
)
case _ =>
// There is an empty line between the comment and the code, so its not a doc
val maybeOffset =
if (newLines > 1) lastOffset
if (newLines > 1 || !foundCommentThatIsNotUsingDirective) lastOffset
else beforeComment
if (foundShebang) maybeOffset - 2
else maybeOffset
}
} else lastOffset
}

val scalaCliUsingDirectiveRegex: Regex = """//>\s*using.*"""".r

private def isScalaCliUsingDirectiveComment(t: Token.Comment): Boolean =
t.text match {
case scalaCliUsingDirectiveRegex() => true
case _ => false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ class ScalaCliActionsSuite
fileName = "A.sc",
)

checkScalaCLI(
"i7071",
s"""|//> using scala "${BuildInfo.scala213}"
|object X {
| <<FiniteDuration>>
|}
|""".stripMargin,
s"""|${ImportMissingSymbol.title("FiniteDuration", "scala.concurrent.duration")}
|${CreateNewSymbol.title("FiniteDuration")}
|""".stripMargin,
s"""|//> using scala "${BuildInfo.scala213}"
|import scala.concurrent.duration.FiniteDuration
|object X {
| FiniteDuration
|}
|""".stripMargin,
scalaCliOptions = List("--actions", "-S", scalaVersion),
expectNoDiagnostics = false,
fileName = "A.scala",
)

checkScalaCLI(
"script-organize-imports",
s"""|//> using scala "${BuildInfo.scala213}"
Expand Down

0 comments on commit 59750ce

Please sign in to comment.