Search in sources :

Example 11 with AbstractNode

use of org.eclipse.xtext.nodemodel.impl.AbstractNode in project xtext-core by eclipse.

the class NodeModelUtils method findLeafNodeAtOffset.

/**
 * Find the leaf node at the given offset. May return <code>null</code> if the given offset is not valid for the
 * node (sub-)tree.
 *
 * A node matches the <code>leafNodeOffset</code> if it fulfills the following condition:
 * <pre>
 *  node.totalOffset <= leafNodeOffset &&
 *  node.totalEndOffset > leafNodeOffset
 * </pre>
 *
 * @param node the container node. May not be <code>null</code>.
 * @param leafNodeOffset the offset that is covered by the searched node.
 * @return the leaf node at the given offset or <code>null</code>.
 */
/* @Nullable */
public static ILeafNode findLeafNodeAtOffset(/* @NonNull */
INode node, int leafNodeOffset) {
    INode localNode = node;
    while (!(localNode instanceof AbstractNode)) {
        localNode = localNode.getParent();
    }
    int offset = localNode.getTotalOffset();
    int length = localNode.getTotalLength();
    BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) localNode).basicIterator();
    if (leafNodeOffset > (offset + length) / 2) {
        while (iterator.hasPrevious()) {
            AbstractNode previous = iterator.previous();
            int previousOffset = previous.getTotalOffset();
            int previousLength = previous.getTotalLength();
            if (!intersects(previousOffset, previousLength, leafNodeOffset)) {
                if (previousOffset + previousLength <= leafNodeOffset) {
                    return null;
                }
                iterator.prune();
            } else {
                if (previous instanceof ILeafNode)
                    return (ILeafNode) previous;
            }
        }
    } else {
        while (iterator.hasNext()) {
            AbstractNode next = iterator.next();
            int nextOffset = next.getTotalOffset();
            int nextLength = next.getTotalLength();
            if (!intersects(nextOffset, nextLength, leafNodeOffset)) {
                if (nextOffset > leafNodeOffset) {
                    return null;
                }
                iterator.prune();
            } else {
                if (next instanceof ILeafNode)
                    return (ILeafNode) next;
            }
        }
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) AbstractNode(org.eclipse.xtext.nodemodel.impl.AbstractNode)

Example 12 with AbstractNode

use of org.eclipse.xtext.nodemodel.impl.AbstractNode in project xtext-core by eclipse.

the class AbstractNodeTest method testGetRootNode_NoParent.

@Test
public void testGetRootNode_NoParent() {
    AbstractNode node = createNode();
    assertNull(node.getRootNode());
}
Also used : AbstractNode(org.eclipse.xtext.nodemodel.impl.AbstractNode) Test(org.junit.Test)

Example 13 with AbstractNode

use of org.eclipse.xtext.nodemodel.impl.AbstractNode in project xtext-core by eclipse.

the class AbstractNodeTest method testIterator_Previous.

@Test
public void testIterator_Previous() {
    AbstractNode node = createNode();
    BidiIterator<INode> iterator = node.iterator();
    assertTrue(iterator.hasPrevious());
    assertSame(node, iterator.previous());
    assertFalse(iterator.hasPrevious());
    try {
        iterator.previous();
        fail("Expected NoSuchElementException");
    } catch (NoSuchElementException e) {
    // ok
    }
}
Also used : AbstractNode(org.eclipse.xtext.nodemodel.impl.AbstractNode) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 14 with AbstractNode

use of org.eclipse.xtext.nodemodel.impl.AbstractNode in project xtext-core by eclipse.

the class AbstractNodeTest method testIterator_Bidi.

@Test
public void testIterator_Bidi() {
    AbstractNode node = createNode();
    BidiIterator<INode> iterator = node.iterator();
    assertSame(node, iterator.next());
    assertTrue(iterator.hasPrevious());
    assertSame(node, iterator.previous());
    assertTrue(iterator.hasNext());
}
Also used : AbstractNode(org.eclipse.xtext.nodemodel.impl.AbstractNode) Test(org.junit.Test)

Example 15 with AbstractNode

use of org.eclipse.xtext.nodemodel.impl.AbstractNode in project xtext-core by eclipse.

the class AbstractNodeTest method testGetPreviousSibling_FirstChild.

@Test
public void testGetPreviousSibling_FirstChild() {
    ICompositeNode rootNode = builder.newRootNode("input");
    AbstractNode first = createNode();
    AbstractNode second = createNode();
    builder.addChild(rootNode, first);
    builder.addChild(rootNode, second);
    assertFalse(first.hasPreviousSibling());
    assertNull(first.getPreviousSibling());
    assertTrue(first.hasSiblings());
}
Also used : AbstractNode(org.eclipse.xtext.nodemodel.impl.AbstractNode) Test(org.junit.Test)

Aggregations

AbstractNode (org.eclipse.xtext.nodemodel.impl.AbstractNode)25 Test (org.junit.Test)22 NoSuchElementException (java.util.NoSuchElementException)6 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)3 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 CompositeNode (org.eclipse.xtext.nodemodel.impl.CompositeNode)2 SyntheticCompositeNode (org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode)2 INode (org.eclipse.xtext.nodemodel.INode)1