List<ILeafNode> currentComments = new ArrayList<ILeafNode>();
NodeIterator nodeIterator = new NodeIterator(rootNode);
// rewind to previous token with token owner
while(nodeIterator.hasPrevious() && currentEObject == null) {
INode node = nodeIterator.previous();
if(tokenUtil.isToken(node)) {
currentEObject = tokenUtil.getTokenOwner(node);
}
}
while(nodeIterator.hasNext()) {
INode node = nodeIterator.next();
if(tokenUtil.isCommentNode(node)) {
currentComments.add((ILeafNode) node);
}
boolean isToken = tokenUtil.isToken(node);
if((node instanceof ILeafNode || isToken) && node.getStartLine() != node.getEndLine() &&
currentEObject != null) {
// found a newline -> associating existing comments with currentEObject
mapping.acceptAfter(currentEObject, currentComments);
// addMapping(mapping, currentComments, currentEObject);
currentEObject = null;
}
if(isToken) {
Pair<List<ILeafNode>, List<ILeafNode>> leadingAndTrailingHiddenTokens = tokenUtil.getLeadingAndTrailingHiddenTokens(node);
for(ILeafNode leadingHiddenNode : leadingAndTrailingHiddenTokens.getFirst()) {
if(tokenUtil.isCommentNode(leadingHiddenNode)) {
currentComments.add(leadingHiddenNode);
}
}
nodeIterator.prune();
currentEObject = tokenUtil.getTokenOwner(node);
if(currentEObject != null) {
mapping.acceptBefore(currentEObject, currentComments);
// addMapping(mapping, currentComments, currentEObject);
if(node.getOffset() > rootNode.getOffset() + rootNode.getLength()) {
// found next EObject outside rootNode
break;
}
}
}