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

The following examples show how to use org.eclipse.xtext.nodemodel.INode#getTotalLength() . 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: N4JSLinker.java    From n4js with 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
Source File: ParserBasedContentAssistContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
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 File: Linker.java    From xtext-xtend with 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
Source File: LeafNodeFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @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
Source File: ContentAssistContextFactory.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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
Source File: NodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * 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;
}