Skip to content

Commit

Permalink
Autoimports should now correctly be inserted for licenses and directi…
Browse files Browse the repository at this point in the history
…ve just before first object

[Cherry-picked d4066d9]
  • Loading branch information
rochala authored and WojciechMazur committed Dec 3, 2024
1 parent 5032a49 commit a8f2409
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 a8f2409

Please sign in to comment.