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

The following examples show how to use org.eclipse.xtext.nodemodel.INode#getParent() . 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: LazyURIEncoder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * ONLY public to be testable
 * 
 * @noreference This method is not intended to be referenced by clients.
 */
public void getRelativePath(StringBuilder result, INode parserNode, INode node) {
	if (parserNode == node)
		return;
	if (isAncestor(parserNode, node)) {
		ICompositeNode parent = node.getParent();
		getRelativePath(result, parserNode, parent);
		int idx = 0;
		INode child = parent.getFirstChild();
		while(child != node && child.hasNextSibling()) {
			idx++;
			child = child.getNextSibling();
		}
		result.append("/").append(idx);
	} else {
		result.append("/..");
		getRelativePath(result, parserNode.getParent(), node);
	}
}
 
Example 2
Source File: ExtendedFormattingConfigBasedStream.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns predecessor of the given node in the parse tree.
 * Copied method from: org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator
 *
 * @param node
 *          for which predecessor should be found
 * @return predecessor
 */
private INode findPrevious(final INode node) {
  ICompositeNode parent = node.getParent();
  if (parent == null) {
    return null;
  }
  INode predecessor = node.getPreviousSibling();
  if (predecessor != null) {
    while (predecessor instanceof ICompositeNode) {
      INode lastChild = ((ICompositeNode) predecessor).getLastChild();
      if (lastChild == null) {
        return predecessor;
      }
      predecessor = lastChild;
    }
    return predecessor;
  }
  return parent;
}
 
Example 3
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 4
Source File: LazyURIEncoder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * ONLY public to be testable
 */
public INode getNode(final INode node, String path) {
	final List<String> split = Strings.split(path, '/');
	INode result = node;
	for (String string : split) {
		String trimmed = string.trim();
		if (trimmed.length() > 0) {
			if ("..".equals(trimmed)) {
				if (result.getParent() == null)
					throw new IllegalStateException("node has no parent");
				result = result.getParent();
			} else {
				int index = Integer.parseInt(string);
				if (index >= 0) {
					INode child = ((ICompositeNode) result).getFirstChild();
					while(index > 0) {
						child = child.getNextSibling();
						index--;
					}
					result = child;
				}
			}
		}
	}
	return result;
}
 
Example 5
Source File: NodeIterator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private INode findPrevious(INode node) {
	ICompositeNode parent = node.getParent();
	if (parent == null) {
		return null;
	}
	INode predecessor = node.getPreviousSibling();
	if (predecessor != null) {
		while (predecessor instanceof ICompositeNode && !prunedComposites.contains(predecessor)) {
			INode lastChild = ((ICompositeNode) predecessor).getLastChild();
			if (lastChild == null) {
				return predecessor;
			}
			predecessor = lastChild;
		}
		return predecessor;
	}
	return parent;
}
 
Example 6
Source File: AbstractCleaningLinker.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @return true, if the parent node could contain cross references to the same semantic element as the given node.
 */
protected boolean shouldCheckParentNode(INode node) {
	EObject grammarElement = node.getGrammarElement();
	if (grammarElement instanceof AbstractElement) {
		ICompositeNode parent = node.getParent();
		if (parent != null) {
			if (!parent.hasDirectSemanticElement()) {
				if (isContainedInFragmentRule(grammarElement)) {
					return false;
				}
				Assignment assignment = GrammarUtil.containingAssignment(grammarElement);
				if (assignment == null) {
					return true;
				}
			}
			if (grammarElement instanceof Action) {
				if (isContainedInFragmentRule(grammarElement)) {
					return parent.getGrammarElement() instanceof RuleCall;
				}
			}
		}
	}
	return false;
}
 
Example 7
Source File: NodeIterator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected INode findNextSibling(INode node) {
	ICompositeNode parent = node.getParent();
	if (parent == null) {
		return null;
	}
	INode successor = node.getNextSibling();
	if (successor != null) {
		return successor;
	}
	return findNextSibling(parent);
}
 
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: SemanticNodeIterator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected INode next(INode node, boolean prune) {
	if (!prune && node instanceof ICompositeNode) {
		INode child = ((ICompositeNode) node).getFirstChild();
		if (child != null)
			return child;
	}
	INode n = node.getNextSibling();
	while (n == null) {
		node = node.getParent();
		if (node == null || isEObjectNode(node))
			return null;
		n = node.getNextSibling();
	}
	return n;
}
 
Example 10
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 11
Source File: AbstractParseTreeConstructor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean containsNodeOrAnyParent(Set<ICompositeNode> nodes, INode node) {
	if (nodes.contains(node))
		return true;
	if (node.getParent() != null)
		return containsNodeOrAnyParent(nodes, node.getParent());
	return false;
}
 
Example 12
Source File: AbstractParseTreeConstructor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void assignNodesByMatching(Map<EObject, AbstractToken> eObject2Token, ICompositeNode rootNode,
		Map<ILeafNode, EObject> comments) throws IOException {
	NodeIterator contents = new NodeIterator(rootNode);
	while (contents.hasNext()) {
		INode containedNode = contents.next();
		AbstractRule rule = containedNode.getGrammarElement() instanceof AbstractRule ? (AbstractRule) containedNode
				.getGrammarElement() : null;
		if (hiddenTokenHelper.isWhitespace(rule))
			continue;
		else if (containedNode instanceof ILeafNode && hiddenTokenHelper.isComment(rule))
			assignComment((ILeafNode) containedNode, eObject2Token, comments);
		else if (tokenUtil.isToken(containedNode)) {
			Pair<List<ILeafNode>, List<ILeafNode>> leadingAndTrailingHiddenTokens = tokenUtil
					.getLeadingAndTrailingHiddenTokens(containedNode);
			for (ILeafNode leadingHiddenNode : leadingAndTrailingHiddenTokens.getFirst()) {
				if (tokenUtil.isCommentNode(leadingHiddenNode)) {
					assignComment(leadingHiddenNode, eObject2Token, comments);
				}
			}
			assignTokenByMatcher(containedNode, eObject2Token);
			for (ILeafNode trailingHiddenNode : leadingAndTrailingHiddenTokens.getSecond()) {
				if (tokenUtil.isCommentNode(trailingHiddenNode)) {
					assignComment(trailingHiddenNode, eObject2Token, comments);
				}
			}
			contents.prune();
			ICompositeNode parentNode = containedNode.getParent();
			while (parentNode != null && assignTokenDirect(parentNode, eObject2Token))
				parentNode = parentNode.getParent();
			if (containedNode.getOffset() > rootNode.getEndOffset()) {
				break;
			}
		}
	}
}
 
Example 13
Source File: FastLazyURIEncoder.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public INode getNode(final INode node, final String path) {
  INode result = node;
  List<String> segments = Strings.split(path, '/');
  for (int i = 0; i < segments.size(); i++) {
    String seg = segments.get(i);
    if (seg.length() > 0) {
      if (PARENT_SEG.equals(seg)) {
        if (result.getParent() == null) {
          throw new IllegalStateException("node has no parent"); //$NON-NLS-1$
        }
        result = result.getParent();
      } else {
        int index = Integer.parseInt(seg);
        if (index >= 0) {
          INode child = ((ICompositeNode) result).getFirstChild();
          while (index > 0) {
            child = child.getNextSibling();
            index--;
          }
          result = child;
        }
      }
    }
  }
  return result;
}
 
Example 14
Source File: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isParentOf(INode node, INode child) {
	if (node == null)
		return false;
	while(child != null && node.equals(child)) {
		child = child.getParent();
	}
	return node.equals(child);
}
 
Example 15
Source File: ParserBasedContentAssistContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public INode getContainingDatatypeRuleNode(INode node) {
	INode result = node;
	EObject grammarElement = result.getGrammarElement();
	if (grammarElement != null) {
		ParserRule parserRule = GrammarUtil.containingParserRule(grammarElement);
		while (parserRule != null && GrammarUtil.isDatatypeRule(parserRule)) {
			result = result.getParent();
			grammarElement = result.getGrammarElement();
			parserRule = GrammarUtil.containingParserRule(grammarElement);
		}
	}
	return result;
}
 
Example 16
Source File: SARLDiagnosticLabelDecorator.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static boolean inside(INode node, ICompositeNode parentCandidate) {
	INode current = node;
	while (current != null) {
		if (current.equals(parentCandidate)) {
			return true;
		}
		current = current.getParent();
	}
	return false;
}
 
Example 17
Source File: ReferenceResolutionFinder.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private EObject getParentImportElement(INode parseTreeNode) {
	while (parseTreeNode != null) {
		EObject semanticElement = parseTreeNode.getSemanticElement();
		if (semanticElement instanceof ImportCallExpression
				|| semanticElement instanceof ImportDeclaration) {

			return semanticElement;
		}
		parseTreeNode = parseTreeNode.getParent();
	}
	return null;
}
 
Example 18
Source File: ExpressionUtil.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected INode nextNodeForFindSelectedExpression(EObject element, INode node, ITextSelection selection) {
	return node.getParent();
}
 
Example 19
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();
	}
}
 
Example 20
Source File: NodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Compute the line and column information at the given offset from any node that belongs the the document. The line is one-based, e.g.
 * the first line has the line number '1'. The line break belongs the line that it breaks. In other words, the first line break in the
 * document also has the line number '1'. The column number starts at '1', too. In effect, the document offset '0' will always return
 * line '1' and column '1'.
 * 
 * If the given documentOffset points exactly to {@code anyNode.root.text.length}, it's assumed to be a virtual character thus
 * the offset is valid and the column and line information is returned as if it was there.
 * 
 * This contract is in sync with {@link org.eclipse.emf.ecore.resource.Resource.Diagnostic}.
 * 
 * @throws IndexOutOfBoundsException
 *             if the document offset does not belong to the document, 
 *             {@code documentOffset < 0 || documentOffset > anyNode.rootNode.text.length}
 */
public static LineAndColumn getLineAndColumn(INode anyNode, int documentOffset) {
	// special treatment for inconsistent nodes such as SyntheticLinkingLeafNode
	if (anyNode.getParent() == null && !(anyNode instanceof RootNode)) {
		return LineAndColumn.from(1,1);
	}
	return InternalNodeModelUtils.getLineAndColumn(anyNode, documentOffset);
}