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

The following examples show how to use org.eclipse.xtext.nodemodel.ILeafNode#getTextRegion() . 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: SemanticHighlighter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Highlights the non-hidden parts of {@code node} with the style that is associated with {@code id}.
 */
protected void highlightNode(INode node, String id, IHighlightedPositionAcceptor acceptor) {
	if (node == null)
		return;
	if (node instanceof ILeafNode) {
		ITextRegion textRegion = node.getTextRegion();
		acceptor.addPosition(textRegion.getOffset(), textRegion.getLength(), id);
	} else {
		for (ILeafNode leaf : node.getLeafNodes()) {
			if (!leaf.isHidden()) {
				ITextRegion leafRegion = leaf.getTextRegion();
				acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(), id);
			}
		}
	}
}
 
Example 2
Source File: FormatterTester.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected ArrayList<TextReplacement> createMissingEditReplacements(XtextResource res, Collection<TextReplacement> edits, int offset,
		int length) {
	Set<Integer> offsets = IterableExtensions
			.toSet(Iterables.transform(edits, (TextReplacement it) -> Integer.valueOf(it.getOffset())));
	ArrayList<TextReplacement> result = new ArrayList<>();
	int lastOffset = 0;
	IParseResult parseResult = res.getParseResult();
	if (parseResult != null) {
		ICompositeNode rootNode = parseResult.getRootNode();
		if (rootNode != null) {
			for (ILeafNode leaf : rootNode.getLeafNodes()) {
				if (!leaf.isHidden() || !StringExtensions.isNullOrEmpty(leaf.getText().trim())) {
					ITextRegion leafRegion = leaf.getTextRegion();
					if (lastOffset >= offset && leafRegion.getOffset() <= offset + length && !offsets.contains(Integer.valueOf(lastOffset))) {
						result.add(new TextReplacement(lastOffset, leafRegion.getOffset() - lastOffset, "!!"));
					}
					lastOffset = leafRegion.getOffset() + leafRegion.getLength();
				}
			}
		}
	}
	return result;
}
 
Example 3
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void replaceKeyword(Keyword keyword, String replacement, EObject container, IXtextDocument document)
		throws BadLocationException {
	ICompositeNode node = NodeModelUtils.findActualNodeFor(container);
	if (node != null) {
		for (ILeafNode leafNode : node.getLeafNodes()) {
			if (leafNode.getGrammarElement() == keyword) {
				ITextRegion leafRegion = leafNode.getTextRegion();
				String actualReplacement = replacement;
				if (!Character.isWhitespace(document.getChar(leafRegion.getOffset() - 1))) {
					actualReplacement = " " + replacement;
				}
				document.replace(leafRegion.getOffset(), leafRegion.getLength(), actualReplacement);
			}
		}
	}
}
 
Example 4
Source File: DefaultSemanticHighlightingCalculator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Highlights the non-hidden parts of {@code node} with the styles given by the {@code styleIds}
 */
protected void highlightNode(IHighlightedPositionAcceptor acceptor, INode node, String... styleIds) {
	if (node == null)
		return;
	if (node instanceof ILeafNode) {
		ITextRegion textRegion = node.getTextRegion();
		acceptor.addPosition(textRegion.getOffset(), textRegion.getLength(), styleIds);
	} else {
		for (ILeafNode leaf : node.getLeafNodes()) {
			if (!leaf.isHidden()) {
				ITextRegion leafRegion = leaf.getTextRegion();
				acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(), styleIds);
			}
		}
	}
}
 
Example 5
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void highlightSpecialIdentifiers(ILeafNode leafNode, IHighlightedPositionAcceptor acceptor,
		TerminalRule idRule) {
	ITextRegion leafRegion = leafNode.getTextRegion();
	if (idLengthsToHighlight.get(leafRegion.getLength())) {
		EObject element = leafNode.getGrammarElement();
		if (element == idRule || (element instanceof RuleCall && ((RuleCall) element).getRule() == idRule)) {
			String text = leafNode.getText();
			String highlightingID = highlightedIdentifiers.get(text);
			if (highlightingID != null) {
				acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(), highlightingID);
			}
		}
	}
}
 
Example 6
Source File: ParserBasedContentAssistContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public boolean doComputePrefix(ICompositeNode node, StringBuilder result) {
	List<ILeafNode> hiddens = Lists.newArrayListWithCapacity(2);
	for (INode child : node.getChildren()) {
		if (child instanceof ICompositeNode) {
			if (!doComputePrefix((ICompositeNode) child, result))
				return false;
		}
		else {
			ILeafNode leaf = (ILeafNode) child;
			ITextRegion leafRegion = leaf.getTextRegion();
			if (leafRegion.getOffset() > completionOffset)
				return false;
			if (leaf.isHidden()) {
				if (result.length() != 0)
					hiddens.add((ILeafNode) child);
			}
			else {
				Iterator<ILeafNode> iter = hiddens.iterator();
				while (iter.hasNext()) {
					result.append(iter.next().getText());
				}
				hiddens.clear();
				result.append(getNodeTextUpToCompletionOffset(leaf));
				if (leafRegion.getOffset() + leafRegion.getLength() > completionOffset)
					return false;
			}
		}
	}
	return true;
}
 
Example 7
Source File: AbstractEObjectHover.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Call this method only from within an IUnitOfWork
 */
protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource, final int offset) {
	// check for cross reference
	EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
	if (crossLinkedEObject != null) {
		if (!crossLinkedEObject.eIsProxy()) {
			IParseResult parseResult = resource.getParseResult();
			if (parseResult != null) {
				ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
				if(leafNode != null && leafNode.isHidden() && leafNode.getOffset() == offset) {
					leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
				}
				if (leafNode != null) {
					ITextRegion leafRegion = leafNode.getTextRegion();
					return Tuples.create(crossLinkedEObject, (IRegion) new Region(leafRegion.getOffset(), leafRegion.getLength()));
				}
			}
		}
	} else {
		EObject o = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
		if (o != null) {
			ITextRegion region = locationInFileProvider.getSignificantTextRegion(o);
			final IRegion region2 = new Region(region.getOffset(), region.getLength());
			if (TextUtilities.overlaps(region2, new Region(offset, 0)))
				return Tuples.create(o, region2);
		}
	}
	return null;
}
 
Example 8
Source File: XtendHighlightingCalculator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void highlightSpecialIdentifiers(ILeafNode leafNode, IHighlightedPositionAcceptor acceptor,
		TerminalRule idRule) {
	super.highlightSpecialIdentifiers(leafNode, acceptor, idRule);
	if (contextualKeywords != null && contextualKeywords.contains(leafNode.getGrammarElement())) {
		ITextRegion leafRegion = leafNode.getTextRegion();
		acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(),
				HighlightingStyles.DEFAULT_ID);
	}
}
 
Example 9
Source File: HoverService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected HoverContext createContext(Document document, XtextResource resource, int offset) {
	EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
	if (crossLinkedEObject != null) {
		if (crossLinkedEObject.eIsProxy()) {
			return null;
		}
		IParseResult parseResult = resource.getParseResult();
		if (parseResult == null) {
			return null;
		}
		ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
		if (leafNode != null && leafNode.isHidden() && leafNode.getOffset() == offset) {
			leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
		}
		if (leafNode == null) {
			return null;
		}
		ITextRegion leafRegion = leafNode.getTextRegion();
		return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject);
	}
	EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
	if (element == null) {
		return null;
	}
	ITextRegion region = locationInFileProvider.getSignificantTextRegion(element);
	return new HoverContext(document, resource, offset, region, element);
}
 
Example 10
Source File: ContentAssistContextFactory.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public boolean doComputePrefix(ICompositeNode node, StringBuilder result) {
	List<ILeafNode> hiddens = Lists.newArrayListWithCapacity(2);
	for (INode child : node.getChildren()) {
		if (child instanceof ICompositeNode) {
			if (!doComputePrefix((ICompositeNode) child, result))
				return false;
		}
		else {
			ILeafNode leaf = (ILeafNode) child;
			ITextRegion leafRegion = leaf.getTextRegion();
			if (leafRegion.getOffset() > completionOffset)
				return false;
			if (leaf.isHidden()) {
				if (result.length() != 0)
					hiddens.add((ILeafNode) child);
			}
			else {
				Iterator<ILeafNode> iter = hiddens.iterator();
				while (iter.hasNext()) {
					result.append(iter.next().getText());
				}
				hiddens.clear();
				result.append(getNodeTextUpToCompletionOffset(leaf));
				if (leafRegion.getOffset() + leafRegion.getLength() > completionOffset)
					return false;
			}
		}
	}
	return true;
}