From d4066d941ad91a7b7babf34418cdcf95fd5022bc Mon Sep 17 00:00:00 2001 From: rochala Date: Sat, 14 Sep 2024 17:45:36 +0200 Subject: [PATCH] Autoimports should now correctly be inserted for licenses and directive just before first object --- .../src/main/dotty/tools/pc/AutoImports.scala | 9 ++-- .../pc/tests/edit/AutoImportsSuite.scala | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) 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 + )