Skip to content

Commit 19cf871

Browse files
authored
Merge pull request #9805 from prolativ/fix-standard-library-symbols-completion
Fix standard library symbols completion (solves #9760)
2 parents c40ef6e + 138c742 commit 19cf871

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,12 @@ object Completion {
263263
*/
264264
private def accessibleMembers(site: Type)(using Context): Seq[Symbol] = site match {
265265
case site: NamedType if site.symbol.is(Package) =>
266-
// Don't look inside package members -- it's too expensive.
267-
site.decls.toList.filter(sym => sym.isAccessibleFrom(site, superAccess = false))
266+
extension (tpe: Type)
267+
def accessibleSymbols = tpe.decls.toList.filter(sym => sym.isAccessibleFrom(site, superAccess = false))
268+
269+
val packageDecls = site.accessibleSymbols
270+
val packageObjectsDecls = packageDecls.filter(_.isPackageObject).flatMap(_.thisType.accessibleSymbols)
271+
packageDecls ++ packageObjectsDecls
268272
case _ =>
269273
def appendMemberSyms(name: Name, buf: mutable.Buffer[SingleDenotation]): Unit =
270274
try buf ++= site.member(name).alternatives

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ object Interactive {
277277

278278
def contextOfPath(path: List[Tree])(using Context): Context = path match {
279279
case Nil | _ :: Nil =>
280-
ctx.run.runContext.fresh.setCompilationUnit(ctx.compilationUnit)
280+
ctx.fresh
281281
case nested :: encl :: rest =>
282282
val outer = contextOfPath(encl :: rest)
283283
try encl match {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
143143
def run(uri: URI, sourceCode: String): List[Diagnostic] = run(uri, toSource(uri, sourceCode))
144144

145145
def run(uri: URI, source: SourceFile): List[Diagnostic] = {
146+
import typer.ImportInfo._
147+
146148
val previousCtx = myCtx
147149
try {
148150
val reporter =
149151
new StoreReporter(null) with UniqueMessagePositions with HideNonSensicalMessages
150152

151153
val run = compiler.newRun(using myInitCtx.fresh.setReporter(reporter))
152-
myCtx = run.runContext
154+
myCtx = run.runContext.withRootImports
153155

154156
given Context = myCtx
155157

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,44 @@ import dotty.tools.languageserver.util.actions.CodeCompletion
1010
class CompletionTest {
1111

1212
@Test def completion0: Unit = {
13-
code"class Foo { val xyz: Int = 0; def y: Int = xy$m1 }".withSource
13+
code"class Foo { val xyz: Int = 0; def y: Int = xy${m1} }".withSource
1414
.completion(m1, Set(("xyz", Field, "Int")))
1515
}
1616

17+
@Test def completionFromScalaPredef: Unit = {
18+
code"class Foo { def foo: Unit = prin${m1} }".withSource
19+
.completion(m1, Set(
20+
("print", Method, "(x: Any): Unit"),
21+
("printf", Method, "(text: String, xs: Any*): Unit"),
22+
("println", Method, "(x: Any): Unit")
23+
))
24+
}
25+
26+
@Test def completionFromDottyPredef: Unit = {
27+
code"class Foo { val foo = summ${m1} }".withSource
28+
.completion(m1, Set(("summon", Method, "[T](using x: T): x.type")))
29+
}
30+
31+
@Test def completionFromScalaPackage: Unit = {
32+
code"class Foo { val foo: Conv${m1} }".withSource
33+
.completion(m1, Set(("Conversion", Class, "scala.Conversion")))
34+
}
35+
36+
@Test def completionFromScalaPackageObject: Unit = {
37+
code"class Foo { val foo: BigD${m1} }".withSource
38+
.completion(m1, Set(("BigDecimal", Field, "type and getter BigDecimal")))
39+
}
40+
41+
@Test def completionFromSyntheticPackageObject: Unit = {
42+
code"class Foo { val foo: IArr${m1} }".withSource
43+
.completion(m1, Set(("IArray", Field, "type and object IArray")))
44+
}
45+
46+
@Test def completionFromJavaDefaults: Unit = {
47+
code"class Foo { val foo: Runn${m1} }".withSource
48+
.completion(m1, Set(("Runnable", Class, "trait and object Runnable")))
49+
}
50+
1751
@Test def completionWithImplicitConversion: Unit = {
1852
withSources(
1953
code"object Foo { implicit class WithBaz(bar: Bar) { def baz = 0 } }",
@@ -29,7 +63,7 @@ class CompletionTest {
2963
).completion(m1, Set(("MyClass", Class, "Foo.MyClass")))
3064
}
3165

32-
@Test def ImportCompleteClassNoPrefix: Unit = {
66+
@Test def importCompleteClassNoPrefix: Unit = {
3367
withSources(
3468
code"""object Foo { class MyClass }""",
3569
code"""import Foo.${m1}"""

0 commit comments

Comments
 (0)