Skip to content

Search all source trees for a given span #14436

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

Merged
merged 2 commits into from
Feb 17, 2022
Merged
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
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/ast/NavigateAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ object NavigateAST {
* the given `span`.
*/
def untypedPath(span: Span)(using Context): List[Positioned] =
pathTo(span, ctx.compilationUnit.untpdTree)
pathTo(span, List(ctx.compilationUnit.untpdTree))


/** The reverse path from node `from` to the node that closest encloses `span`,
/** The reverse path from any node in `from` to the node that closest encloses `span`,
* or `Nil` if no such path exists. If a non-empty path is returned it starts with
* the node closest enclosing `span` and ends with `from`.
* the node closest enclosing `span` and ends with one of the nodes in `from`.
*
* @param skipZeroExtent If true, skip over zero-extent nodes in the search. These nodes
* do not correspond to code the user wrote since their start and
* end point are the same, so this is useful when trying to reconcile
* nodes with source code.
*/
def pathTo(span: Span, from: Positioned, skipZeroExtent: Boolean = false)(using Context): List[Positioned] = {
Copy link
Member

Choose a reason for hiding this comment

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

The documentation comment above needs to be slightly tweaked: it says "from node from" but I guess now it would be "from any node in from"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. Good catch!

def pathTo(span: Span, from: List[Positioned], skipZeroExtent: Boolean = false)(using Context): List[Positioned] = {
def childPath(it: Iterator[Any], path: List[Positioned]): List[Positioned] = {
var bestFit: List[Positioned] = path
while (it.hasNext) {
Expand Down Expand Up @@ -120,6 +120,6 @@ object NavigateAST {
case _ => path
}
}
singlePath(from, Nil)
childPath(from.iterator, Nil)
}
}
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/interactive/Interactive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ object Interactive {
* the tree closest enclosing `pos` and ends with an element of `trees`.
*/
def pathTo(trees: List[SourceTree], pos: SourcePosition)(using Context): List[Tree] =
trees.find(_.pos.contains(pos)) match {
case Some(tree) => pathTo(tree.tree, pos.span)
case None => Nil
}
pathTo(trees.map(_.tree), pos.span)

def pathTo(tree: Tree, span: Span)(using Context): List[Tree] =
if (tree.span.contains(span))
NavigateAST.pathTo(span, tree, skipZeroExtent = true)
pathTo(List(tree), span)

private def pathTo(trees: List[Tree], span: Span)(using Context): List[Tree] =
if (trees.exists(_.span.contains(span)))
NavigateAST.pathTo(span, trees, skipZeroExtent = true)
.collect { case t: untpd.Tree => t }
.dropWhile(!_.hasType).asInstanceOf[List[tpd.Tree]]
else Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,13 @@ class HoverTest {
|""".withSource
.hover(m1 to m2, hoverContent("example.SimpleEnum.Color"))
}

@Test def enums: Unit = {
code"""|package example
|enum TestEnum3:
| case ${m1}A${m2} // no tooltip
|
|""".withSource
.hover(m1 to m2, hoverContent("example.TestEnum3"))
}
}