Skip to content

Commit 1ecec4c

Browse files
committed
Support jump to definition in top-level imports
This is done by recording the top level imports that appear in every source file.
1 parent 189bf3e commit 1ecec4c

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ object Interactive {
273273
(implicit ctx: Context): List[SourceTree] = safely {
274274
val buf = new mutable.ListBuffer[SourceTree]
275275

276-
trees foreach { case SourceTree(topTree, source) =>
276+
trees foreach { case SourceTree(_, topTree, source) =>
277277
new untpd.TreeTraverser {
278278
override def traverse(tree: untpd.Tree)(implicit ctx: Context) = {
279279
tree match {
@@ -285,7 +285,7 @@ object Interactive {
285285
&& !tree.pos.isZeroExtent
286286
&& (includeReferences || isDefinition(tree))
287287
&& treePredicate(tree))
288-
buf += SourceTree(tree, source)
288+
buf += SourceTree(Nil, tree, source)
289289
traverseChildren(tree)
290290
case tree: untpd.Inlined =>
291291
traverse(tree.call)
@@ -303,11 +303,13 @@ object Interactive {
303303
* or `Nil` if no such path exists. If a non-empty path is returned it starts with
304304
* the tree closest enclosing `pos` and ends with an element of `trees`.
305305
*/
306-
def pathTo(trees: List[SourceTree], pos: SourcePosition)(implicit ctx: Context): List[Tree] =
307-
trees.find(_.pos.contains(pos)) match {
308-
case Some(tree) => pathTo(tree.tree, pos.pos)
306+
def pathTo(trees: List[SourceTree], pos: SourcePosition)(implicit ctx: Context): List[Tree] = {
307+
val allTrees = trees.flatMap(t => t.tree :: t.topLevelImports)
308+
allTrees.find(t => t.pos.contains(pos.pos)) match {
309+
case Some(tree) => pathTo(tree, pos.pos)
309310
case None => Nil
310311
}
312+
}
311313

312314
def pathTo(tree: Tree, pos: Position)(implicit ctx: Context): List[Tree] =
313315
if (tree.pos.contains(pos))

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,15 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
172172
private def topLevelClassTrees(topTree: Tree, source: SourceFile): List[SourceTree] = {
173173
val trees = new mutable.ListBuffer[SourceTree]
174174

175-
def addTrees(tree: Tree): Unit = tree match {
175+
def addTrees(topLevelImports: List[Import], tree: Tree): Unit = tree match {
176176
case PackageDef(_, stats) =>
177-
stats.foreach(addTrees)
177+
val imports = stats.collect { case imp: Import => imp }
178+
stats.foreach(addTrees(imports, _))
178179
case tree: TypeDef =>
179-
trees += SourceTree(tree, source)
180+
trees += SourceTree(topLevelImports, tree, source)
180181
case _ =>
181182
}
182-
addTrees(topTree)
183+
addTrees(Nil, topTree)
183184

184185
trees.toList
185186
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Contexts._, NameOps._, Symbols._
1010
import util._, util.Positions._
1111

1212
/** A typechecked named `tree` coming from `source` */
13-
case class SourceTree(tree: tpd.NameTree, source: SourceFile) {
13+
case class SourceTree(topLevelImports: List[tpd.Import], tree: tpd.NameTree, source: SourceFile) {
1414
/** The position of `tree` */
1515
def pos(implicit ctx: Context): SourcePosition = source.atPos(tree.pos)
1616

@@ -47,10 +47,11 @@ object SourceTree {
4747
import ast.Trees._
4848
def sourceTreeOfClass(tree: tpd.Tree): Option[SourceTree] = tree match {
4949
case PackageDef(_, stats) =>
50-
stats.flatMap(sourceTreeOfClass).headOption
50+
val imports = stats.collect { case imp: tpd.Import => imp }
51+
stats.flatMap(sourceTreeOfClass(_).map(_.copy(topLevelImports = imports))).headOption
5152
case tree: tpd.TypeDef if tree.symbol == sym =>
5253
val sourceFile = new SourceFile(sym.sourceFile, Codec.UTF8)
53-
Some(SourceTree(tree, sourceFile))
54+
Some(SourceTree(Nil, tree, sourceFile))
5455
case _ => None
5556
}
5657
sourceTreeOfClass(sym.treeContaining(id))

0 commit comments

Comments
 (0)