diff --git a/presentation-compiler/src/main/dotty/tools/pc/AutoImports.scala b/presentation-compiler/src/main/dotty/tools/pc/AutoImports.scala index 896954c4e1a4..1b44dce8c642 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/AutoImports.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/AutoImports.scala @@ -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) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImportsSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImportsSuite.scala index e4ef8c0f747d..3bb5bfea7bc0 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImportsSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImportsSuite.scala @@ -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: + | <> + |""".stripMargin, + """|//> using scala 3.5.0 + |import java.nio.file.Files + |object Main: + | Files + |""".stripMargin + ) + + @Test def scalaCliNoEmptyLineAfterLicense = + checkEdit( + """|/** + | * Some license text + | */ + | + |object Main: + | <> + |""".stripMargin, + """|/** + | * Some license text + | */ + |import java.nio.file.Files + | + |object Main: + | Files + |""".stripMargin + ) + + @Test def scalaCliNoEmptyLineAfterLicenseWithPackage = + checkEdit( + """|/** + | * Some license text + | */ + |package test + | + |object Main: + | <> + |""".stripMargin, + """|/** + | * Some license text + | */ + |package test + | + |import java.nio.file.Files + | + |object Main: + | Files + |""".stripMargin + )