Skip to content

Commit 384d297

Browse files
committed
Make pathTo work for overlapping ranges
So far pathTo(span) would only guarantee to return the path to the closest tree enclosing `span` if trees were non-overlapping. This is property does not hold for typed trees, yet `pathTo` is called from `Interactive` on such typed trees. This commit makes `pathTo` work correctly also for overlapping trees.
1 parent c8266dc commit 384d297

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

compiler/src/dotty/tools/dotc/ast/NavigateAST.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,19 @@ object NavigateAST {
7070
*/
7171
def pathTo(span: Span, from: Positioned, skipZeroExtent: Boolean = false)(implicit ctx: Context): List[Positioned] = {
7272
def childPath(it: Iterator[Any], path: List[Positioned]): List[Positioned] = {
73+
var bestFit: List[Positioned] = path
7374
while (it.hasNext) {
7475
val path1 = it.next() match {
7576
case p: Positioned => singlePath(p, path)
7677
case m: untpd.Modifiers => childPath(m.productIterator, path)
7778
case xs: List[_] => childPath(xs.iterator, path)
7879
case _ => path
7980
}
80-
if (path1 ne path) return path1
81+
if ((path1 ne path) &&
82+
((bestFit eq path) || bestFit.head.span.contains(path1.head.span)))
83+
bestFit = path1
8184
}
82-
path
85+
bestFit
8386
}
8487
def singlePath(p: Positioned, path: List[Positioned]): List[Positioned] =
8588
if (p.span.exists && !(skipZeroExtent && p.span.isZeroExtent) && p.span.contains(span)) {

0 commit comments

Comments
 (0)