Skip to content

Commit

Permalink
fix: use already imported package aliases for auto import
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek authored and tgodzik committed Jul 10, 2024
1 parent 905fe0e commit 1a00bda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion mtags/src/main/scala-3/scala/meta/internal/pc/AutoImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,15 @@ object AutoImports:
private def importName(sym: Symbol): String =
if indexedContext.importContext.toplevelClashes(sym) then
s"_root_.${sym.fullNameBackticked(false)}"
else sym.fullNameBackticked(false)
else {
sym.ownersIterator.zipWithIndex.foldLeft((List.empty[String], false)) { case ((acc, isDone), (sym, idx)) =>
if(isDone || sym.isEmptyPackage || sym.isRoot) (acc, true)
else indexedContext.rename(sym) match
case Some(renamed) => (renamed :: acc, true)
case None if !sym.isPackageObject => (sym.nameBackticked(false) :: acc, false)
case None => (acc, false)
}._1.mkString(".")
}
end AutoImportsGenerator

private def autoImportPosition(
Expand Down
13 changes: 13 additions & 0 deletions tests/cross/src/test/scala/tests/pc/AutoImportsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,19 @@ class AutoImportsSuite extends BaseAutoImportsSuite {
|""".stripMargin
)

checkEdit(
"use-packages-in-scope".tag(IgnoreScala2),
"""|import scala.collection.mutable as mut
|
|val l = <<ListBuffer>>(2)
|""".stripMargin,
"""|import scala.collection.mutable as mut
|import mut.ListBuffer
|
|val l = ListBuffer(2)
|""".stripMargin
)

private def ammoniteWrapper(code: String): String =
// Vaguely looks like a scala file that Ammonite generates
// from a sc file.
Expand Down

0 comments on commit 1a00bda

Please sign in to comment.