Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autoimports with using directives #21590

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Loading