Search in sources :

Example 31 with IScope

use of org.eclipse.xtext.scoping.IScope 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 32 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreter method findTypeByName.

protected JvmType findTypeByName(final EObject context, final String qualifiedName) {
    final IScope scope = this.scopeProvider.getScope(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
    final QualifiedName qn = this.qualifiedNameConverter.toQualifiedName(qualifiedName);
    IEObjectDescription _singleElement = scope.getSingleElement(qn);
    EObject _eObjectOrProxy = null;
    if (_singleElement != null) {
        _eObjectOrProxy = _singleElement.getEObjectOrProxy();
    }
    return ((JvmType) _eObjectOrProxy);
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 33 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class ReferenceUpdater method updateReference.

@Override
public void updateReference(ITextRegionDiffBuilder rewriter, IUpdatableReference upd) {
    IUpdatableReference updatable = upd;
    if (rewriter.isModified(updatable.getReferenceRegion())) {
        return;
    }
    IScope scope = scopeProvider.getScope(updatable.getSourceEObject(), updatable.getEReference());
    ISemanticRegion region = updatable.getReferenceRegion();
    QualifiedName oldName = nameConverter.toQualifiedName(region.getText());
    IEObjectDescription oldDesc = scope.getSingleElement(oldName);
    if (oldDesc != null && oldDesc.getEObjectOrProxy() == updatable.getTargetEObject()) {
        return;
    }
    String newName = findValidName(updatable, scope);
    if (newName != null) {
        rewriter.replace(region, newName);
    }
}
Also used : IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IScope(org.eclipse.xtext.scoping.IScope) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 34 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class CrossReferenceSerializer method isValid.

@Override
public boolean isValid(EObject semanticObject, CrossReference crossref, EObject target, INode node, Acceptor errors) {
    if ((target == null || target.eIsProxy()) && node != null) {
        CrossReference crossrefFromNode = GrammarUtil.containingCrossReference(node.getGrammarElement());
        return crossref == crossrefFromNode;
    }
    final EReference ref = GrammarUtil.getReference(crossref, semanticObject.eClass());
    final IScope scope = scopeProvider.getScope(semanticObject, ref);
    if (scope == null) {
        if (errors != null)
            errors.accept(diagnostics.getNoScopeFoundDiagnostic(semanticObject, crossref, target));
        return false;
    }
    if (target != null && target.eIsProxy()) {
        target = handleProxy(target, semanticObject, ref);
    }
    return getCrossReferenceNameFromScope(semanticObject, crossref, target, scope, errors) != null;
}
Also used : CrossReference(org.eclipse.xtext.CrossReference) IScope(org.eclipse.xtext.scoping.IScope) EReference(org.eclipse.emf.ecore.EReference)

Example 35 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class CrossReferenceSerializer method serializeCrossRef.

@Override
public String serializeCrossRef(EObject semanticObject, CrossReference crossref, EObject target, INode node, Acceptor errors) {
    if ((target == null || target.eIsProxy()) && node != null)
        return tokenUtil.serializeNode(node);
    final EReference ref = GrammarUtil.getReference(crossref, semanticObject.eClass());
    final IScope scope = scopeProvider.getScope(semanticObject, ref);
    if (scope == null) {
        if (errors != null)
            errors.accept(diagnostics.getNoScopeFoundDiagnostic(semanticObject, crossref, target));
        return null;
    }
    if (target != null && target.eIsProxy()) {
        target = handleProxy(target, semanticObject, ref);
    }
    if (target != null && node != null) {
        String text = linkingHelper.getCrossRefNodeAsString(node, true);
        QualifiedName qn = qualifiedNameConverter.toQualifiedName(text);
        URI targetURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(target);
        for (IEObjectDescription desc : scope.getElements(qn)) {
            if (targetURI.equals(desc.getEObjectURI()))
                return tokenUtil.serializeNode(node);
        }
    }
    return getCrossReferenceNameFromScope(semanticObject, crossref, target, scope, errors);
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) IScope(org.eclipse.xtext.scoping.IScope) URI(org.eclipse.emf.common.util.URI) EReference(org.eclipse.emf.ecore.EReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

IScope (org.eclipse.xtext.scoping.IScope)148 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)57 Test (org.junit.Test)53 EObject (org.eclipse.emf.ecore.EObject)43 QualifiedName (org.eclipse.xtext.naming.QualifiedName)26 XtextResource (org.eclipse.xtext.resource.XtextResource)19 EReference (org.eclipse.emf.ecore.EReference)17 EClass (org.eclipse.emf.ecore.EClass)15 StringInputStream (org.eclipse.xtext.util.StringInputStream)15 URI (org.eclipse.emf.common.util.URI)12 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)12 ArrayList (java.util.ArrayList)10 Entity (org.eclipse.xtext.index.indexTestLanguage.Entity)10 Resource (org.eclipse.emf.ecore.resource.Resource)9 AbstractScopingTest (com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 FilteringScope (org.eclipse.xtext.scoping.impl.FilteringScope)7 Predicate (com.google.common.base.Predicate)6 EClassifier (org.eclipse.emf.ecore.EClassifier)6 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)6