Search in sources :

Example 1 with AnnexTextPositionResolver

use of org.osate.annexsupport.AnnexTextPositionResolver in project osate2 by osate.

the class Aadl2EObjectAtOffsetHelper method resolveCrossReferencedElementAt.

/**
 * @return the cross referenced EObject under, right or left to the cursor (in that order) or
 *         <code>null</code> if there is no cross referenced object next to the offset.
 */
@Override
public EObject resolveCrossReferencedElementAt(XtextResource resource, int offset) {
    INode annexLeaf = AnnexUtil.findAnnexLeafNode(resource, offset);
    if (annexLeaf != null) {
        if (textpositionresolverregistry == null) {
            initTextPositionResolverRegistry();
        }
        if (textpositionresolverregistry != null) {
            EObject obj = NodeModelUtils.findActualSemanticObjectFor(annexLeaf);
            if (obj instanceof NamedElement) {
                String annexName = ((NamedElement) obj).getName();
                // find the actual subclause or library instead of the default one found from the NodeModel
                if (obj instanceof AnnexSubclause || obj instanceof AnnexLibrary) {
                    AnnexTextPositionResolver atpr = textpositionresolverregistry.getTextPositionResolver(annexName);
                    EObject actualAnnexElement = AnnexUtil.getParsedAnnex(obj);
                    if (atpr != null && actualAnnexElement != null) {
                        TextPositionInfo tpo = atpr.resolveCrossReferencedElementAt(actualAnnexElement, offset);
                        return tpo.getModelObject();
                    }
                }
            }
        }
        // now try xtext based annexes via adapted ParseResult
        annexLeaf = getLeafInParsedAnnex((ILeafNode) annexLeaf, offset);
        annexLeaf = findCrossReferenceNode(annexLeaf);
        if (annexLeaf != null) {
            return getCrossReferencedElement(annexLeaf);
        }
    }
    INode node = getCrossReferenceNode(resource, new TextRegion(offset, 0));
    if (node != null) {
        return getCrossReferencedElement(node);
    }
    return null;
}
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) TextPositionInfo(org.osate.annexsupport.TextPositionInfo) EObject(org.eclipse.emf.ecore.EObject) AnnexTextPositionResolver(org.osate.annexsupport.AnnexTextPositionResolver) AnnexLibrary(org.osate.aadl2.AnnexLibrary) NamedElement(org.osate.aadl2.NamedElement) AnnexSubclause(org.osate.aadl2.AnnexSubclause)

Example 2 with AnnexTextPositionResolver

use of org.osate.annexsupport.AnnexTextPositionResolver in project osate2 by osate.

the class Aadl2EObjectAtOffsetHelper method resolveElementAt.

/*
	 * resolve the element, first by attempting as CrossReference and then as element itself
	 * 
	 * @see org.eclipse.xtext.resource.EObjectAtOffsetHelper#resolveElementAt(org.eclipse.xtext.resource.XtextResource, int)
	 */
@Override
public EObject resolveElementAt(XtextResource resource, int offset) {
    INode annexLeaf = AnnexUtil.findAnnexLeafNode(resource, offset);
    if (annexLeaf != null) {
        EObject obj = NodeModelUtils.findActualSemanticObjectFor(annexLeaf);
        if (textpositionresolverregistry == null) {
            initTextPositionResolverRegistry();
        }
        if (textpositionresolverregistry != null) {
            if (obj instanceof NamedElement) {
                String annexName = ((NamedElement) obj).getName();
                // find the actual subclause or library instead of the default one found from the NodeModel
                if (obj instanceof AnnexSubclause || obj instanceof AnnexLibrary) {
                    AnnexTextPositionResolver atpr = textpositionresolverregistry.getTextPositionResolver(annexName);
                    EObject actualAnnexElement = AnnexUtil.getParsedAnnex(obj);
                    if (atpr != null && actualAnnexElement != null) {
                        TextPositionInfo tpo = atpr.resolveElementAt(actualAnnexElement, offset);
                        return tpo.getModelObject();
                    }
                }
            }
        }
        // now try xtext based annexes via adapted ParseResult
        annexLeaf = getLeafInParsedAnnex((ILeafNode) annexLeaf, offset);
        if (annexLeaf != null) {
            return NodeModelUtils.findActualSemanticObjectFor(annexLeaf);
        } else {
            return obj;
        }
    }
    INode node = getCrossReferenceNode(resource, new TextRegion(offset, 0));
    if (node != null) {
        return getCrossReferencedElement(node);
    }
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null && parseResult.getRootNode() != null) {
        ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
        if (leaf != null && leaf.isHidden() && leaf.getOffset() == offset) {
            leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
        }
        if (leaf != null) {
            return NodeModelUtils.findActualSemanticObjectFor(leaf);
        }
    }
    return null;
// return internalResolveElementAt(resource, offset, true);
}
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) TextPositionInfo(org.osate.annexsupport.TextPositionInfo) EObject(org.eclipse.emf.ecore.EObject) AnnexTextPositionResolver(org.osate.annexsupport.AnnexTextPositionResolver) IParseResult(org.eclipse.xtext.parser.IParseResult) AnnexLibrary(org.osate.aadl2.AnnexLibrary) NamedElement(org.osate.aadl2.NamedElement) AnnexSubclause(org.osate.aadl2.AnnexSubclause)

Example 3 with AnnexTextPositionResolver

use of org.osate.annexsupport.AnnexTextPositionResolver in project osate2 by osate.

the class Aadl2HyperlinkHelper method createHyperlinksByOffset.

@Override
public void createHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) {
    INode annexLeaf = findAnnexTextLeafNode(resource, offset);
    if (annexLeaf != null) {
        // handle extensionpoint based text position
        EObject obj = NodeModelUtils.findActualSemanticObjectFor(annexLeaf);
        if (obj instanceof NamedElement) {
            EObject actualAnnexElement = AnnexUtil.getParsedAnnex(obj);
            if (actualAnnexElement != null) {
                String annexName = ((NamedElement) obj).getName();
                if (textpositionresolverregistry == null) {
                    initTextPositionResolverRegistry();
                }
                if (textpositionresolverregistry != null) {
                    AnnexTextPositionResolver atpr = textpositionresolverregistry.getTextPositionResolver(annexName);
                    if (atpr != null) {
                        TextPositionInfo tpo = atpr.resolveCrossReferencedElementAt(actualAnnexElement, offset);
                        if (tpo.getModelObject() != null && !tpo.getModelObject().eIsProxy()) {
                            Region region = new Region(tpo.getOffset(), tpo.getLength());
                            createHyperlinksTo(resource, region, tpo.getModelObject(), acceptor);
                        }
                    }
                }
            }
        }
    }
    // xtext based position is getting resolved by Aadl2EObjectAtOffsetHelper
    INode crossRefNode = eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(offset, 0));
    if (crossRefNode == null) {
        return;
    }
    EObject crossLinkedEObject = eObjectAtOffsetHelper.getCrossReferencedElement(crossRefNode);
    if (crossLinkedEObject != null && !crossLinkedEObject.eIsProxy()) {
        Region region = new Region(crossRefNode.getOffset(), crossRefNode.getLength());
        createHyperlinksTo(resource, region, crossLinkedEObject, acceptor);
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) TextRegion(org.eclipse.xtext.util.TextRegion) TextPositionInfo(org.osate.annexsupport.TextPositionInfo) EObject(org.eclipse.emf.ecore.EObject) AnnexTextPositionResolver(org.osate.annexsupport.AnnexTextPositionResolver) Region(org.eclipse.jface.text.Region) TextRegion(org.eclipse.xtext.util.TextRegion) NamedElement(org.osate.aadl2.NamedElement)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)3 INode (org.eclipse.xtext.nodemodel.INode)3 TextRegion (org.eclipse.xtext.util.TextRegion)3 NamedElement (org.osate.aadl2.NamedElement)3 AnnexTextPositionResolver (org.osate.annexsupport.AnnexTextPositionResolver)3 TextPositionInfo (org.osate.annexsupport.TextPositionInfo)3 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)2 ITextRegion (org.eclipse.xtext.util.ITextRegion)2 AnnexLibrary (org.osate.aadl2.AnnexLibrary)2 AnnexSubclause (org.osate.aadl2.AnnexSubclause)2 Region (org.eclipse.jface.text.Region)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1