Skip to content

Commit ad8c21a

Browse files
authored
Fix autoimports with using directives (#21590)
2 parents 83efd23 + d4066d9 commit ad8c21a

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

presentation-compiler/src/main/dotty/tools/pc/AutoImports.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,14 @@ object AutoImports:
320320
case _ => None
321321

322322

323-
def skipUsingDirectivesOffset(
324-
firstObjectPos: Int = firstMemberDefinitionStart(tree).getOrElse(0)
325-
): Int =
323+
def skipUsingDirectivesOffset(firstObjectPos: Int = firstMemberDefinitionStart(tree).getOrElse(0)): Int =
326324
val firstObjectLine = pos.source.offsetToLine(firstObjectPos)
325+
327326
comments
328327
.takeWhile(comment =>
329-
!comment.isDocComment && pos.source.offsetToLine(comment.span.end) + 1 < firstObjectLine
328+
val commentLine = pos.source.offsetToLine(comment.span.end)
329+
val isFirstObjectComment = commentLine + 1 == firstObjectLine && !comment.raw.startsWith("//>")
330+
commentLine < firstObjectLine && !isFirstObjectComment
330331
)
331332
.lastOption
332333
.fold(0)(_.span.end + 1)

presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImportsSuite.scala

+54
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,57 @@ class AutoImportsSuite extends BaseAutoImportsSuite:
500500
|object Main{ val obj = ABC }
501501
|""".stripMargin
502502
)
503+
504+
@Test def scalaCliNoEmptyLineAfterDirective =
505+
checkEdit(
506+
"""|//> using scala 3.5.0
507+
|object Main:
508+
| <<Files>>
509+
|""".stripMargin,
510+
"""|//> using scala 3.5.0
511+
|import java.nio.file.Files
512+
|object Main:
513+
| Files
514+
|""".stripMargin
515+
)
516+
517+
@Test def scalaCliNoEmptyLineAfterLicense =
518+
checkEdit(
519+
"""|/**
520+
| * Some license text
521+
| */
522+
|
523+
|object Main:
524+
| <<Files>>
525+
|""".stripMargin,
526+
"""|/**
527+
| * Some license text
528+
| */
529+
|import java.nio.file.Files
530+
|
531+
|object Main:
532+
| Files
533+
|""".stripMargin
534+
)
535+
536+
@Test def scalaCliNoEmptyLineAfterLicenseWithPackage =
537+
checkEdit(
538+
"""|/**
539+
| * Some license text
540+
| */
541+
|package test
542+
|
543+
|object Main:
544+
| <<Files>>
545+
|""".stripMargin,
546+
"""|/**
547+
| * Some license text
548+
| */
549+
|package test
550+
|
551+
|import java.nio.file.Files
552+
|
553+
|object Main:
554+
| Files
555+
|""".stripMargin
556+
)

0 commit comments

Comments
 (0)