Skip to content

Allow import <ident> to show completions #14069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dotty.tools.dotc.printing.Texts._
import dotty.tools.dotc.util.{NameTransformer, NoSourcePosition, SourcePosition}

import scala.collection.mutable
import scala.util.control.NonFatal

/**
* One of the results of a completion query.
Expand Down Expand Up @@ -61,6 +62,8 @@ object Completion {
*/
def completionMode(path: List[Tree], pos: SourcePosition): Mode =
path match {
case Ident(_) :: Import(_, _) :: _ =>
Mode.Import
case (ref: RefTree) :: _ =>
if (ref.name.isTermName) Mode.Term
else if (ref.name.isTypeName) Mode.Type
Expand Down Expand Up @@ -211,13 +214,35 @@ object Completion {
// import a.C
def isSameSymbolImportedDouble = denotss.forall(_.denots == first.denots)

def isScalaPackage(scopedDenots: ScopedDenotations) =
scopedDenots.denots.exists(_.info.typeSymbol.owner == defn.ScalaPackageClass)

def isJavaLangPackage(scopedDenots: ScopedDenotations) =
scopedDenots.denots.exists(_.info.typeSymbol.owner == defn.JavaLangPackageClass)

// For example
// import java.lang.annotation
// is shadowed by
// import scala.annotation
def isJavaLangAndScala =
try
denotss.forall(denots => isScalaPackage(denots) || isJavaLangPackage(denots))
catch
case NonFatal(_) => false

denotss.find(!_.ctx.isImportContext) match {
// most deeply nested member or local definition if not shadowed by an import
case Some(local) if local.ctx.scope == first.ctx.scope =>
resultMappings += name -> local.denots

case None if isSingleImport || isImportedInDifferentScope || isSameSymbolImportedDouble =>
resultMappings += name -> first.denots
case None if isJavaLangAndScala =>
denotss.foreach{
denots =>
if isScalaPackage(denots) then
resultMappings += name -> denots.denots
}

case _ =>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,4 +976,22 @@ class CompletionTest {
("main", Module, "main")
)
)

@Test def i13623_annotation : Unit =
code"""import annot${m1}"""
.withSource
.completion(m1,
Set(
("annotation", Module, "scala.annotation")
)
)

@Test def importAnnotationAfterImport : Unit =
code"""import java.lang.annotation; import annot${m1}"""
.withSource
.completion(m1,
Set(
("annotation", Module, "scala.annotation")
)
)
}