Skip to content

Commit 5677dfc

Browse files
committed
Show stable terms when completing types
Stable terms can be part of the path to a type, and should be shown in completion when writing a type.
1 parent 9321210 commit 5677dfc

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dotty.tools.dotc.interactive
33
import dotty.tools.dotc.ast.Trees._
44
import dotty.tools.dotc.config.Printers.interactiv
55
import dotty.tools.dotc.core.Contexts.{Context, NoContext}
6+
import dotty.tools.dotc.core.CheckRealizable
67
import dotty.tools.dotc.core.Decorators.StringInterpolators
78
import dotty.tools.dotc.core.Denotations.SingleDenotation
89
import dotty.tools.dotc.core.Flags._
@@ -13,7 +14,7 @@ import dotty.tools.dotc.core.Symbols.{defn, NoSymbol, Symbol}
1314
import dotty.tools.dotc.core.Scopes
1415
import dotty.tools.dotc.core.StdNames.{nme, tpnme}
1516
import dotty.tools.dotc.core.TypeError
16-
import dotty.tools.dotc.core.Types.{NamedType, NameFilter, Type, takeAllFilter}
17+
import dotty.tools.dotc.core.Types.{NamedType, Type, takeAllFilter}
1718
import dotty.tools.dotc.printing.Texts._
1819
import dotty.tools.dotc.util.{NoSourcePosition, SourcePosition}
1920

@@ -219,9 +220,20 @@ object Completion {
219220
!sym.is(allOf(Mutable, Accessor)) &&
220221
(
221222
(mode.is(Mode.Term) && sym.isTerm)
223+
|| (mode.is(Mode.StableTerm) && sym.isTerm && validPathSegment(sym))
222224
|| (mode.is(Mode.Type) && sym.isType)
223225
)
224226

227+
/** Can this symbol be part of a path? See SLS 3.1 for a definition of a valid path. */
228+
private def validPathSegment(sym: Symbol)(implicit ctx: Context): Boolean = {
229+
def isRealizable = {
230+
val realizability = CheckRealizable.realizability(sym.info)
231+
realizability == CheckRealizable.Realizable
232+
}
233+
234+
!sym.is(Method) && isRealizable
235+
}
236+
225237
/**
226238
* Find all the members of `site` that are accessible and which should be included in `info`.
227239
*
@@ -298,11 +310,13 @@ object Completion {
298310
/** Term symbols are allowed */
299311
val Term: Mode = new Mode(1)
300312

313+
val StableTerm: Mode = new Mode(2)
314+
301315
/** Type symbols are allowed */
302-
val Type: Mode = new Mode(2)
316+
val Type: Mode = new Mode(4) | StableTerm
303317

304318
/** Both term and type symbols are allowed */
305-
val Import: Mode = new Mode(4) | Term | Type
319+
val Import: Mode = new Mode(8) | Term | Type
306320
}
307321

308322
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,15 @@ class CompletionTest {
188188
assertFalse(results.exists((label, _, _) => label == "Zig"))
189189
})
190190
}
191+
192+
@Test def typeCompletionShowsTerm: Unit = {
193+
code"""class Bar
194+
|object Foo {
195+
| val bar = new Bar
196+
| def baz = new Bar
197+
| object bat
198+
| val bizz: ba${m1}
199+
|}""".withSource
200+
.completion(m1, Set(("bar", Field, "Bar"), ("bat", Module, "Foo.bat")))
201+
}
191202
}

0 commit comments

Comments
 (0)