Search in sources :

Example 71 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project n4js by eclipse.

the class ErrorAwareLinkingService method getLinkedObjects.

@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
    final EClass requiredType = ref.getEReferenceType();
    if (requiredType == null)
        return Collections.<EObject>emptyList();
    final String crossRefString = getCrossRefNodeAsString(context, ref, node);
    if (crossRefString != null && !crossRefString.equals("")) {
        final IScope scope = getScope(context, ref);
        QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
        IEObjectDescription eObjectDescription = scope.getSingleElement(qualifiedLinkName);
        if (IEObjectDescriptionWithError.isErrorDescription(eObjectDescription) && context.eResource() != null && !n4jsCore.isNoValidate(context.eResource().getURI())) {
            addError(context, node, IEObjectDescriptionWithError.getDescriptionWithError(eObjectDescription));
        } else if (eObjectDescription instanceof UnresolvableObjectDescription) {
            return Collections.<EObject>singletonList((EObject) context.eGet(ref, false));
        }
        if (eObjectDescription != null) {
            EObject candidate = eObjectDescription.getEObjectOrProxy();
            if (!candidate.eIsProxy() && candidate.eResource() == null) {
                // Error is necessary since EMF catches all exceptions in EcoreUtil#resolve
                throw new AssertionError("Found an instance without resource and without URI");
            }
            // if supported, mark object description as used
            if (eObjectDescription instanceof IUsageAwareEObjectDescription) {
                ((IUsageAwareEObjectDescription) eObjectDescription).markAsUsed();
            }
            return Collections.singletonList(candidate);
        }
    }
    return Collections.emptyList();
}
Also used : EClass(org.eclipse.emf.ecore.EClass) IUsageAwareEObjectDescription(org.eclipse.n4js.scoping.IUsageAwareEObjectDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) UnresolvableObjectDescription(org.eclipse.n4js.scoping.utils.UnresolvableObjectDescription)

Example 72 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project n4js by eclipse.

the class BuiltInTypeScopePluginTest method testLoadingBuiltInTypes.

@SuppressWarnings("javadoc")
@Test
public void testLoadingBuiltInTypes() {
    XtextResourceSet resourceSet = (XtextResourceSet) resourceSetProvider.get(null);
    resourceSet.setClasspathURIContext(N4JSResource.class.getClassLoader());
    BuiltInTypeScope scope = BuiltInTypeScope.get(resourceSet);
    IEObjectDescription anyType = scope.getSingleElement(QualifiedName.create("any"));
    Assert.assertNotNull(anyType);
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) N4JSResource(org.eclipse.n4js.resource.N4JSResource) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 73 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class CheckResourceUtil method allGrammars.

/**
 * Gets all available grammars.
 * <p>
 * The result contains no null entries.
 * </p>
 *
 * @return an iterator over all grammars in the workspace followed by all those in the registry.
 */
private Iterable<Grammar> allGrammars() {
    final ResourceSet resourceSetForResolve = new ResourceSetImpl();
    final Function<IEObjectDescription, Grammar> description2GrammarTransform = new Function<IEObjectDescription, Grammar>() {

        @Override
        public Grammar apply(final IEObjectDescription desc) {
            EObject obj = desc.getEObjectOrProxy();
            if (obj != null && obj.eIsProxy()) {
                obj = EcoreUtil.resolve(obj, resourceSetForResolve);
            }
            if (obj instanceof Grammar && !obj.eIsProxy()) {
                return (Grammar) obj;
            } else {
                return null;
            }
        }
    };
    final Iterable<IEObjectDescription> grammarDescriptorsFromIndex = Access.getIResourceDescriptions().get().getExportedObjectsByType(XtextPackage.Literals.GRAMMAR);
    return Iterables.concat(Iterables.filter(Iterables.transform(grammarDescriptorsFromIndex, description2GrammarTransform), Predicates.notNull()), allGrammarsFromRegistry());
}
Also used : Function(com.google.common.base.Function) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) EObject(org.eclipse.emf.ecore.EObject) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Grammar(org.eclipse.xtext.Grammar) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 74 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class ResourceDescriptionDelta method internalHasObjectFingerprints.

/**
 * Internal helper method to initialize {@link #objectFingerprints}.
 *
 * @return {@code true} if this delta has object fingerprints
 */
private boolean internalHasObjectFingerprints() {
    IResourceDescription desc = newDesc == null ? oldDesc : getNew();
    final Iterator<IEObjectDescription> objectIter = desc.getExportedObjects().iterator();
    if (!objectIter.hasNext()) {
        return false;
    } else {
        return objectIter.next().getUserData(IFingerprintComputer.OBJECT_FINGERPRINT) != null;
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 75 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class ResourceDescriptionDelta method internalHasChanges.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "PMD.CompareObjectsWithEquals", "PMD.NPathComplexity" })
protected boolean internalHasChanges() {
    if (getNew() == null || oldDesc == null) {
        // Deleted or new resource
        return true;
    }
    final Iterator<IEObjectDescription> oldObjectsIter = oldDesc.getExportedObjects().iterator();
    final Iterator<IEObjectDescription> newObjectsIter = getNew().getExportedObjects().iterator();
    final boolean empty = !oldObjectsIter.hasNext();
    if (empty != !newObjectsIter.hasNext()) {
        // One is empty, but the other one is not
        return true;
    } else if (empty) {
        // Both are empty
        return false;
    }
    final IEObjectDescription oldFirst = oldObjectsIter.next();
    final IEObjectDescription newFirst = newObjectsIter.next();
    objectFingerprints = newFirst.getUserData(IFingerprintComputer.OBJECT_FINGERPRINT) != null;
    final String oldHash = oldFirst.getUserData(IFingerprintComputer.RESOURCE_FINGERPRINT);
    final String newHash = newFirst.getUserData(IFingerprintComputer.RESOURCE_FINGERPRINT);
    if (oldHash != null && newHash != null) {
        // Both have fingerprints: it suffices to compare those!
        return !oldHash.equals(newHash);
    }
    // At least one has no fingerprint. If both have none, try the default method, otherwise, there is a change.
    if (oldHash != newHash) {
        if (LOGGER.isDebugEnabled()) {
            // $NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(getNew().getURI().toString() + ": fingerprint has changed from: " + oldHash + " to " + newHash);
        }
        return true;
    }
    if (!equals(oldFirst, newFirst)) {
        return true;
    }
    while (oldObjectsIter.hasNext()) {
        if (!newObjectsIter.hasNext() || !equals(oldObjectsIter.next(), newObjectsIter.next())) {
            return true;
        }
    }
    return newObjectsIter.hasNext();
}
Also used : IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)324 Test (org.junit.Test)95 EObject (org.eclipse.emf.ecore.EObject)82 QualifiedName (org.eclipse.xtext.naming.QualifiedName)79 IScope (org.eclipse.xtext.scoping.IScope)56 URI (org.eclipse.emf.common.util.URI)41 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)33 EClass (org.eclipse.emf.ecore.EClass)30 Resource (org.eclipse.emf.ecore.resource.Resource)28 EObjectDescription (org.eclipse.xtext.resource.EObjectDescription)22 AliasedEObjectDescription (org.eclipse.xtext.resource.impl.AliasedEObjectDescription)22 ArrayList (java.util.ArrayList)19 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)14 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 StyledString (org.eclipse.jface.viewers.StyledString)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 StringInputStream (org.eclipse.xtext.util.StringInputStream)11 EReference (org.eclipse.emf.ecore.EReference)10 List (java.util.List)9