Java Code Examples for org.eclipse.xtext.nodemodel.BidiTreeIterator#hasPrevious()

The following examples show how to use org.eclipse.xtext.nodemodel.BidiTreeIterator#hasPrevious() . 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: TokenSequencePreservingPartialParsingHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns true if the previous document state was completely broken, e.g. the parser did not recover at all.
 * This may happen e.g. in Xtend for documents like
 * <pre>import static class C {}</pre>
 * where the class keyword is consumed as an invalid token in the import declaration and everything thereafter
 * is unrecoverable.
 */
protected boolean isBrokenPreviousState(IParseResult previousParseResult, int offset) {
	if (previousParseResult.hasSyntaxErrors()) {
		BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) previousParseResult.getRootNode()).basicIterator();
		while(iterator.hasPrevious()) {
			AbstractNode previous = iterator.previous();
			if (previous.getGrammarElement() == null) {
				return true;
			}
			if (previous instanceof ILeafNode && previous.getOffset() <= offset) {
				break;
			}
		}
	}
	return false;
}
 
Example 2
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected INode getLastLeafNode() {
	BidiTreeIterator<INode> iter = currentNode.getAsTreeIterable().iterator();
	while(iter.hasPrevious()) {
		INode previous = iter.previous();
		if (previous instanceof ILeafNode)
			return previous;
	}
	return currentNode;
}
 
Example 3
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private INode getLastChild(ICompositeNode parent) {
	BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator();
	while(iterator.hasPrevious()) {
		INode previous = iterator.previous();
		if (previous instanceof ILeafNode) {
			return previous;
		} else if (previous instanceof ICompositeNode) {
			if (!((ICompositeNode) previous).hasChildren())
				return previous;
		}
	}
	return parent;
}
 
Example 4
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private INode getLastLeaf(ICompositeNode parent) {
	BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator();
	while(iterator.hasPrevious()) {
		INode previous = iterator.previous();
		if (previous instanceof ILeafNode) {
			return previous;
		} 
	}
	return null;
}
 
Example 5
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
private INode getLastChild(final ICompositeNode parent) {
  BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator();
  while (iterator.hasPrevious()) {
    INode previous = iterator.previous();
    if (previous instanceof ILeafNode) {
      return previous;
    } else if (previous instanceof ICompositeNode) {
      if (!((ICompositeNode) previous).hasChildren()) {
        return previous;
      }
    }
  }
  return parent;
}
 
Example 6
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
private INode getLastLeaf(final ICompositeNode parent) {
  BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator();
  while (iterator.hasPrevious()) {
    INode previous = iterator.previous();
    if (previous instanceof ILeafNode) {
      return previous;
    }
  }
  return null;
}
 
Example 7
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;
}