Skip to content

Commit b3befee

Browse files
committed
NavigateAST#pathTo: Add an option to skip over zero-extent nodes
1 parent 3948861 commit b3befee

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ object NavigateAST {
6161
/** The reverse path from node `from` to the node that closest encloses position `pos`,
6262
* or `Nil` if no such path exists. If a non-empty path is returned it starts with
6363
* the node closest enclosing `pos` and ends with `from`.
64+
*
65+
* @param skipZeroExtent If true, skip over zero-extent nodes in the search. These nodes
66+
* do not correspond to code the user wrote since their start and
67+
* end point are the same, so this is useful when trying to reconcile
68+
* nodes with source code.
6469
*/
65-
def pathTo(pos: Position, from: Positioned)(implicit ctx: Context): List[Positioned] = {
70+
def pathTo(pos: Position, from: Positioned, skipZeroExtent: Boolean = false)(implicit ctx: Context): List[Positioned] = {
6671
def childPath(it: Iterator[Any], path: List[Positioned]): List[Positioned] = {
6772
while (it.hasNext) {
6873
val path1 = it.next match {
@@ -75,7 +80,7 @@ object NavigateAST {
7580
path
7681
}
7782
def singlePath(p: Positioned, path: List[Positioned]): List[Positioned] =
78-
if (p.pos contains pos) {
83+
if (p.pos.exists && !(skipZeroExtent && p.pos.isZeroExtent) && p.pos.contains(pos)) {
7984
// FIXME: We shouldn't be manually forcing trees here, we should replace
8085
// our usage of `productIterator` by something in `Positioned` that takes
8186
// care of low-level details like this for us.

compiler/src/dotty/tools/dotc/util/Positions.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ object Positions {
7979
/** Is this position source-derived? */
8080
def isSourceDerived = !isSynthetic
8181

82+
/** Is this a zero-extent position? */
83+
def isZeroExtent = start == end
84+
8285
/** A position where all components are shifted by a given `offset`
8386
* relative to this position.
8487
*/

0 commit comments

Comments
 (0)