Skip to content

Commit

Permalink
Fix autoimports with using directives (#21590)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik authored Sep 15, 2024
2 parents 83efd23 + d4066d9 commit ad8c21a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,14 @@ object AutoImports:
case _ => None


def skipUsingDirectivesOffset(
firstObjectPos: Int = firstMemberDefinitionStart(tree).getOrElse(0)
): Int =
def skipUsingDirectivesOffset(firstObjectPos: Int = firstMemberDefinitionStart(tree).getOrElse(0)): Int =
val firstObjectLine = pos.source.offsetToLine(firstObjectPos)

comments
.takeWhile(comment =>
!comment.isDocComment && pos.source.offsetToLine(comment.span.end) + 1 < firstObjectLine
val commentLine = pos.source.offsetToLine(comment.span.end)
val isFirstObjectComment = commentLine + 1 == firstObjectLine && !comment.raw.startsWith("//>")
commentLine < firstObjectLine && !isFirstObjectComment
)
.lastOption
.fold(0)(_.span.end + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,57 @@ class AutoImportsSuite extends BaseAutoImportsSuite:
|object Main{ val obj = ABC }
|""".stripMargin
)

@Test def scalaCliNoEmptyLineAfterDirective =
checkEdit(
"""|//> using scala 3.5.0
|object Main:
| <<Files>>
|""".stripMargin,
"""|//> using scala 3.5.0
|import java.nio.file.Files
|object Main:
| Files
|""".stripMargin
)

@Test def scalaCliNoEmptyLineAfterLicense =
checkEdit(
"""|/**
| * Some license text
| */
|
|object Main:
| <<Files>>
|""".stripMargin,
"""|/**
| * Some license text
| */
|import java.nio.file.Files
|
|object Main:
| Files
|""".stripMargin
)

@Test def scalaCliNoEmptyLineAfterLicenseWithPackage =
checkEdit(
"""|/**
| * Some license text
| */
|package test
|
|object Main:
| <<Files>>
|""".stripMargin,
"""|/**
| * Some license text
| */
|package test
|
|import java.nio.file.Files
|
|object Main:
| Files
|""".stripMargin
)

0 comments on commit ad8c21a

Please sign in to comment.