Java Code Examples for org.eclipse.xtext.nodemodel.INode#getTotalLength()

The following examples show how to use org.eclipse.xtext.nodemodel.INode#getTotalLength() . These examples are extracted from open source projects. 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 Project: n4js   File: N4JSLinker.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * The node itself has content or if it is a composite node its last child node is a leaf (i.e. this child node has
 * no further child nodes).
 */
@Override
protected boolean hasLeafNodes(INode node) {
	if (node.getTotalLength() > 0)
		return true;
	if (node instanceof ICompositeNode) {
		return ((ICompositeNode) node).getLastChild() instanceof ILeafNode;
	}
	return false;
}
 
Example 2
public String getPrefix(INode prefixNode) {
	if (prefixNode instanceof ILeafNode) {
		if (((ILeafNode) prefixNode).isHidden() && prefixNode.getGrammarElement() != null)
			return "";
		return getNodeTextUpToCompletionOffset(prefixNode);
	}
	StringBuilder result = new StringBuilder(prefixNode.getTotalLength());
	doComputePrefix((ICompositeNode) prefixNode, result);
	return result.toString();
}
 
Example 3
Source Project: xtext-xtend   File: Linker.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected boolean hasLeafNodes(INode node) {
	if (node.getTotalLength() > 0)
		return true;
	if (node instanceof ICompositeNode) {
		return ((ICompositeNode) node).getLastChild() instanceof ILeafNode;
	}
	return false;
}
 
Example 4
/**
 * @since 2.13
 */
protected boolean matchesSearchCriteria(INode object) {
	if (leading) {
		if (object.getTotalOffset() < offset && object.getTotalLength() + object.getTotalOffset() >= offset) {
			return true;
		}
	} else {
		if (object.getTotalOffset() <= offset && object.getTotalLength() + object.getTotalOffset() > offset) {
			return true;
		}
	}
	return object.getTotalOffset() == offset && object.getTotalLength() == 0;
}
 
Example 5
public String getPrefix(INode prefixNode) {
	if (prefixNode instanceof ILeafNode) {
		if (((ILeafNode) prefixNode).isHidden() && prefixNode.getGrammarElement() != null)
			return "";
		return getNodeTextUpToCompletionOffset(prefixNode);
	}
	StringBuilder result = new StringBuilder(prefixNode.getTotalLength());
	doComputePrefix((ICompositeNode) prefixNode, result);
	return result.toString();
}
 
Example 6
/**
 * 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 &lt;= leafNodeOffset &amp;&amp;
 *  node.totalEndOffset &gt; 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;
}