Skip to content

Fix #4978: Complete imported symbol in REPL #4979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/interactive/Interactive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,22 @@ object Interactive {
private def safely[T](op: => List[T]): List[T] =
try op catch { case ex: TypeError => Nil }

private def addExtraImports(extraImports: List[untpd.Import], ctx: Context): Context = {
extraImports.foldLeft(ctx) { case (c, i) => c.importContext(i, i.symbol(c)) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.symbol on untyped tree?

}

/** Get possible completions from tree at `pos`
*
* @param pos The cursor position in the current compilation unit, where the
* completion should be introduced.
* @param extraImports Additional import statements that are not reflected in
* the compilation unit, but which should be considered.
* @return offset and list of symbols for possible completions
*/
def completions(pos: SourcePosition)(implicit ctx: Context): (Int, List[Symbol]) = {
def completions(pos: SourcePosition, extraImports: List[untpd.Import] = Nil)(implicit ctx: Context): (Int, List[Symbol]) = {
val path = pathTo(ctx.compilationUnit.tpdTree, pos.pos)
computeCompletions(pos, path)(contextOfPath(path))
val completionCtx = addExtraImports(extraImports, contextOfPath(path))
computeCompletions(pos, path)(completionCtx)
}

private def computeCompletions(pos: SourcePosition, path: List[Tree])(implicit ctx: Context): (Int, List[Symbol]) = {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ReplDriver(settings: Array[String],
unit.tpdTree = tree
implicit val ctx = state.context.fresh.setCompilationUnit(unit)
val srcPos = SourcePosition(file, Position(cursor))
val (_, completions) = Interactive.completions(srcPos)
val (_, completions) = Interactive.completions(srcPos, state.imports)
completions.map(makeCandidate)
}
.getOrElse(Nil)
Expand Down
10 changes: 10 additions & 0 deletions compiler/test/dotty/tools/repl/TabcompleteTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ class TabcompleteTests extends ReplTest {
val expected = List("comp1", "comp2", "comp3")
assertEquals(expected, tabComplete("(new Foo).comp").sorted)
}

@Test def tabCompleteImport =
fromInitialState { implicit state =>
val src = "import java.io.FileDescriptor"
run(src)
}
.andThen { implicit state =>
val expected = List("FileDescriptor")
assertEquals(expected, tabComplete("val foo: FileDesc"))
}
}