Search in sources :

Example 6 with NodeIterator

use of org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator in project xtext-core by eclipse.

the class HiddenTokenSequencer method getHiddenNodesBetween.

/**
 * @return null if the whitespace between these nodes is unknown.
 * In this case, the formatter needs to decide.
 */
protected List<INode> getHiddenNodesBetween(INode from, INode to) {
    if (from == null && to == null)
        return null;
    List<INode> out = Lists.newArrayList();
    Set<EObject> deletedSemanticElements = Sets.newHashSet();
    if (to == null) {
        NodeIterator ni = new NodeIterator(from);
        while (ni.hasNext()) {
            INode next = ni.next();
            if (next.getTotalOffset() > rootEndOffset) {
                break;
            }
            if (next == lastEmittedNode) {
                break;
            } else if (tokenUtil.isWhitespaceOrCommentNode(next)) {
                out.add(next);
            } else if (belongsToDeletedElement(next)) {
                handleDeletedElement(out, deletedSemanticElements, next);
                ni.prune();
            } else {
                break;
            }
        }
    } else if (from == null) {
        NodeIterator ni = new NodeIterator(to);
        while (ni.hasPrevious()) {
            INode prev = ni.previous();
            if (prev.getTotalEndOffset() < rootOffset) {
                break;
            }
            if (prev == lastEmittedNode) {
                break;
            } else if (tokenUtil.isWhitespaceOrCommentNode(prev)) {
                out.add(0, prev);
            } else if (belongsToDeletedElement(prev)) {
                handleDeletedElement(out, deletedSemanticElements, prev);
                ni.prune();
            } else {
                break;
            }
        }
    } else {
        NodeIterator ni = new NodeIterator(from);
        while (ni.hasNext()) {
            INode next = ni.next();
            if (tokenUtil.isWhitespaceOrCommentNode(next)) {
                out.add(next);
            } else if (next.equals(to)) {
                if (next instanceof ICompositeNode && (GrammarUtil.isDatatypeRuleCall(next.getGrammarElement()) || GrammarUtil.isEnumRuleCall(next.getGrammarElement()) || next.getGrammarElement() instanceof CrossReference)) {
                    while (ni.hasNext()) {
                        INode next2 = ni.next();
                        if (tokenUtil.isWhitespaceOrCommentNode(next2)) {
                            out.add(next2);
                        } else if (next2 instanceof ILeafNode) {
                            break;
                        }
                    }
                    break;
                } else {
                    break;
                }
            } else if (belongsToDeletedElement(next)) {
                handleDeletedElement(out, deletedSemanticElements, next);
                ni.prune();
            } else if (tokenUtil.isToken(next)) {
                break;
            }
        }
    }
    if ((from == null || to == null) && out.isEmpty()) {
        return null;
    } else {
        return filterNodesOfDeletedElements(out, deletedSemanticElements);
    }
}
Also used : NodeIterator(org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) CrossReference(org.eclipse.xtext.CrossReference)

Example 7 with NodeIterator

use of org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator in project xtext-core by eclipse.

the class HiddenTokenSequencer method getLeadingCommentsIncludingWhitespace.

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;
}
Also used : NodeIterator(org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode)

Example 8 with NodeIterator

use of org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator in project xtext-core by eclipse.

the class NodeIteratorTest method testPruneComposite.

@Test
public void testPruneComposite() throws Exception {
    NodeIterator nodeIterator = new NodeIterator(nodes[3]);
    nodeIterator.prune();
    assertEquals(nodes[6], nodeIterator.next());
    assertEquals(nodes[3], nodeIterator.previous());
    assertEquals(nodes[2], nodeIterator.previous());
    assertEquals(nodes[1], nodeIterator.previous());
}
Also used : NodeIterator(org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator) Test(org.junit.Test)

Example 9 with NodeIterator

use of org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator in project xtext-core by eclipse.

the class NodeIteratorTest method testPruneLeaf.

@Test
public void testPruneLeaf() throws Exception {
    // pruning a leaf should not have any effect
    NodeIterator nodeIterator = new NodeIterator(nodes[8]);
    nodeIterator.prune();
    assertEquals(nodes[9], nodeIterator.next());
    assertEquals(nodes[8], nodeIterator.previous());
    assertEquals(nodes[7], nodeIterator.previous());
    assertEquals(nodes[6], nodeIterator.previous());
}
Also used : NodeIterator(org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator) Test(org.junit.Test)

Aggregations

NodeIterator (org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator)9 INode (org.eclipse.xtext.nodemodel.INode)5 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)4 CrossReference (org.eclipse.xtext.CrossReference)2 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 Test (org.junit.Test)2 EObject (org.eclipse.emf.ecore.EObject)1