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;
}
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());
}
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
}
}
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());
}
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());
}
Aggregations