Search in sources :

Example 46 with IScope

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

the class XbaseProposalProvider method completeXMemberFeatureCall_Feature.

@Override
public void completeXMemberFeatureCall_Feature(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    if (model instanceof XMemberFeatureCall) {
        XExpression memberCallTarget = ((XMemberFeatureCall) model).getMemberCallTarget();
        IResolvedTypes resolvedTypes = typeResolver.resolveTypes(memberCallTarget);
        LightweightTypeReference memberCallTargetType = resolvedTypes.getActualType(memberCallTarget);
        Iterable<JvmFeature> featuresToImport = getFavoriteStaticFeatures(model, input -> {
            if (input instanceof JvmOperation && input.isStatic()) {
                List<JvmFormalParameter> parameters = ((JvmOperation) input).getParameters();
                if (parameters.size() > 0) {
                    JvmFormalParameter firstParam = parameters.get(0);
                    JvmTypeReference parameterType = firstParam.getParameterType();
                    if (parameterType != null) {
                        LightweightTypeReference lightweightTypeReference = memberCallTargetType.getOwner().toLightweightTypeReference(parameterType);
                        if (lightweightTypeReference != null) {
                            return memberCallTargetType.isAssignableFrom(lightweightTypeReference);
                        }
                    }
                }
            }
            return false;
        });
        // Create StaticExtensionFeatureDescriptionWithImplicitFirstArgument instead of SimpleIdentifiableElementDescription since we want the Proposal to show parameters
        Iterable<IEObjectDescription> scopedFeatures = Iterables.transform(featuresToImport, feature -> {
            QualifiedName qualifiedName = QualifiedName.create(feature.getSimpleName());
            return new StaticExtensionFeatureDescriptionWithImplicitFirstArgument(qualifiedName, feature, memberCallTarget, memberCallTargetType, 0, true);
        });
        // Scope for all static features
        IScope staticMemberScope = new SimpleScope(IScope.NULLSCOPE, scopedFeatures);
        proposeFavoriteStaticFeatures(model, context, acceptor, staticMemberScope);
        // Regular proposals
        createReceiverProposals(((XMemberFeatureCall) model).getMemberCallTarget(), (CrossReference) assignment.getTerminal(), context, acceptor);
    } else if (model instanceof XAssignment) {
        createReceiverProposals(((XAssignment) model).getAssignable(), (CrossReference) assignment.getTerminal(), context, acceptor);
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StaticExtensionFeatureDescriptionWithImplicitFirstArgument(org.eclipse.xtext.xbase.scoping.batch.StaticExtensionFeatureDescriptionWithImplicitFirstArgument) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFeature(org.eclipse.xtext.common.types.JvmFeature) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) IScope(org.eclipse.xtext.scoping.IScope) CrossReference(org.eclipse.xtext.CrossReference) XExpression(org.eclipse.xtext.xbase.XExpression) XAssignment(org.eclipse.xtext.xbase.XAssignment)

Example 47 with IScope

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

the class ArithmeticsScopeProvider method getScope.

@Override
public IScope getScope(EObject context, EReference reference) {
    if (reference == ArithmeticsPackage.Literals.IMPORT__MODULE) {
        return super.getGlobalScope(context.eResource(), reference);
    }
    Module module = EcoreUtil2.getContainerOfType(context, Module.class);
    IScope result = IScope.NULLSCOPE;
    for (Import i : module.getImports()) {
        if (!i.getModule().eIsProxy()) {
            result = getModuleScope(context, reference, i.getModule(), result);
        }
    }
    result = getModuleScope(context, reference, module, result);
    return getDefinitionScope(context, reference, result);
}
Also used : Import(org.eclipse.xtext.example.arithmetics.arithmetics.Import) IScope(org.eclipse.xtext.scoping.IScope) Module(org.eclipse.xtext.example.arithmetics.arithmetics.Module)

Example 48 with IScope

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

the class JavaTypeQuickfixes method addQuickfixes.

@Override
public void addQuickfixes(Issue issue, IssueResolutionAcceptor issueResolutionAcceptor, IXtextDocument xtextDocument, XtextResource resource, EObject referenceOwner, EReference unresolvedReference) throws Exception {
    String issueString = xtextDocument.get(issue.getOffset(), issue.getLength());
    IScope scope = scopeProvider.getScope(referenceOwner, unresolvedReference);
    boolean useJavaSearch = isUseJavaSearch(unresolvedReference, issue);
    if (useJavaSearch) {
        JvmDeclaredType jvmType = importsConfiguration.getContextJvmDeclaredType(referenceOwner);
        IJavaSearchScope javaSearchScope = getJavaSearchScope(referenceOwner);
        boolean proposeImports = true;
        if (isConstructorReference(unresolvedReference))
            proposeImports = !createConstructorProposals(jvmType, issue, issueString, javaSearchScope, issueResolutionAcceptor);
        if (proposeImports)
            createImportProposals(jvmType, issue, issueString, javaSearchScope, issueResolutionAcceptor);
        scope = getImportedTypesScope(referenceOwner, issueString, scope, javaSearchScope);
    }
    List<IEObjectDescription> discardedDescriptions = Lists.newArrayList();
    Set<String> proposedSolutions = Sets.newHashSet();
    int addedDescriptions = 0;
    int checkedDescriptions = 0;
    for (IEObjectDescription referableElement : scope.getAllElements()) {
        String solution = qualifiedNameConverter.toString(referableElement.getName());
        if (!equal(issueString, solution) && proposedSolutions.add(solution)) {
            if (useJavaSearch || similarityMatcher.isSimilar(issueString, solution)) {
                addedDescriptions++;
                createResolution(issue, issueResolutionAcceptor, issueString, referableElement);
                proposedSolutions.add(solution);
            } else {
                discardedDescriptions.add(referableElement);
            }
        }
        checkedDescriptions++;
        if (checkedDescriptions > 100)
            break;
    }
    if (discardedDescriptions.size() + addedDescriptions <= 5) {
        for (IEObjectDescription referableElement : discardedDescriptions) {
            createResolution(issue, issueResolutionAcceptor, issueString, referableElement);
        }
    }
}
Also used : IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) IScope(org.eclipse.xtext.scoping.IScope) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 49 with IScope

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

the class XbaseHoverDocumentationProvider method getResolvedDeclarator.

protected JvmDeclaredType getResolvedDeclarator(String name) {
    JvmDeclaredType jvmDeclaredType = null;
    if (Strings.isEmpty(name) || name.trim().length() == 0) {
        jvmDeclaredType = getDeclaringType(context);
    } else {
        HoverReference reference = new HoverReference(TypesPackage.Literals.JVM_TYPE);
        IScope scope = scopeProvider.getScope(context, reference);
        IEObjectDescription declarator = scope.getSingleElement(qualifiedNameConverter.toQualifiedName(name));
        if (declarator != null && EcoreUtil2.isAssignableFrom(TypesPackage.eINSTANCE.getJvmGenericType(), declarator.getEClass())) {
            jvmDeclaredType = (JvmDeclaredType) context.eResource().getResourceSet().getEObject(declarator.getEObjectURI(), true);
        }
    }
    return jvmDeclaredType;
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 50 with IScope

use of org.eclipse.xtext.scoping.IScope in project n4js by eclipse.

the class IntersectionMemberScope method getCheckedDescription.

@Override
protected IEObjectDescription getCheckedDescription(String name, TMember member) {
    IEObjectDescription description = EObjectDescription.create(member.getName(), member);
    QualifiedName qn = QualifiedName.create(name);
    boolean allDescrWithError = true;
    for (IScope currSubScope : subScopes) {
        IEObjectDescription subDescription = currSubScope.getSingleElement(qn);
        boolean descrWithError = subDescription == null || subDescription instanceof IEObjectDescriptionWithError;
        allDescrWithError &= descrWithError;
    }
    if (allDescrWithError) {
        return createComposedMemberDescriptionWithErrors(description);
    }
    return description;
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescriptionWithError(org.eclipse.n4js.xtext.scoping.IEObjectDescriptionWithError) 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