use of com.github.javaparser.ast.visitor.TreeVisitor in project javaparser by javaparser.
the class LexicalPreservingPrinter method storeInitialText.
private static void storeInitialText(Node root) {
Map<Node, List<JavaToken>> tokensByNode = new IdentityHashMap<>();
// range of interest and ignore all other branches
for (JavaToken token : root.getTokenRange().get()) {
Range tokenRange = token.getRange().orElseThrow(() -> new RuntimeException("Token without range: " + token));
Node owner = findNodeForToken(root, tokenRange);
if (owner == null) {
throw new RuntimeException("Token without node owning it: " + token);
}
if (!tokensByNode.containsKey(owner)) {
tokensByNode.put(owner, new LinkedList<>());
}
tokensByNode.get(owner).add(token);
}
// Now that we know the tokens we use them to create the initial NodeText for each node
new TreeVisitor() {
@Override
public void process(Node node) {
if (!PhantomNodeLogic.isPhantomNode(node)) {
LexicalPreservingPrinter.storeInitialTextForOneNode(node, tokensByNode.get(node));
}
}
}.visitBreadthFirst(root);
}
Aggregations