Skip to content

Commit 61c5c26

Browse files
committed
Fix scala#4978: Complete imported symbol in REPL
1 parent da2a245 commit 61c5c26

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,22 @@ object Interactive {
9494
private def safely[T](op: => List[T]): List[T] =
9595
try op catch { case ex: TypeError => Nil }
9696

97+
private def addExtraImports(extraImports: List[untpd.Import], ctx: Context): Context = {
98+
extraImports.foldLeft(ctx) { case (c, i) => c.importContext(i, i.symbol(c)) }
99+
}
100+
97101
/** Get possible completions from tree at `pos`
98102
*
103+
* @param pos The cursor position in the current compilation unit, where the
104+
* completion should be introduced.
105+
* @param extraImports Additional import statements that are not reflected in
106+
* the compilation unit, but which should be considered.
99107
* @return offset and list of symbols for possible completions
100108
*/
101-
def completions(pos: SourcePosition)(implicit ctx: Context): (Int, List[Symbol]) = {
109+
def completions(pos: SourcePosition, extraImports: List[untpd.Import] = Nil)(implicit ctx: Context): (Int, List[Symbol]) = {
102110
val path = pathTo(ctx.compilationUnit.tpdTree, pos.pos)
103-
computeCompletions(pos, path)(contextOfPath(path))
111+
val completionCtx = addExtraImports(extraImports, contextOfPath(path))
112+
computeCompletions(pos, path)(completionCtx)
104113
}
105114

106115
private def computeCompletions(pos: SourcePosition, path: List[Tree])(implicit ctx: Context): (Int, List[Symbol]) = {

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class ReplDriver(settings: Array[String],
167167
unit.tpdTree = tree
168168
implicit val ctx = state.context.fresh.setCompilationUnit(unit)
169169
val srcPos = SourcePosition(file, Position(cursor))
170-
val (_, completions) = Interactive.completions(srcPos)
170+
val (_, completions) = Interactive.completions(srcPos, state.imports)
171171
completions.map(makeCandidate)
172172
}
173173
.getOrElse(Nil)

compiler/test/dotty/tools/repl/TabcompleteTests.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ class TabcompleteTests extends ReplTest {
6060
val expected = List("comp1", "comp2", "comp3")
6161
assertEquals(expected, tabComplete("(new Foo).comp").sorted)
6262
}
63+
64+
@Test def tabCompleteImport =
65+
fromInitialState { implicit state =>
66+
val src = "import java.io.FileDescriptor"
67+
run(src)
68+
}
69+
.andThen { implicit state =>
70+
val expected = List("FileDescriptor")
71+
assertEquals(expected, tabComplete("val foo: FileDesc"))
72+
}
6373
}

0 commit comments

Comments
 (0)