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

The following examples show how to use org.eclipse.xtext.nodemodel.ILeafNode#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: IndentationAwareCompletionPrefixProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private PeekingIterator<ILeafNode> createReversedLeafIterator(INode root, INode candidate, LinkedList<ILeafNode> sameGrammarElement) {
	EObject grammarElement = null;
	PeekingIterator<ILeafNode> iterator = Iterators.peekingIterator(Iterators.filter(root.getAsTreeIterable().reverse().iterator(), ILeafNode.class));
	// traverse until we find the current candidate
	while(iterator.hasNext()) {
		ILeafNode next = iterator.next();
		if (candidate.equals(next)) {
			break;
		} else if (next.getTotalLength() == 0) {
			EObject otherGrammarElement = tryGetGrammarElementAsRule(next);
			if (grammarElement == null) {
				grammarElement = otherGrammarElement;
			}
			if (otherGrammarElement.equals(grammarElement)) {
				sameGrammarElement.add(next);
			} else {
				sameGrammarElement.removeLast();
			}
		}
	}
	return iterator;
}
 
Example 2
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeTypeRef_Classifier(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	Grammar grammar = GrammarUtil.getGrammar(model);
	ContentAssistContext.Builder myContextBuilder = context.copy();
	myContextBuilder.setMatcher(new ClassifierPrefixMatcher(context.getMatcher(), classifierQualifiedNameConverter));
	if (model instanceof TypeRef) {
		ICompositeNode node = NodeModelUtils.getNode(model);
		if (node != null) {
			int offset = node.getOffset();
			Region replaceRegion = new Region(offset, context.getReplaceRegion().getLength()
					+ context.getReplaceRegion().getOffset() - offset);
			myContextBuilder.setReplaceRegion(replaceRegion);
			myContextBuilder.setLastCompleteNode(node);
			StringBuilder availablePrefix = new StringBuilder(4);
			for (ILeafNode leaf : node.getLeafNodes()) {
				if (leaf.getGrammarElement() != null && !leaf.isHidden()) {
					if ((leaf.getTotalLength() + leaf.getTotalOffset()) < context.getOffset())
						availablePrefix.append(leaf.getText());
					else
						availablePrefix.append(leaf.getText().substring(0,
								context.getOffset() - leaf.getTotalOffset()));
				}
				if (leaf.getTotalOffset() >= context.getOffset())
					break;
			}
			myContextBuilder.setPrefix(availablePrefix.toString());
		}
	}
	ContentAssistContext myContext = myContextBuilder.toContext();
	for (AbstractMetamodelDeclaration declaration : grammar.getMetamodelDeclarations()) {
		if (declaration.getEPackage() != null) {
			createClassifierProposals(declaration, model, myContext, acceptor);
		}
	}
}
 
Example 3
Source File: InvariantChecker.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected int doCheckLeafNodeAndReturnLength(ILeafNode leafNode, int startsAt) {
	if (leafNode.getTotalOffset() != startsAt)
		throw new InconsistentNodeModelException("node with unexpected offset");
	return leafNode.getTotalLength();
}
 
Example 4
Source File: IndentationAwareCompletionPrefixProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected INode findBestEndToken(INode root, INode candidate, int completionColumn, boolean candidateIsEndToken) {
	LinkedList<ILeafNode> sameGrammarElement = Lists.newLinkedList();
	PeekingIterator<ILeafNode> iterator = createReversedLeafIterator(root, candidate, sameGrammarElement);
	if (!iterator.hasNext()) {
		return candidate;
	}
	// collect all candidates that belong to the same offset
	LinkedList<ILeafNode> sameOffset = candidateIsEndToken ? collectLeafsWithSameOffset((ILeafNode)candidate, iterator) : Lists.newLinkedList();
	// continue until we find a paired leaf with length 0 that is at the correct offset
	EObject grammarElement = tryGetGrammarElementAsRule(candidateIsEndToken || sameGrammarElement.isEmpty() ? candidate : sameGrammarElement.getLast()); 
	ILeafNode result = candidateIsEndToken ? null : (ILeafNode) candidate;
	int sameOffsetSize = sameOffset.size();
	while(iterator.hasNext()) {
		ILeafNode next = iterator.next();
		if (result == null || result.isHidden()) {
			result = next;
		}
		if (next.getTotalLength() == 0) {
			// potential indentation token
			EObject rule = tryGetGrammarElementAsRule(next);
			if (rule != grammarElement) {
				LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(root, next.getTotalOffset());
				if (lineAndColumn.getColumn() <= completionColumn) {
					return result;
				} else {
					if (sameOffset.isEmpty()) {
						if (sameGrammarElement.isEmpty()) {
							result = null;	
						} else {
							result = sameGrammarElement.removeLast();
						}
						
					} else {
						if (sameOffsetSize >= sameOffset.size()) {
							result = sameOffset.removeLast();	
						} else {
							sameOffset.removeLast();
						}
					}
				}
			} else {
				sameOffset.add(next);
			}
		}
	}
	return candidate;
}