use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class SyntheticLinkingLeafNode method getParent.
/**
* @since 2.10
*/
protected CompositeNode getParent(EObject obj, EReference eRef, String crossRefString, int offset, int length) {
ICompositeNode node = NodeModelUtils.getNode(obj);
if (node != null) {
ICompositeNode rootNode = node.getRootNode();
if (rootNode instanceof CompositeNode)
return (CompositeNode) rootNode;
}
Resource resource = obj.eResource();
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult != null) {
ICompositeNode rootNode = parseResult.getRootNode();
if (rootNode instanceof CompositeNode)
return (CompositeNode) rootNode;
}
}
return null;
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class NodeModelStreamer method feedTokenStream.
@Override
public ITextRegion feedTokenStream(ITokenStream out, ICompositeNode in, int offset, int length) throws IOException {
List<INode> nodes = getLeafs(in, offset, offset + length);
if (nodes.isEmpty())
return new TextRegion(in.getOffset(), 0);
if (out instanceof ITokenStreamExtension)
((ITokenStreamExtension) out).init(findRootRuleForRegion(nodes.get(0)));
boolean lastIsTokenOrComment = false;
for (INode node : nodes) {
boolean currentIsTokenOrComment = tokenUtil.isCommentNode(node) || tokenUtil.isToken(node);
if (lastIsTokenOrComment && currentIsTokenOrComment)
writeHiddenEmpty(out);
lastIsTokenOrComment = currentIsTokenOrComment;
if (node instanceof ILeafNode) {
ILeafNode leaf = (ILeafNode) node;
if (leaf.isHidden())
writeHidden(out, leaf);
else
writeSemantic(out, leaf);
} else if (node instanceof ICompositeNode)
writeSemantic(out, (ICompositeNode) node);
}
out.flush();
int rStart = nodes.get(0).getOffset();
int rLength = nodes.get(nodes.size() - 1).getEndOffset() - rStart;
return new TextRegion(rStart, rLength);
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class NodeModelBasedRegionAccessBuilder method add.
protected void add(NodeModelBasedRegionAccess access, INode node) {
if (node instanceof ILeafNode && ((ILeafNode) node).isHidden()) {
ILeafNode leafNode = (ILeafNode) node;
lastHidden.addPart(createHidden(lastHidden, leafNode));
} else if (node instanceof ICompositeNode || node.getLength() > 0) {
NodeEObjectRegion eObjectTokens = stack.peek();
NodeSemanticRegion newSemantic = createSemanticRegion(access, node);
NodeHiddenRegion newHidden = createHiddenRegion(access);
newSemantic.setTrailingHiddenRegion(newHidden);
newHidden.setPrevious(newSemantic);
newSemantic.setLeadingHiddenRegion(lastHidden);
lastHidden.setNext(newSemantic);
eObjectTokens.addChild(newSemantic);
newSemantic.setEObjectTokens(eObjectTokens);
lastHidden = newHidden;
}
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class NodeModelBasedRegionAccessBuilder method getEObjectToTokensMap.
protected Map<EObject, AbstractEObjectRegion> getEObjectToTokensMap(ITextRegionAccess regionAccess) {
this.eObjToTokens = Maps.newHashMap();
this.firstHidden = createHiddenRegion(regionAccess);
this.lastHidden = this.firstHidden;
NodeModelBasedRegionAccess access = (NodeModelBasedRegionAccess) regionAccess;
ICompositeNode rootNode = resource.getParseResult().getRootNode();
process(rootNode, access);
return ImmutableMap.<EObject, AbstractEObjectRegion>copyOf(this.eObjToTokens);
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class NodeModelBasedRegionAccessBuilder method process.
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();
}
}
Aggregations