Search in sources :

Example 11 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-xtend by eclipse.

the class XtendImportedNamespaceScopeProvider method getScope.

@Override
public IScope getScope(final EObject context, final EReference reference) {
    EClass referenceType = reference.getEReferenceType();
    if (TypesPackage.Literals.JVM_TYPE.isSuperTypeOf(referenceType)) {
        if (context instanceof XImportDeclaration) {
            Resource resource = context.eResource();
            IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
            AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
            IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
            IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
            if (resourceDescription != null) {
                typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
            }
            RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
            // TODO this scope doesn't support binary syntax for inner types. It should be a KnownTypes scope which doesn't allow simple names
            // Unfortunately I cannot use a RecordingTypeScope as a parent as it is not compatible...
            IScope scope = SelectableBasedScope.createScope(recordingTypeScope, getAllDescriptions(resource), reference.getEReferenceType(), false);
            return scope;
        }
        final XtendFile xtendFile = getXtendFile(context);
        final Resource resource = xtendFile.eResource();
        AbstractScope result = resourceScopeCache.get("type.scope", xtendFile.eResource(), new Provider<AbstractScope>() {

            @Override
            public AbstractScope get() {
                IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
                AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
                IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
                IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
                if (resourceDescription != null) {
                    typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
                }
                RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
                AbstractScope rootTypeScope = getRootTypeScope(xtendFile, recordingTypeScope);
                AbstractScope importScope = getImportScope(xtendFile.getImportSection(), rootTypeScope, recordingTypeScope);
                AbstractScope localTypes = getResourceTypeScope(xtendFile.eResource(), xtendFile.getPackage(), importScope);
                AbstractScope primitiveAware = new PrimitiveAwareScope(localTypes, typeScope);
                AbstractScope caching = new CachingTypeScope(primitiveAware);
                return caching;
            }
        });
        if (context instanceof AnonymousClass) {
            // necessary for the super type of an anonymous class expression
            JvmDeclaredType inferredAnonymousType = associations.getInferredType((AnonymousClass) context);
            if (inferredAnonymousType != null)
                result = new LocalTypeScope(singletonList(inferredAnonymousType), result);
        }
        XtendMember syntacticContainer = EcoreUtil2.getContainerOfType(context, XtendMember.class);
        if (syntacticContainer != null) {
            result = getContainerScope(syntacticContainer, result);
        }
        EObject logicalContainer = logicalContainerProvider.getNearestLogicalContainer(context);
        if (logicalContainer != null) {
            List<List<JvmTypeParameter>> typeParameters = new ArrayList<List<JvmTypeParameter>>();
            while (logicalContainer instanceof JvmTypeParameterDeclarator) {
                JvmTypeParameterDeclarator typeParamProvider = (JvmTypeParameterDeclarator) logicalContainer;
                if (!typeParamProvider.getTypeParameters().isEmpty()) {
                    typeParameters.add(typeParamProvider.getTypeParameters());
                }
                logicalContainer = logicalContainer.eContainer();
            }
            if (!typeParameters.isEmpty())
                result = new TypeParameterScope(typeParameters, result);
        }
        return result;
    } else if (TypesPackage.Literals.JVM_CONSTRUCTOR.isSuperTypeOf(referenceType)) {
        IScope typeScope = getScope(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
        // this is not called from the type resolution where we want to allow constructors to link to interfaces
        // in order to improve the error message, therefore we use a strict wrapper here
        IScope result = new ConstructorTypeScopeWrapper(context, IVisibilityHelper.ALL, typeScope, true);
        return result;
    } else {
        throw new IllegalArgumentException("Unexpected global request for " + reference);
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) AbstractTypeScope(org.eclipse.xtext.common.types.xtext.AbstractTypeScope) ArrayList(java.util.ArrayList) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) EClass(org.eclipse.emf.ecore.EClass) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) Resource(org.eclipse.emf.ecore.resource.Resource) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ConstructorTypeScopeWrapper(org.eclipse.xtext.xbase.scoping.batch.ConstructorTypeScopeWrapper)

Example 12 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class EObjectSnapshotProvider method createEObjectSnapshots.

protected Map<EObject, IEObjectSnapshot> createEObjectSnapshots(Resource resource) {
    Map<EObject, IEObjectSnapshot> result = Maps.newLinkedHashMap();
    for (IEObjectDescription desc : descriptionProvider.getEObjectDescriptions(resource)) {
        EObject obj = EcoreUtil.resolve(desc.getEObjectOrProxy(), resource);
        EObjectSnapshot snapshot = getOrCreate(result, obj);
        snapshot.descriptions.add(desc);
    }
    ResourceSet resourceSet = resource.getResourceSet();
    IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(resourceSet);
    URI uri = resource.getURI();
    for (IResourceDescription desc : descriptions.getAllResourceDescriptions()) {
        for (IReferenceDescription ref : desc.getReferenceDescriptions()) {
            URI targetURI = ref.getTargetEObjectUri();
            URI sourceEObjectUri = ref.getSourceEObjectUri();
            EReference reference = ref.getEReference();
            if (targetURI == null || sourceEObjectUri == null || reference == null) {
                continue;
            }
            if (!uri.equals(targetURI.trimFragment())) {
                continue;
            }
            EObject target = resource.getEObject(targetURI.fragment());
            if (target == null || target.eIsProxy()) {
                continue;
            }
            EObjectSnapshot snapshot = getOrCreate(result, target);
            ReferenceSnapshot rd = new ReferenceSnapshot(sourceEObjectUri, snapshot, reference, ref.getIndexInList(), ref.getContainerEObjectURI());
            snapshot.incomingReferences.add(rd);
        }
    }
    return result;
}
Also used : IEObjectSnapshot(org.eclipse.xtext.ide.serializer.hooks.IEObjectSnapshot) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IEObjectSnapshot(org.eclipse.xtext.ide.serializer.hooks.IEObjectSnapshot) URI(org.eclipse.emf.common.util.URI) IReferenceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IReferenceSnapshot) EReference(org.eclipse.emf.ecore.EReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription)

Example 13 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class AbstractLiveContainerTest method testContainerLoadUnload.

@Test
public void testContainerLoadUnload() throws Exception {
    ResourceSet resourceSet = new XtextResourceSet();
    Resource res = parse("local", resourceSet).eResource();
    Resource foo = resourceSet.createResource(computeUnusedUri(resourceSet));
    IResourceDescription resourceDescription = descriptionManager.getResourceDescription(res);
    IResourceDescriptions resourceDescriptions = descriptionsProvider.getResourceDescriptions(res);
    List<IContainer> containers = containerManager.getVisibleContainers(resourceDescription, resourceDescriptions);
    assertEquals(1, containers.size());
    IContainer container = containers.get(0);
    // assertEquals("local", format(container.getExportedObjects()));
    foo.load(new StringInputStream("foo"), null);
    assertEquals("foo, local", format(container.getExportedObjects()));
// foo.unload();
// assertEquals("local", format(container.getExportedObjects()));
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IContainer(org.eclipse.xtext.resource.IContainer) Test(org.junit.Test)

Example 14 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class XtextValidator method checkExternalPackage.

protected void checkExternalPackage(ReferencedMetamodel metamodelReference, String importURI) {
    EPackage referencedPackage = metamodelReference.getEPackage();
    if (referencedPackage.eIsProxy() || isRuntime(metamodelReference))
        return;
    if (isRegisteredPackage(referencedPackage)) {
        addIssue("The imported package is not on the classpath of this project which may lead to follow-up errors.", metamodelReference, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_NOT_ON_CLASSPATH, importURI);
        return;
    }
    if (!importURI.equals(referencedPackage.getNsURI())) {
        IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(metamodelReference.eResource());
        Iterable<IEObjectDescription> packagesInIndex = descriptions.getExportedObjects(EcorePackage.Literals.EPACKAGE, QualifiedName.create(referencedPackage.getNsURI()), false);
        if (!Iterables.isEmpty(packagesInIndex)) {
            addIssue("Packages should be imported by their namespace URI.", metamodelReference, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_EXTERNAL, referencedPackage.getNsURI());
        } else if (!ClasspathUriUtil.isClasspathUri(URI.createURI(importURI))) {
            addIssue("The imported package is not on the classpath of this project which may lead to follow-up errors.", metamodelReference, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_NOT_ON_CLASSPATH, importURI);
            return;
        } else {
            return;
        }
    }
    Map<EObject, Collection<Setting>> allReferences = EcoreUtil.CrossReferencer.find(Collections.singletonList(referencedPackage));
    Set<Object> allReferencedInstances = Sets.newHashSet();
    for (Setting setting : Iterables.concat(allReferences.values())) {
        Object referenced = setting.get(true);
        if (allReferencedInstances.add(referenced) && referenced instanceof EObject) {
            EPackage transitive = EcoreUtil2.getContainerOfType((EObject) referenced, EPackage.class);
            if (isRegisteredPackage(transitive)) {
                if (referenced instanceof EDataType)
                    continue;
                if (referenced == EcorePackage.Literals.EOBJECT)
                    continue;
                IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(metamodelReference.eResource());
                Iterable<IEObjectDescription> packagesInIndex = descriptions.getExportedObjects(EcorePackage.Literals.EPACKAGE, QualifiedName.create(importURI), false);
                if (!Iterables.isEmpty(packagesInIndex)) {
                    if (setting.getEObject().eResource().getURI().isPlatformResource())
                        addIssue("The imported package refers to elements in the package registry instead of using the instances from the workspace", metamodelReference, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_EXTERNAL, referencedPackage.getNsURI());
                    else
                        addIssue("The imported package refers to elements in the package registry instead of using the instances from the workspace", metamodelReference, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_EXTERNAL);
                    return;
                } else {
                    addIssue("The imported package refers to elements that are not on the classpath of this project. The package '" + transitive.getNsURI() + "' was loaded from the registry.", metamodelReference, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_NOT_ON_CLASSPATH, referencedPackage.getNsURI());
                    return;
                }
            }
        }
    }
}
Also used : EDataType(org.eclipse.emf.ecore.EDataType) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) EObject(org.eclipse.emf.ecore.EObject) Setting(org.eclipse.emf.ecore.EStructuralFeature.Setting) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) EPackage(org.eclipse.emf.ecore.EPackage) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 15 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class ImportUriGlobalScopeProvider method getResourceDescriptions.

public IResourceDescriptions getResourceDescriptions(Resource resource, Collection<URI> importUris) {
    IResourceDescriptions result = getResourceDescriptions(resource);
    LoadOnDemandResourceDescriptions demandResourceDescriptions = loadOnDemandDescriptions.get();
    demandResourceDescriptions.initialize(result, importUris, resource);
    return demandResourceDescriptions;
}
Also used : IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions)

Aggregations

IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)46 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)23 URI (org.eclipse.emf.common.util.URI)18 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)15 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)13 Resource (org.eclipse.emf.ecore.resource.Resource)12 EObject (org.eclipse.emf.ecore.EObject)11 QualifiedName (org.eclipse.xtext.naming.QualifiedName)7 IContainer (org.eclipse.xtext.resource.IContainer)7 List (java.util.List)6 Test (org.junit.Test)6 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)5 IScope (org.eclipse.xtext.scoping.IScope)5 IProject (org.eclipse.core.resources.IProject)4 EClass (org.eclipse.emf.ecore.EClass)4 XtextResource (org.eclipse.xtext.resource.XtextResource)4 IFile (org.eclipse.core.resources.IFile)3 CoreException (org.eclipse.core.runtime.CoreException)3 EPackage (org.eclipse.emf.ecore.EPackage)3 TModule (org.eclipse.n4js.ts.types.TModule)3