protected EStructuralFeatureAndEObject find(XtextResource resource, int offset) { INode leaf = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset); NodeIterator ni = null; while (ni == null || ni.hasNext()) { INode next = ni == null ? leaf : ni.next(); if (ni == null) ni = new NodeIterator(leaf); EObject object = NodeModelUtils.findActualSemanticObjectFor(next); INode current = next; do { Assignment ass = GrammarUtil.containingAssignment(current.getGrammarElement()); if (ass != null) { EStructuralFeature feat = object.eClass().getEStructuralFeature(ass.getFeature()); if (feat != null && matches(object, feat)) return create(object, feat); } current = current.getParent(); } while (current != null && object == NodeModelUtils.findActualSemanticObjectFor(current)); } throw new RuntimeException("No EStructuralFeature found at offset " + offset); }
public Set<QualifiedName> getImportedNames(XtextResource resource) { XPackage xPackage = getXPackage(resource); String packageName = xPackage.getName(); List<String> implicitPackageImports = newArrayList(packageName, "java.lang"); Set<QualifiedName> importedNames = newLinkedHashSet(); Map<String, QualifiedName> implicitAliases = getImplicitAliases(); for (INode node : XcoreUtil.importableCrossReferences(xPackage)) { CrossReference grammarElement = (CrossReference) node.getGrammarElement(); EObject container = grammarElement.eContainer(); if (container instanceof Assignment) { String name = node.getText().trim(); if (name.endsWith("::")) { name = name.substring(0, name.length() - 2); } QualifiedName actualQualifiedName = nameConverter.toQualifiedName(name); Assignment assignment = (Assignment) container; String feature = assignment.getFeature(); EObject semanticObject = NodeModelUtils.findActualSemanticObjectFor(node); EStructuralFeature eStructuralFeature = semanticObject.eClass().getEStructuralFeature(feature); if (!eStructuralFeature.isMany()) { EObject eCrossReference = (EObject) semanticObject.eGet(eStructuralFeature); EObject eContainer = eCrossReference.eContainer(); if (eContainer != xPackage && !(eContainer instanceof XPackage && "xcore.lang".equals(((XPackage) eContainer).getName())) && !(eContainer instanceof GenPackage && packageName.equals(((GenPackage) eContainer).getQualifiedPackageName())) && !(eCrossReference instanceof JvmDeclaredType && implicitPackageImports.contains( ((JvmDeclaredType) eCrossReference).getPackageName()))) { QualifiedName fullyQualifiedName = nameProvider.getFullyQualifiedName(eCrossReference); if (fullyQualifiedName != null && !actualQualifiedName.equals(fullyQualifiedName) && !fullyQualifiedName.equals(implicitAliases.get(name))) { importedNames.add(fullyQualifiedName); } } } else { throw new RuntimeException("Not expecting multi-valued cross references in these models"); } } else { throw new RuntimeException( "Expecting all cross references to be part of an assignment in these models"); } } return importedNames; }
protected ICrossReferenceDescription getCrossReferenceDescription() { ICrossReferenceDescription _xblockexpression = null; { ASTNode _node = this.myElement.getNode(); IElementType _elementType = _node.getElementType(); EObject _grammarElement = ((IGrammarAwareElementType) _elementType).getGrammarElement(); final CrossReference crossReference = ((CrossReference) _grammarElement); BaseXtextFile _xtextFile = this.myElement.getXtextFile(); ASTNode _node_1 = this.myElement.getNode(); final INode node = _xtextFile.getINode(_node_1); final EObject context = NodeModelUtils.findActualSemanticObjectFor(node); final EReference reference = GrammarUtil.getReference(crossReference); List<INode> _findNodesForFeature = NodeModelUtils.findNodesForFeature(context, reference); Iterable<Pair<Integer, INode>> _indexed = IterableExtensions.<INode>indexed(_findNodesForFeature); final Function1<Pair<Integer, INode>, Boolean> _function = new Function1<Pair<Integer, INode>, Boolean>() { @Override public Boolean apply(final Pair<Integer, INode> it) { boolean _and = false; INode _value = it.getValue(); int _totalOffset = _value.getTotalOffset(); int _totalOffset_1 = node.getTotalOffset(); boolean _lessEqualsThan = (_totalOffset <= _totalOffset_1); if (!_lessEqualsThan) { _and = false; } else { INode _value_1 = it.getValue(); int _totalEndOffset = _value_1.getTotalEndOffset(); int _totalEndOffset_1 = node.getTotalEndOffset(); boolean _greaterEqualsThan = (_totalEndOffset >= _totalEndOffset_1); _and = _greaterEqualsThan; } return Boolean.valueOf(_and); } }; Pair<Integer, INode> _findFirst = IterableExtensions.<Pair<Integer, INode>>findFirst(_indexed, _function); Integer _key = null; if (_findFirst != null) { _key = _findFirst.getKey(); } final Integer index = _key; _xblockexpression = this.crossReferenceDescriptionProvider.get(context, reference, index); } return _xblockexpression; }