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

The following examples show how to use org.eclipse.xtext.nodemodel.ILeafNode#getTotalOffset() . 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
protected PartialParsingPointers calculatePartialParsingPointers(ICompositeNode oldRoot, ILeafNode left, ILeafNode right) {
	ICompositeNode result = right.getParent();
	while(result.getTotalOffset() > left.getTotalOffset()) {
		result = result.getParent();
	}
	List<ICompositeNode> nodesEnclosingRegion = getAllParents(result);
	Range range = new Range(left.getTotalOffset(), right.getTotalEndOffset());
	List<ICompositeNode> validReplaceRootNodes = internalFindValidReplaceRootNodeForChangeRegion(nodesEnclosingRegion);

	filterInvalidRootNodes(validReplaceRootNodes);

	if (validReplaceRootNodes.isEmpty()) {
		validReplaceRootNodes = Collections.singletonList(oldRoot);
	}
	return new PartialParsingPointers(oldRoot, range.getOffset(), range.getLength(), validReplaceRootNodes, nodesEnclosingRegion);
}
 
Example 2
Source File: AstSelectionProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected ITextRegion getTextRegion(EObject eObject) {
	if (eObject == null)
		return null;
	IParseResult parseResult = ((XtextResource)eObject.eResource()).getParseResult();
	if (parseResult == null)
		return null;
	ICompositeNode rootNode = parseResult.getRootNode();
	Map<ILeafNode, EObject> comments = commentAssociater.associateCommentsWithSemanticEObjects(eObject, singleton(rootNode));
	final ITextRegion result = locationProvider.getFullTextRegion(eObject);
	int start = result.getOffset();
	int end = result.getOffset() + result.getLength();
	for (Entry<ILeafNode, EObject> entry : comments.entrySet()) {
		if (entry.getValue() == eObject) {
			ILeafNode node = entry.getKey();
			if (node.getTotalOffset() < start) {
				start = node.getTotalOffset();
			}
			if (node.getTotalEndOffset() > end) {
				end = node.getTotalEndOffset();
			}
		}
	}
	return new TextRegion(start, end-start);
}
 
Example 3
Source File: AbstractNode.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public int getOffset() {
	Iterator<ILeafNode> leafIter = Iterators.filter(basicIterator(), ILeafNode.class);
	int firstLeafOffset = -1;
	while(leafIter.hasNext()) {
		ILeafNode leaf = leafIter.next();
		if (firstLeafOffset == -1) {
			firstLeafOffset = leaf.getTotalOffset();
		}
		if (!leaf.isHidden())
			return leaf.getTotalOffset();
	}
	if (firstLeafOffset != -1)
		return firstLeafOffset;
	return getTotalOffset();
}
 
Example 4
Source File: ParamAwareEntryPointFinder.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ICompositeNode findEntryPoint(ICompositeNode node, int offset) {
	ICompositeNode result = node;
	result = getApplicableNode(result);
	while (result != null) {
		int remainingLookAhead = result.getLookAhead();
		Iterator<ILeafNode> leafNodes = result.getLeafNodes().iterator();
		while (leafNodes.hasNext()) {
			ILeafNode leaf = leafNodes.next();
			if (leaf.getTotalOffset() >= offset) {
				break;
			}
			if (!leaf.isHidden()) {
				if (remainingLookAhead > 0) {
					remainingLookAhead--;
				}
				if (remainingLookAhead == 0) {
					if (!shouldUseParent(result, offset, leaf)) {
						return result;
					} else {
						break;
					}
				}
			}
		}
		result = getApplicableNode(result.getParent());
	}
	return result;
}
 
Example 5
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Implementation of the {@link TokenSource} interface. Return new tokens as long as there are some, afterwards
 * return {@link Token#EOF_TOKEN}.
 */
@Override
public Token nextToken() {
	if (next != null) {
		Token result = next;
		next = null;
		return result;
	}
	if (!leafNodes.hasNext()) {
		return Token.EOF_TOKEN;
	}
	ILeafNode leaf = leafNodes.next();
	if (leaf.getTotalOffset() >= endOffset) {
		leafNodes = Collections.emptyIterator();
		return Token.EOF_TOKEN;
	}
	if (leaf.getTotalEndOffset() <= startOffset) {
		return nextToken();
	}
	if (leaf.getTotalEndOffset() > endOffset) {
		return toPrefixToken(leaf);
	}
	SyntaxErrorMessage syntaxErrorMessage = leaf.getSyntaxErrorMessage();
	if (syntaxErrorMessage != null && SEMICOLON_INSERTED.equals(syntaxErrorMessage.getIssueCode())) {
		return toASIToken(leaf);
	}
	if (leaf.isHidden()) {
		return processHiddenToken(leaf);
	}
	int tokenType = tokenTypeMapper.getInternalTokenType(leaf);
	return new CommonToken(tokenType, leaf.getText());
}
 
Example 6
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 7
Source File: EntryPointFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ICompositeNode findEntryPoint(ICompositeNode node, int offset) {
	ICompositeNode result = node;
	result = getApplicableNode(result);
	while (result != null) {
		int remainingLookAhead = result.getLookAhead();
		if (remainingLookAhead != 0) {
			Iterator<ILeafNode> leafNodes = result.getLeafNodes().iterator();
			while (leafNodes.hasNext() && remainingLookAhead > 0) {
				ILeafNode leaf = leafNodes.next();
				if (leaf.getTotalOffset() >= offset) {
					break;
				}
				if (!leaf.isHidden()) {
					if (remainingLookAhead > 0) {
						remainingLookAhead--;
					}
					if (remainingLookAhead == 0) {
						if (shouldUseParent(result, offset, leaf)) {
							ICompositeNode parent = result.getParent();
							return parent;
						}
						return result;
					}
				}
			}
		}
		result = getApplicableNode(result.getParent());
	}
	return result;
}
 
Example 8
Source File: IndentationAwareCompletionPrefixProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private LinkedList<ILeafNode> collectLeafsWithSameOffset(ILeafNode candidate, PeekingIterator<ILeafNode> iterator) {
	LinkedList<ILeafNode> sameOffset = Lists.newLinkedList();
	sameOffset.add(candidate);
	int offset = candidate.getTotalOffset();
	while(iterator.hasNext()) {
		ILeafNode peek = iterator.peek();
		if (peek.getTotalOffset() == offset) {
			sameOffset.add(peek);
			iterator.next();
		} else {
			break;
		}
	}
	return sameOffset;
}
 
Example 9
Source File: TokenSequencePreservingPartialParsingHelper.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public IParseResult reparse(IParser parser, IParseResult previousParseResult, ReplaceRegion changedRegion) {
	if (isBrokenPreviousState(previousParseResult, changedRegion.getOffset())) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	ICompositeNode oldRootNode = previousParseResult.getRootNode();
	Iterator<ILeafNode> leafNodes = oldRootNode.getLeafNodes().iterator();
	ILeafNode leftNode = getLeftNode(leafNodes, changedRegion.getOffset());
	if (leftNode == null) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	ILeafNode rightNode = getRightNode(leafNodes, changedRegion.getEndOffset());
	if (rightNode == null) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	while(leafNodes.hasNext()) {
		if (leafNodes.next().getSyntaxErrorMessage() != null) {
			return fullyReparse(parser, previousParseResult, changedRegion);
		}
	}
	String originalText = oldRootNode.getText().substring(leftNode.getTotalOffset());
	StringBuilder newTextBuilder = new StringBuilder(originalText);
	changedRegion.shiftBy(-leftNode.getTotalOffset()).applyTo(newTextBuilder);
	String newText = newTextBuilder.toString();
	if (originalText.equals(newText)) {
		// nothing to do
		return previousParseResult;
	}
	int originalLength = rightNode.getTotalEndOffset() - leftNode.getTotalOffset();
	int expectedLength = originalLength - changedRegion.getLength() + changedRegion.getText().length();
	if (!isSameTokenSequence(originalText.substring(0, originalLength), newText, expectedLength)) {
		// different token sequence, cannot perform a partial parse run
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	
	PartialParsingPointers parsingPointers = calculatePartialParsingPointers(oldRootNode, leftNode, rightNode);
	ICompositeNode replaceMe = getReplacedNode(parsingPointers);
	if (replaceMe == null || replaceMe == oldRootNode || replaceMe.getOffset() == 0 && replaceMe.getEndOffset() == oldRootNode.getLength()) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	String reparseRegion = insertChangeIntoReplaceRegion(replaceMe, changedRegion);
	
	EObject oldSemanticElement = getOldSemanticElement(replaceMe, parsingPointers);
	if (oldSemanticElement == null)
		return fullyReparse(parser, previousParseResult, changedRegion);
	if (oldSemanticElement == replaceMe.getParent().getSemanticElement()) {
		throw new IllegalStateException("oldParent == oldElement");
	}
	
	IParseResult newParseResult = doParseRegion(parser, parsingPointers, replaceMe, reparseRegion);
	if (newParseResult == null) {
		throw new IllegalStateException("Could not perform a partial parse operation");
	}
	
	replaceOldSemanticElement(oldSemanticElement, previousParseResult, newParseResult);
	nodeModelBuilder.replaceAndTransferLookAhead(replaceMe, newParseResult.getRootNode());
	((ParseResult) newParseResult).setRootNode(oldRootNode);
	StringBuilder builder = new StringBuilder(oldRootNode.getText());
	changedRegion.applyTo(builder);
	nodeModelBuilder.setCompleteContent(oldRootNode, builder.toString());
	return newParseResult;
}
 
Example 10
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();
}