From 1a00bda3b3c18183f952e854ad3282a274f425c8 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Tue, 9 Jul 2024 18:17:53 +0200 Subject: [PATCH] fix: use already imported package aliases for auto import --- .../scala/meta/internal/pc/AutoImports.scala | 10 +++++++++- .../src/test/scala/tests/pc/AutoImportsSuite.scala | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/AutoImports.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/AutoImports.scala index df40056d22d..11ce6ad687f 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/AutoImports.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/AutoImports.scala @@ -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( diff --git a/tests/cross/src/test/scala/tests/pc/AutoImportsSuite.scala b/tests/cross/src/test/scala/tests/pc/AutoImportsSuite.scala index 9413f6840c4..8451de96796 100644 --- a/tests/cross/src/test/scala/tests/pc/AutoImportsSuite.scala +++ b/tests/cross/src/test/scala/tests/pc/AutoImportsSuite.scala @@ -398,6 +398,19 @@ class AutoImportsSuite extends BaseAutoImportsSuite { |""".stripMargin ) + checkEdit( + "use-packages-in-scope".tag(IgnoreScala2), + """|import scala.collection.mutable as mut + | + |val l = <>(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.