Skip to content

Commit

Permalink
bugfix: Auto imports in worksheets in Scala 3
Browse files Browse the repository at this point in the history
Previously the import would be inserted above using directives
  • Loading branch information
jkciesluk committed Jan 25, 2024
1 parent a6d28d7 commit 1da8ec3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ object AutoImports:
text: String,
tree: Tree,
)(using Context): AutoImportPosition =
def isScala3Worksheet = pos.source.path.isWorksheet

@tailrec
def lastPackageDef(
Expand Down Expand Up @@ -321,7 +322,10 @@ object AutoImports:
case Some(stm) => (stm.endPos.line + 1, false)
case None if pkg.pid.symbol.isEmptyPackage =>
val offset =
ScriptFirstImportPosition.skipUsingDirectivesOffset(text)
ScriptFirstImportPosition.skipUsingDirectivesOffset(
text,
isScala3Worksheet,
)
(pos.source.offsetToLine(offset), false)
case None =>
val pos = pkg.pid.endPos
Expand Down Expand Up @@ -362,7 +366,8 @@ object AutoImports:

def fileStart =
AutoImportPosition(
ScriptFirstImportPosition.skipUsingDirectivesOffset(text),
ScriptFirstImportPosition
.skipUsingDirectivesOffset(text, isScala3Worksheet),
0,
padTop = false,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,21 @@ object ScriptFirstImportPosition {
}
}

def skipUsingDirectivesOffset(text: String): Int =
skipPrefixesOffset(usingDirectives, adjustShebang(text))
def skipUsingDirectivesOffset(
text: String,
isScala3Worksheet: Boolean = false
): Int =
if (isScala3Worksheet) {
val iterator = tokenize(adjustShebang(text)).iterator
skipScala3WorksheetObject(iterator)
.map { startOffset =>
val offset =
skipPrefixesOffset(usingDirectives, iterator, None)
.getOrElse(startOffset)
offset + 1
}
.getOrElse(0)
} else skipPrefixesOffset(usingDirectives, adjustShebang(text))

def skipPrefixesOffset(prefixes: List[String], text: String): Int = {
val it = tokenize(text).iterator
Expand Down Expand Up @@ -86,6 +99,19 @@ object ScriptFirstImportPosition {
} else None
}

@tailrec
private def skipScala3WorksheetObject(
it: Iterator[Token]
): Option[Int] = {
if (it.hasNext) {
it.next() match {
// We skip everything until the brace in `object worksheet {`
case t: Token.LeftBrace => Some(t.pos.end)
case _ => skipScala3WorksheetObject(it)
}
} else None
}

@tailrec
private def skipPrefixesOffset(
prefixes: List[String],
Expand Down
24 changes: 15 additions & 9 deletions tests/unit/src/main/scala/tests/BaseWorksheetLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,8 @@ abstract class BaseWorksheetLspSuite(
|/metals.json
|{"a": {"scalaVersion": "${scalaVersion}"}}
|/$path
|//> using scala $scalaVersion
|
|object A {
| val f = Future.successful(42)
|}
Expand All @@ -881,10 +883,12 @@ abstract class BaseWorksheetLspSuite(
server
.assertCodeAction(
path,
"""|object A {
| val f = <<Future>>.successful(42)
|}
|""".stripMargin,
s"""|//> using scala $scalaVersion
|
|object A {
| val f = <<Future>>.successful(42)
|}
|""".stripMargin,
expectedActions,
Nil,
)
Expand All @@ -895,11 +899,13 @@ abstract class BaseWorksheetLspSuite(
// Assert if indentation is correct. See `AutoImports.renderImport`
_ = assertNoDiff(
server.bufferContents(path),
"""|import scala.concurrent.Future
|object A {
| val f = Future.successful(42)
|}
|""".stripMargin,
s"""|//> using scala $scalaVersion
|import scala.concurrent.Future
|
|object A {
| val f = Future.successful(42)
|}
|""".stripMargin,
)
} yield ()
}
Expand Down

0 comments on commit 1da8ec3

Please sign in to comment.