Search in sources :

Example 81 with ICompositeNode

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;
}
Also used : CompositeNode(org.eclipse.xtext.nodemodel.impl.CompositeNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 82 with ICompositeNode

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);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ITokenStreamExtension(org.eclipse.xtext.parsetree.reconstr.ITokenStreamExtension)

Example 83 with ICompositeNode

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;
    }
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 84 with ICompositeNode

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);
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 85 with ICompositeNode

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();
    }
}
Also used : Action(org.eclipse.xtext.Action) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Aggregations

ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)263 Test (org.junit.Test)87 INode (org.eclipse.xtext.nodemodel.INode)79 EObject (org.eclipse.emf.ecore.EObject)70 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)57 XtextResource (org.eclipse.xtext.resource.XtextResource)41 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)24 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)20 Resource (org.eclipse.emf.ecore.resource.Resource)19 Model (org.eclipse.xtext.valueconverter.bug250313.Model)15 IParseResult (org.eclipse.xtext.parser.IParseResult)14 ParserRule (org.eclipse.xtext.ParserRule)12 ITextRegion (org.eclipse.xtext.util.ITextRegion)12 RuleCall (org.eclipse.xtext.RuleCall)11 CrossReference (org.eclipse.xtext.CrossReference)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)8 AbstractRule (org.eclipse.xtext.AbstractRule)8 Keyword (org.eclipse.xtext.Keyword)8