Skip to content

Commit 40ae7ee

Browse files
committed
Allow import <ident> to show completions
Previously, there would be no completions after simple `import annot`. Now, we make sure that the completions mode is set correctly for that case. Additionally, whenever both `java.lang` and `scala` imports conflict we choose scala one.
1 parent fc7e8c3 commit 40ae7ee

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ object Completion {
6161
*/
6262
def completionMode(path: List[Tree], pos: SourcePosition): Mode =
6363
path match {
64+
case Ident(_) :: Import(_, _) :: _ =>
65+
Mode.Import
6466
case (ref: RefTree) :: _ =>
6567
if (ref.name.isTermName) Mode.Term
6668
else if (ref.name.isTypeName) Mode.Type
@@ -211,13 +213,35 @@ object Completion {
211213
// import a.C
212214
def isSameSymbolImportedDouble = denotss.forall(_.denots == first.denots)
213215

216+
def isScalaPackage(scopedDenots: ScopedDenotations) =
217+
scopedDenots.denots.exists(_.info.typeSymbol.owner == defn.ScalaPackageClass)
218+
219+
def isJavaLangPackage(scopedDenots: ScopedDenotations) =
220+
scopedDenots.denots.exists(_.info.typeSymbol.owner == defn.JavaLangPackageClass)
221+
222+
// For example
223+
// import java.lang.annotation
224+
// is shadowed by
225+
// import scala.annotation
226+
def isJavaLangAndScala = denotss match
227+
case List(first, second) =>
228+
isScalaPackage(first) && isJavaLangPackage(second) ||
229+
isScalaPackage(second) && isJavaLangPackage(first)
230+
case _ => false
231+
214232
denotss.find(!_.ctx.isImportContext) match {
215233
// most deeply nested member or local definition if not shadowed by an import
216234
case Some(local) if local.ctx.scope == first.ctx.scope =>
217235
resultMappings += name -> local.denots
218236

219237
case None if isSingleImport || isImportedInDifferentScope || isSameSymbolImportedDouble =>
220238
resultMappings += name -> first.denots
239+
case None if isJavaLangAndScala =>
240+
denotss.foreach{
241+
denots =>
242+
if isScalaPackage(denots) then
243+
resultMappings += name -> denots.denots
244+
}
221245

222246
case _ =>
223247
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,4 +976,13 @@ 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+
)
979988
}

0 commit comments

Comments
 (0)