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

The following examples show how to use org.eclipse.xtext.nodemodel.INode#hasDirectSemanticElement() . 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: SemanticNodeIterator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isEObjectNode(INode node) {
	if (node.getGrammarElement() instanceof AbstractRule)
		return true;
	if (node.getGrammarElement() instanceof Action)
		return true;
	if (GrammarUtil.isAssignedEObjectRuleCall(node.getGrammarElement())) {
		if (node.hasDirectSemanticElement())
			return true;
		AbstractRule rule = ((RuleCall) node.getGrammarElement()).getRule();
		node = node.getParent();
		while (node != null) {
			if (GrammarUtil.isAssigned(node.getGrammarElement()))
				return true;
			if (node.getGrammarElement() instanceof Action
					&& GrammarUtil.containingRule(node.getGrammarElement()) == rule)
				return false;
			node = node.getParent();
		}
		return true;
	}
	return false;
}
 
Example 2
Source File: DefaultQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private CrossReference findCrossReference(EObject context, INode node) {
	if (node == null || (node.hasDirectSemanticElement() && context.equals(node.getSemanticElement())))
		return null;

	EObject grammarElement = node.getGrammarElement();
	if (grammarElement instanceof CrossReference) {
		return (CrossReference) grammarElement;
	} else
		return findCrossReference(context, node.getParent());
}
 
Example 3
Source File: AbstractParseTreeConstructor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean assignTokenDirect(INode node, Map<EObject, AbstractToken> eObject2Token) {
	if (!node.hasDirectSemanticElement())
		return true;
	AbstractToken token = eObject2Token.get(node.getSemanticElement());
	if (token != null && token.getNode() == null) {
		token.setNode(node);
		return true;
	}
	return false;
}
 
Example 4
Source File: TokenUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public EObject getTokenOwner(INode node) {
	if (node.hasDirectSemanticElement())
		return node.getSemanticElement();
	if (node.getParent() != null) {
		if (node.getParent().hasDirectSemanticElement())
			return node.getParent().getSemanticElement();
		EObject parentGrammarElement = node.getParent().getGrammarElement();
		boolean isParser = GrammarUtil.isEObjectRule(parentGrammarElement) || GrammarUtil.isEObjectRuleCall(parentGrammarElement);
		for (INode sibling : node.getParent().getChildren())
			if (sibling.hasDirectSemanticElement() && (isParser || sibling.getGrammarElement() instanceof Action))
				return sibling.getSemanticElement();
	}
	return node.getSemanticElement();
}
 
Example 5
Source File: DefaultCommentAssociater.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected EObject getEObjectForRemainingComments(ICompositeNode rootNode) {
	TreeIterator<INode> i = rootNode.getAsTreeIterable().iterator();
	while (i.hasNext()) {
		INode o = i.next();
		if (o.hasDirectSemanticElement())
			return o.getSemanticElement();
	}
	return null;
}
 
Example 6
Source File: SemanticNodeIterator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected EObject getEObjectNodeEObject(INode node) {
	if (node.hasDirectSemanticElement())
		return node.getSemanticElement();
	Iterator<INode> i = node.getAsTreeIterable().iterator();
	while (i.hasNext()) {
		INode n = i.next();
		if (n.hasDirectSemanticElement())
			return n.getSemanticElement();
	}
	return null;
}
 
Example 7
Source File: NodeModelBasedRegionAccessBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected EObject findGrammarElement(INode node, EObject obj) {
	INode current = node;
	String feature = obj.eContainingFeature().getName();
	while (current != null) {
		EObject grammarElement = current.getGrammarElement();
		Assignment assignment = GrammarUtil.containingAssignment(grammarElement);
		if (assignment != null && feature.equals(assignment.getFeature()))
			return grammarElement;
		if (grammarElement instanceof Action) {
			Action action = (Action) grammarElement;
			if (feature.equals(action.getFeature()))
				return grammarElement;
			else if (current == node && current instanceof ICompositeNode) {
				INode child = ((ICompositeNode) current).getFirstChild();
				while (child instanceof ICompositeNode) {
					EObject grammarElement2 = child.getGrammarElement();
					Assignment assignment2 = GrammarUtil.containingAssignment(grammarElement2);
					if (assignment2 != null && feature.equals(assignment2.getFeature()))
						return grammarElement2;
					// if (child.hasDirectSemanticElement() && child.getSemanticElement() != obj)
					// break;
					child = ((ICompositeNode) child).getFirstChild();
				}
			}
		}
		if (current.hasDirectSemanticElement() && current.getSemanticElement() != obj)
			return null;
		current = current.getParent();
	}
	return null;
}
 
Example 8
Source File: NodeModelStreamer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.0
 */
protected ParserRule findRootRuleForRegion(INode node) {
	if (GrammarUtil.isEObjectRule(node.getGrammarElement()))
		return (ParserRule) node.getGrammarElement();
	if (node.hasDirectSemanticElement())
		return GrammarUtil.containingParserRule(node.getGrammarElement());
	if (node.getParent() != null)
		return findRootRuleForRegion(node.getParent());
	return null;
}
 
Example 9
Source File: NodeModelBasedRegionAccessBuilder.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void process(INode node, NodeModelBasedRegionAccess access) {
	NodeEObjectRegion tokens = stack.peek();
	boolean creator = isEObjectRoot(node);
	if (creator || tokens == null) {
		tokens = createTokens(access, node);
		tokens.setLeadingHiddenRegion(lastHidden);
		NodeEObjectRegion parent = stack.peek();
		if (parent != null) {
			parent.addChild(tokens);
		}
		stack.push(tokens);
	}
	if (tokens.getSemanticElement() == null) {
		if (node.getParent() == null) {
			tokens.setSemanticElement(resource.getContents().get(0));
			EObject element = node.getGrammarElement();
			if (element instanceof Action)
				element = ((ICompositeNode) node).getFirstChild().getGrammarElement();
			tokens.setGrammarElement(element);
		} else if (node.hasDirectSemanticElement()) {
			tokens.setSemanticElement(node.getSemanticElement());
			tokens.setGrammarElement(findGrammarElement(node, tokens.getSemanticElement()));
		}
	}
	if (include(node)) {
		if (node instanceof ICompositeNode) {
			for (ILeafNode leaf : node.getLeafNodes())
				if (leaf.isHidden())
					this.add(access, leaf);
				else
					break;
		}
		this.add(access, node);
	} else if (node instanceof ICompositeNode) {
		for (INode child : ((ICompositeNode) node).getChildren())
			process(child, access);
	}
	if (creator) {
		NodeEObjectRegion popped = stack.pop();
		popped.setTrailingHiddenRegion(lastHidden);
		EObject semanticElement = popped.getSemanticElement();
		if (semanticElement == null)
			throw new IllegalStateException();
		if (!stack.isEmpty() && semanticElement.eContainer() != stack.peek().getSemanticElement())
			throw new IllegalStateException();
		EObject grammarElement = popped.getGrammarElement();
		if (grammarElement == null) {
			throw new IllegalStateException();
		}
		NodeEObjectRegion old = eObjToTokens.put(semanticElement, popped);
		if (old != null)
			throw new IllegalStateException();
	}
}