Java Code Examples for org.eclipse.xtext.nodemodel.ICompositeNode#getChildren()

The following examples show how to use org.eclipse.xtext.nodemodel.ICompositeNode#getChildren() . 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: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkNoJavaStyleTypeCasting(XBlockExpression blockExpression) {
	if(isIgnored(JAVA_STYLE_TYPE_CAST)) {
		return;
	}
	if (blockExpression.getExpressions().size() <= 1) {
		return;
	}
	ICompositeNode node = NodeModelUtils.getNode(blockExpression);
	if (node == null) {
		return;
	}
	INode expressionNode = null;
	for (INode child : node.getChildren()) {
		if (isSemicolon(child)) {
			expressionNode = null;
		} else if (isXExpressionInsideBlock(child)) {
			if (expressionNode != null) {
				checkNoJavaStyleTypeCasting(expressionNode);
			}
			expressionNode = child;
		}
	}
}
 
Example 2
Source File: XbaseStratumBreakpointSupport.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isValidLineForBreakpoint(ICompositeNode node, int line) {
	for (INode n : node.getChildren()) {
		ITextRegionWithLineInformation textRegion = n.getTextRegionWithLineInformation();
		if (textRegion.getLineNumber()<= line && textRegion.getEndLineNumber() >= line) {
			EObject eObject = n.getSemanticElement();
			if (eObject instanceof XExpression && !(eObject.eClass() == XbasePackage.Literals.XBLOCK_EXPRESSION)) {
				return true;
			}
			if (n instanceof ICompositeNode && isValidLineForBreakpoint((ICompositeNode) n, line)) {
				return true;
			}
		}
		if (textRegion.getLineNumber() > line) {
			return false;
		}
	}
	return false;
}
 
Example 3
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ILocationData getLocationWithoutTypeArguments(XAbstractFeatureCall call) {
	final ICompositeNode startNode = NodeModelUtils.getNode(call);
	if (startNode != null) {
		List<INode> resultNodes = Lists.newArrayList();
		if (call instanceof XFeatureCall || call instanceof XMemberFeatureCall) {
			boolean featureReferenceSeen = false;
			for (INode child : startNode.getChildren()) {
				if (featureReferenceSeen) {
					resultNodes.add(child);
				} else {
					EObject grammarElement = child.getGrammarElement();
					if (grammarElement instanceof CrossReference) {
						Assignment assignment = GrammarUtil.containingAssignment(grammarElement);
						if (assignment != null && "feature".equals(assignment.getFeature())) {
							featureReferenceSeen = true;
							resultNodes.add(child);
						}
					}
				}
			}
		}
		return toLocationData(resultNodes);
	}
	return null;
}
 
Example 4
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ILocationData getLocationOfDefault(XSwitchExpression expression) {
	final ICompositeNode startNode = NodeModelUtils.getNode(expression);
	if (startNode != null) {
		List<INode> resultNodes = Lists.newArrayList();
		boolean defaultSeen = false;
		for (INode child : startNode.getChildren()) {
			if (defaultSeen) {
				resultNodes.add(child);
				if (GrammarUtil.containingAssignment(child.getGrammarElement()) != null) {
					break;
				}
			} else if (child.getGrammarElement() instanceof Keyword && "default".equals(child.getText())) {
				defaultSeen = true;
				resultNodes.add(child);
			}
		}
		return toLocationData(resultNodes);
	}
	return null;
}
 
Example 5
Source File: DefaultLocationInFileProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the smallest node that covers all assigned values of the given object. It handles the semantics of {@link Action
 * actions} and {@link RuleCall unassigned rule calls}.
 * 
 * @return the minimal node that covers all assigned values of the given object.
 * @since 2.3
 */
protected ICompositeNode findNodeFor(EObject semanticObject) {
	ICompositeNode result = NodeModelUtils.getNode(semanticObject);
	if (result != null) {
		ICompositeNode node = result;
		while (GrammarUtil.containingAssignment(node.getGrammarElement()) == null && node.getParent() != null && !node.getParent().hasDirectSemanticElement()) {
			ICompositeNode parent = node.getParent();
			if (node.hasSiblings()) {
				for(INode sibling: parent.getChildren()) {
					EObject grammarElement = sibling.getGrammarElement();
					if (grammarElement != null && GrammarUtil.containingAssignment(grammarElement) != null) {
						result = parent;
					}
				}
			}
			node = parent;
		}
	}
	return result;
}
 
Example 6
Source File: XbaseLocationInFileProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected List<INode> getLocationNodes(EObject obj) {
	if (obj instanceof XMemberFeatureCall || obj instanceof XAssignment || obj instanceof XFeatureCall) {
		List<INode> resultNodes = Lists.newArrayList();
		final ICompositeNode startNode = findNodeFor(obj);
		boolean crossRefConsumed = false;
		for (INode child : startNode.getChildren()) {
			if (crossRefConsumed) {
				resultNodes.add(child);
			} else {
				EObject grammarElement = child.getGrammarElement();
				if (grammarElement instanceof CrossReference) {
					// We don't use the grammar access to be more robust against
					// overwriting grammars
					Assignment assignment = GrammarUtil.containingAssignment(grammarElement);
					if (XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE.getName().equals(assignment.getFeature())) {
						crossRefConsumed = true;
						resultNodes.add(child);
					}
				}
			} 
		}
		if (!resultNodes.isEmpty())
			return resultNodes;
	}
	return super.getLocationNodes(obj);
}
 
Example 7
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ILocationData getLocationWithNewKeyword(XConstructorCall call) {
	final ICompositeNode startNode = NodeModelUtils.getNode(call);
	if (startNode != null) {
		List<INode> resultNodes = Lists.newArrayList();
		for (INode child : startNode.getChildren()) {
			if (child.getGrammarElement() instanceof Keyword && "(".equals(child.getText()))
				break;
			resultNodes.add(child);
		}
		return toLocationData(resultNodes);
	}
	return null;
}
 
Example 8
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 9
Source File: DefaultLocationInFileProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected List<INode> getLocationNodes(EObject obj) {
	final EStructuralFeature nameFeature = getIdentifierFeature(obj);
	if (nameFeature != null) {
		List<INode> result = NodeModelUtils.findNodesForFeature(obj, nameFeature);
		if (!result.isEmpty())
			return result;
	}

	List<INode> resultNodes = Lists.newArrayList();
	final ICompositeNode startNode = findNodeFor(obj);
	INode keywordNode = null;
	// use LeafNodes instead of children?
	for (INode child : startNode.getChildren()) {
		EObject grammarElement = child.getGrammarElement();
		if (grammarElement instanceof RuleCall) {
			RuleCall ruleCall = (RuleCall) grammarElement;
			String ruleName = ruleCall.getRule().getName();
			if (ruleName.equals("ID")) {
				resultNodes.add(child);
			}
		} else if (grammarElement instanceof Keyword) {
			// TODO use only keywords, that aren't symbols like '=' ?
			if (keywordNode == null && useKeyword((Keyword) grammarElement, obj)) {
				keywordNode = child;
			}
		}
	}
	if (resultNodes.isEmpty() && keywordNode != null)
		resultNodes.add(keywordNode);
	return resultNodes;
}
 
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;
}
 
Example 11
Source File: BaseContentAssistParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean appendTextToParse(ICompositeNode node, int offset, boolean skipOptional, StringBuilder result) {
	for (INode child : node.getChildren()) {
		if (child instanceof ILeafNode) {
			String text = child.getText();
			if (child.getTotalEndOffset() >= offset) {
				String sub = text.substring(0, offset - child.getTotalOffset());
				result.append(sub);
				return true;
			} else {
				result.append(text);
			}
		} else {
			if (!skipOptional) {
				if (appendTextToParse((ICompositeNode) child, offset, child.getTotalEndOffset() < offset, result)) {
					return true;
				}
			} else {
				String skippedAs = getReplacement((ICompositeNode) child);
				if (skippedAs != null) {
					result.append(skippedAs);
				} else {
					if (appendTextToParse((ICompositeNode) child, offset, true, result)) {
						return true;
					}
				}
			}
		}
	}
	return false;
}
 
Example 12
Source File: AbstractContentAssistUiTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the total offset for given token.
 *
 * @param parserNode
 *          the parser node
 * @param name
 *          the name
 * @return the total offset for given token
 */
protected int getTotalOffsetForToken(final ICompositeNode parserNode, final String name) {
  int result = NO_OFFSET_FOUND;
  for (final INode n : parserNode.getChildren()) {
    if (n instanceof ILeafNode && name.equals(((ILeafNode) n).getText())) {
      result = ((ILeafNode) n).getTotalOffset();
    } else if (n instanceof ICompositeNode) {
      result = getTotalOffsetForToken((ICompositeNode) n, name);
    }
  }
  return result;
}