Skip to content

Commit 0367d6b

Browse files
authored
Merge pull request #14069 from tgodzik/fix-i13623
Allow `import <ident>` to show completions
2 parents f56760a + ed1f399 commit 0367d6b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import dotty.tools.dotc.printing.Texts._
2424
import dotty.tools.dotc.util.{NameTransformer, NoSourcePosition, SourcePosition}
2525

2626
import 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
}

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,4 +976,22 @@ class CompletionTest {
976976
("main", Module, "main")
977977
)
978978
)
979+
980+
@Test def i13623_annotation : Unit =
981+
code"""import annot${m1}"""
982+
.withSource
983+
.completion(m1,
984+
Set(
985+
("annotation", Module, "scala.annotation")
986+
)
987+
)
988+
989+
@Test def importAnnotationAfterImport : Unit =
990+
code"""import java.lang.annotation; import annot${m1}"""
991+
.withSource
992+
.completion(m1,
993+
Set(
994+
("annotation", Module, "scala.annotation")
995+
)
996+
)
979997
}

0 commit comments

Comments
 (0)