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

The following examples show how to use org.eclipse.xtext.nodemodel.INode#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: ExpressionUtil.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @return whether the given selection is zero length
 */
protected boolean isBeginOfExpression(INode node) {
	ITextRegion textRegion = node.getTextRegion();
	if (textRegion.getLength() == 0)
		return false;
	char firstChar = node.getText().charAt(0);
	return Character.isLetterOrDigit(firstChar)
			|| firstChar == '\''
			|| firstChar == '"'
			|| firstChar == '['
			|| firstChar == '('
			|| firstChar == '{'
			|| firstChar == '#'
			|| firstChar == '@'
			;
}
 
Example 3
Source File: XtendProposalProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void completeInRichString(EObject model, RuleCall ruleCall, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	INode node = context.getCurrentNode();
	ITextRegion textRegion = node.getTextRegion();
	int offset = textRegion.getOffset();
	int length = textRegion.getLength();
	String currentNodeText = node.getText();
	if (currentNodeText.startsWith("\u00BB") && offset + 1 <= context.getOffset()
			|| currentNodeText.startsWith("'''") && offset + 3 <= context.getOffset()) {
		if (context.getOffset() > offset && context.getOffset() < offset + length)
			addGuillemotsProposal(context, acceptor);
	} else if (currentNodeText.startsWith("\u00AB\u00AB")) {
		try {
			IDocument document = context.getViewer().getDocument();
			int nodeLine = document.getLineOfOffset(offset);
			int completionLine = document.getLineOfOffset(context.getOffset());
			if (completionLine > nodeLine) {
				addGuillemotsProposal(context, acceptor);
			}
		} catch (BadLocationException e) {
			// ignore
		}
	}
}
 
Example 4
Source File: XtendJavaDocProposalFactory.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected int getReplacementContextLength(ContentAssistContext context) {
	int replacementOffset = context.getReplaceRegion().getOffset();
	INode currentNode = context.getCurrentNode();
	ITextRegion currentRegion = currentNode.getTextRegion();
	String text = currentNode.getText();
	int index = text.indexOf('}', replacementOffset - currentRegion.getOffset());
	if (index == -1) {
		index = text.indexOf('\n', replacementOffset - currentRegion.getOffset());
	}
	if (index != -1) {
		int indexFromStart = index + currentNode.getOffset();
		return indexFromStart - replacementOffset;
	} else {
		return context.getReplaceContextLength();
	}
}
 
Example 5
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 6
Source File: ImportSectionRegionUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public ITextRegion computeRegion(XtextResource resource) {
	XImportSection xImportSection = config.getImportSection(resource);
	// try to obtain the region from the text if it is not a synthetic region.
	if (xImportSection != null && xImportSection.eResource() != null) {
		INode node = NodeModelUtils.findActualNodeFor(xImportSection);
		if(node == null) 
			LOG.error("Cannot detect node for original import section");
		else 
			return node.getTextRegion();
	} 
	return new TextRegion(config.getImportSectionOffset(resource), 0);
}
 
Example 7
Source File: AstSelectionProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected ITextRegion computeInitialFineGrainedSelection(INode node, ITextRegion currentEditorSelection) {
	if (node.getText().trim().length() > 0) {
		ITextRegion textRegion = node.getTextRegion();
		if (currentEditorSelection.getOffset() >= textRegion.getOffset()
			&& getEndOffset(currentEditorSelection) < textRegion.getOffset() + textRegion.getLength()) {
			//TODO enhance to just select a single word in a comment or string literal.
			return textRegion;
		}
	}
	return null;
}
 
Example 8
Source File: XbaseHyperLinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private ITextRegion getTextRegion(final XImportDeclaration it, final int offset) {
	final List<INode> nodes = NodeModelUtils.findNodesForFeature(it,
			XtypePackage.Literals.XIMPORT_DECLARATION__MEMBER_NAME);
	for (final INode node : nodes) {
		final ITextRegion textRegion = node.getTextRegion();
		if (textRegion.contains(offset)) {
			return textRegion;
		}
	}
	return null;
}
 
Example 9
Source File: ValidatingRichStringAcceptor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void resetCurrentOffset(/* @Nullable */ RichStringLiteral origin) {
	if (root == null)
		root = origin;
	if (origin != null && origin != recent) {
		if (wasCurrentOffset == -1)
			wasCurrentOffset = currentOffset;
		int diff = lastOffsetOfLiteral - currentOffset;
		// no actions are involved, we are interested in the real node
		recent = origin;
		List<INode> featureNodes = NodeModelUtils.findNodesForFeature(origin,
				XbasePackage.Literals.XSTRING_LITERAL__VALUE);
		if (featureNodes.size() == 1) {
			INode node = featureNodes.get(0);
			ITextRegion textRegion = node.getTextRegion();
			currentOffset = textRegion.getOffset();
			String nodeText = node.getText();
			if (nodeText.endsWith("'''")) {
				lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 3;
			} else if (nodeText.endsWith("''")) {
				lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 2;
			} else if (nodeText.endsWith("'") || nodeText.endsWith("\u00AB")) {
				lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 1;
			}
			if (nodeText.charAt(0) == '\'') {
				currentOffset += 3;
			} else {
				currentOffset += 1;
			}
		}
		currentOffset -= diff;
	}
}
 
Example 10
Source File: IndentationAwareCompletionPrefixProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected INode getLastCompleteNodeByOffset(INode node, int offset) {
	BidiTreeIterator<INode> iterator = node.getRootNode().getAsTreeIterable().iterator();
	INode result = node;
	ITextRegion candidateTextRegion = node.getTextRegion();
	while (iterator.hasNext()) {
		INode candidate = iterator.next();
		ITextRegion textRegion = candidate.getTextRegion();
		if (textRegion.getOffset() >= offset && !(textRegion.getOffset() == offset && textRegion.getLength() == 0)) {
			if (!candidateTextRegion.equals(textRegion) && candidate instanceof ILeafNode && textRegion.getLength() + textRegion.getOffset() >= offset) {
				break;
			}
		} 
		if ((candidate instanceof ILeafNode) &&
				   (candidate.getGrammarElement() == null ||
						   candidate.getGrammarElement() instanceof AbstractElement ||
						   candidate.getGrammarElement() instanceof ParserRule)) {
			if (textRegion.getLength() == 0) {
				if (candidateTextRegion.getOffset() + candidateTextRegion.getLength() < offset || candidateTextRegion.getLength() == 0 && candidateTextRegion.getOffset() <= offset) {
					result = candidate;
					candidateTextRegion = candidate.getTextRegion();
				}
			} else {
				result = candidate;
				candidateTextRegion = candidate.getTextRegion();
			}
		}
	}
	return result;
}
 
Example 11
Source File: HyperlinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Produces hyperlinks for the given {@code node} which is associated with a cross reference
 * that points to the referenced {@code target}.
 * 
 * @see #createHyperlinksTo(XtextResource, Region, EObject, IHyperlinkAcceptor)
 * 
 * @since 2.4
 */
protected void createHyperlinksTo(XtextResource resource, INode node, EObject target, IHyperlinkAcceptor acceptor) {
	ITextRegion textRegion = node.getTextRegion();
	Region region = new Region(textRegion.getOffset(), textRegion.getLength());
	createHyperlinksTo(resource, region, target, acceptor);
}