@@ -24,6 +24,7 @@ import dotty.tools.dotc.printing.Texts._
2424import dotty .tools .dotc .util .{NameTransformer , NoSourcePosition , SourcePosition }
2525
2626import scala .collection .mutable
27+ import scala .util .control .NonFatal
2728
2829/**
2930 * One of the results of a completion query.
@@ -61,6 +62,8 @@ object Completion {
6162 */
6263 def completionMode (path : List [Tree ], pos : SourcePosition ): Mode =
6364 path match {
65+ case Ident (_) :: Import (_, _) :: _ =>
66+ Mode .Import
6467 case (ref : RefTree ) :: _ =>
6568 if (ref.name.isTermName) Mode .Term
6669 else if (ref.name.isTypeName) Mode .Type
@@ -211,13 +214,35 @@ object Completion {
211214 // import a.C
212215 def isSameSymbolImportedDouble = denotss.forall(_.denots == first.denots)
213216
217+ def isScalaPackage (scopedDenots : ScopedDenotations ) =
218+ scopedDenots.denots.exists(_.info.typeSymbol.owner == defn.ScalaPackageClass )
219+
220+ def isJavaLangPackage (scopedDenots : ScopedDenotations ) =
221+ scopedDenots.denots.exists(_.info.typeSymbol.owner == defn.JavaLangPackageClass )
222+
223+ // For example
224+ // import java.lang.annotation
225+ // is shadowed by
226+ // import scala.annotation
227+ def isJavaLangAndScala =
228+ try
229+ denotss.forall(denots => isScalaPackage(denots) || isJavaLangPackage(denots))
230+ catch
231+ case NonFatal (_) => false
232+
214233 denotss.find(! _.ctx.isImportContext) match {
215234 // most deeply nested member or local definition if not shadowed by an import
216235 case Some (local) if local.ctx.scope == first.ctx.scope =>
217236 resultMappings += name -> local.denots
218237
219238 case None if isSingleImport || isImportedInDifferentScope || isSameSymbolImportedDouble =>
220239 resultMappings += name -> first.denots
240+ case None if isJavaLangAndScala =>
241+ denotss.foreach{
242+ denots =>
243+ if isScalaPackage(denots) then
244+ resultMappings += name -> denots.denots
245+ }
221246
222247 case _ =>
223248 }
0 commit comments