Skip to content

Commit

Permalink
Backport "Fix autoimports with using directives" to LTS (#22131)
Browse files Browse the repository at this point in the history
Backports #21590 to the 3.3.5.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Dec 4, 2024
2 parents 72bbc64 + 3c05414 commit 8a459ab
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 8a459ab

Please sign in to comment.