Created
January 20, 2016 18:58
-
-
Save andyscott/08829b5f39422ea52d32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object DocCommentFinder { | |
def findByName[G <: Global](g: G)(rootTree: g.Tree, searchNames: Set[String]): Map[g.Symbol, g.DocComment] = { | |
import g.{ Try ⇒ _, _ } | |
val searchSymbols: Set[Symbol] = searchNames.map(name ⇒ g.rootMirror.staticModule(name)) | |
findBySymbol(g)(rootTree, searchSymbols) | |
} | |
def findBySymbol[G <: Global](g: G)(rootTree: g.Tree, searchSymbols: Set[g.Symbol]): Map[g.Symbol, g.DocComment] = { | |
import g._ | |
class DocCommentSearchTraverser(searchSymbols: Set[Symbol], root: Tree) extends Traverser { | |
val remainingItems = collection.mutable.HashSet[Symbol]() ++ searchSymbols | |
val found = collection.mutable.HashMap[Symbol, DocComment]() | |
def done = remainingItems.isEmpty | |
traverse(root) | |
override def traverse(tree: Tree) { | |
if (!done) { | |
tree match { | |
case DocDef(comment, subTree) if remainingItems.contains(subTree.symbol) ⇒ | |
remainingItems -= subTree.symbol | |
found += subTree.symbol → comment | |
super.traverse(subTree) | |
case _ ⇒ super.traverse(tree) | |
} | |
} | |
} | |
} | |
(new DocCommentSearchTraverser(searchSymbols, rootTree)).found.toMap | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment