Java Code Examples for org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator#hasPrevious()

The following examples show how to use org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator#hasPrevious() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HiddenLeafAccess.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ILeafNode findPreviousLeaf(final INode node, final Function1<? super ILeafNode, ? extends Boolean> matches) {
  INode current = node;
  while ((current instanceof ICompositeNode)) {
    current = ((ICompositeNode)current).getLastChild();
  }
  if (((current instanceof ILeafNode) && (matches.apply(((ILeafNode) current))).booleanValue())) {
    return ((ILeafNode) current);
  }
  if ((current != null)) {
    final NodeIterator ni = new NodeIterator(current);
    while (ni.hasPrevious()) {
      {
        final INode previous = ni.previous();
        if (((previous instanceof ILeafNode) && (matches.apply(((ILeafNode) previous))).booleanValue())) {
          return ((ILeafNode) previous);
        }
      }
    }
  }
  return null;
}
 
Example 2
Source File: HiddenTokenSequencer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected Set<INode> getLeadingCommentsIncludingWhitespace(ILeafNode node) {
	Set<INode> associatedNodes = Sets.newHashSet();
	Set<INode> pendingWhitespace = Sets.newHashSet();
	INode lastLink = node;
	NodeIterator ni = new NodeIterator(lastLink);
	while(ni.hasPrevious()) {
		INode prev = ni.previous();
		if (tokenUtil.isCommentNode(prev)) {
			if (isLeadingCommentFor(prev, lastLink)) {
				lastLink = prev;
				associatedNodes.addAll(pendingWhitespace);
				associatedNodes.add(prev);
			} else {
				break;
			}
		} else if (tokenUtil.isWhitespaceNode(prev)) {
			pendingWhitespace.add(prev);
		} else if (prev instanceof ILeafNode){
			break;
		}
	}
	return associatedNodes;
}
 
Example 3
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Helper method that collects all the preceding hidden tokens for the given node.
 *
 * @param to
 *          node model element to get the tokens for, must not be {@code null}
 * @param deletedSemanticElements
 *          set of semantic elements that have been deleted from the model, must not be {@code null}
 * @return list of hidden tokens, never {@code null}, can be empty
 */
private List<INode> getPrecedingHiddenTokens(final INode to, final Set<EObject> deletedSemanticElements) {
  List<INode> result = Lists.newLinkedList();
  NodeIterator nodes = new NodeIterator(to);
  while (nodes.hasPrevious()) {
    INode previous = nodes.previous();
    if (previous.getTotalEndOffset() < rootOffset || previous.equals(lastEmittedNode)) {
      break;
    } else if (tokenUtil.isWhitespaceOrCommentNode(previous)) {
      if (!emittedComments.contains(previous)) {
        result.add(0, previous);
      }
    } else if (belongsToDeletedElement(previous)) {
      handleDeletedElement(result, deletedSemanticElements, previous);
      nodes.prune();
    } else {
      break;
    }
  }
  return result;
}
 
Example 4
Source File: HiddenLeafAccess.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected List<ILeafNode> findPreviousHiddenLeafs(final INode node) {
  List<ILeafNode> _xblockexpression = null;
  {
    INode current = node;
    while ((current instanceof ICompositeNode)) {
      current = ((ICompositeNode)current).getLastChild();
    }
    final ArrayList<ILeafNode> result = CollectionLiterals.<ILeafNode>newArrayList();
    if ((current != null)) {
      final NodeIterator ni = new NodeIterator(current);
      while (ni.hasPrevious()) {
        {
          final INode previous = ni.previous();
          if (((!Objects.equal(previous, current)) && (previous instanceof ILeafNode))) {
            boolean _isHidden = ((ILeafNode) previous).isHidden();
            if (_isHidden) {
              result.add(((ILeafNode) previous));
            } else {
              return ListExtensions.<ILeafNode>reverse(result);
            }
          }
        }
      }
    }
    _xblockexpression = ListExtensions.<ILeafNode>reverse(result);
  }
  return _xblockexpression;
}
 
Example 5
Source File: NodeIteratorTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void checkDescending(int index) {
	int i = index;
	NodeIterator nodeIterator = new NodeIterator(nodes[i]);
	while (nodeIterator.hasPrevious()) {
		assertEquals(nodes[--i], nodeIterator.previous());
	}
}
 
Example 6
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the leading comments of the given leaf node.
 *
 * @param node
 *          leaf element of the node model to look up to, must not be {@code null}
 * @return list of hidden tokens, can be empty, never {@code null}
 */
private List<INode> getLeadingCommentsIncludingWhitespace(final ILeafNode node) {
  List<INode> associatedNodes = Lists.newLinkedList();
  Set<INode> pendingWhitespace = Sets.newHashSet();
  INode lastLink = node;
  NodeIterator nodes = new NodeIterator(lastLink);
  while (nodes.hasPrevious()) {
    INode prev = nodes.previous();
    if (tokenUtil.isCommentNode(prev)) {
      if (isLeadingCommentFor(prev, lastLink)) {
        lastLink = prev;
        associatedNodes.addAll(0, pendingWhitespace);
        pendingWhitespace.clear();
        associatedNodes.add(0, prev);
      } else {
        break;
      }
    } else if (tokenUtil.isWhitespaceNode(prev)) {
      pendingWhitespace.add(prev);
    } else if (prev instanceof ILeafNode) {
      break;
    }
  }
  associatedNodes.addAll(0, pendingWhitespace);
  // We want trailing comments to have precedence over leading comments
  // Therefore all the trailing comments of the previous node must be computed
  // and deleted from the results, as they have been already output in previous call of 'emitTrailingTokens' method
  EObject nodeObject = NodeModelUtils.findActualSemanticObjectFor(node);
  ICompositeNode actualNode = NodeModelUtils.findActualNodeFor(nodeObject);
  INode previousNode = actualNode.getPreviousSibling();
  if (previousNode instanceof ICompositeNode) {
    associatedNodes.removeAll(getTrailingCommentsIncludingWhitespace(previousNode));
  }

  // Following line is need to insert a new line after the leading comments if it is needed, but wasn't originally present
  // See also usage in 'emitHiddenTokens'
  associatedNodes.add(0, new LeadingCommentsMarkerNode());
  return associatedNodes;
}