Skip to content

Commit 3a9aeb3

Browse files
committed
Suggest wilcard import in completions
When in import mode, and there's no prefix or the prefix is `_`, and the qualifier has a stable type, add the wildcard import to the completion suggestion.
1 parent 350dd91 commit 3a9aeb3

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ object Completion {
181181
// Implicit conversions do not kick in when importing
182182
implicitConversionTargets(qual)(ctx.fresh.setExploreTyperState())
183183
.foreach(addAccessibleMembers)
184+
} else if (qual.tpe.isStable && (prefix.isEmpty || prefix == nme.WILDCARD.toString)) {
185+
// In import mode, suggest the wildcard import if there's no prefix or it is `_`.
186+
val wildcard = ctx.newCompletePackageSymbol(qual.symbol, nme.WILDCARD)
187+
completions.enter(wildcard)
184188
}
185189
}
186190

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ class CompletionTest {
3636
).completion(m1, completionItems => {
3737
val results = CodeCompletion.simplifyResults(completionItems)
3838
val myClass = ("MyClass", Class, "Foo.MyClass")
39-
assertTrue(results.contains(("MyClass", Class, "Foo.MyClass")))
39+
val wildcard = ("_", Module, "_")
4040

41-
// Verify that apart from `MyClass`, we only have the methods that exists on `Foo`
42-
assertTrue((results - myClass).forall { case (_, kind, _) => kind == Method })
41+
assertTrue(results.contains(wildcard))
42+
assertTrue(results.contains(myClass))
43+
44+
// Verify that apart from `MyClass` and `wildcard`, we only have the methods that exists on `Foo`
45+
assertTrue((results -- Set(myClass, wildcard)).forall { case (_, kind, _) => kind == Method })
4346

4447
// Verify that we don't have things coming from an implicit conversion, such as ensuring
4548
assertFalse(results.exists { case (name, _, _) => name == "ensuring" })
@@ -175,4 +178,24 @@ class CompletionTest {
175178
|}""".withSource
176179
.completion(m1, results => assertTrue(results.nonEmpty))
177180
}
181+
182+
@Test def noWildcardOutsideImport: Unit = {
183+
code"""class Foo { def bar = 0 }
184+
|object Bar {
185+
| val foo = new Foo
186+
| foo.${m1}
187+
|}""".withSource
188+
.completion(m1, results => {
189+
assertFalse("`_` was a completion result", results.exists(_.getLabel == "_"))
190+
assertTrue(results.forall(_.getKind == Method))
191+
})
192+
}
193+
194+
@Test def importCompletionShowWildcard: Unit = {
195+
code"""import java.${m1}""".withSource
196+
.completion(m1, results => {
197+
assertTrue(results.exists(r => r.getLabel == "_"))
198+
assertTrue(results.forall(_.getKind == Module))
199+
})
200+
}
178201
}

0 commit comments

Comments
 (0)