Search in sources :

Example 86 with IScope

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

the class ValidScopingTest method testEClassScope.

/**
 * Tests that the Valid language allows defining EClass instances as contexts for validation rules.
 */
@Test
public void testEClassScope() throws IOException {
    final IScope scope = scopeProvider.getScope(getTestSource().getModel(), ValidPackage.Literals.CONTEXT__CONTEXT_TYPE);
    assertNotNull("Found EClass \"Model\"", scope.getSingleElement(QualifiedName.create("Model")));
    assertNull("Found non-existing EClass", scope.getSingleElement(QualifiedName.create("NonExistingEClass")));
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) AbstractScopingTest(com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest) Test(org.junit.Test)

Example 87 with IScope

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

the class AbstractPolymorphicScopeProvider method internalGetScope.

/**
 * Gets the scope given a context object and an expected type, using the given scope name.
 *
 * @param context
 *          the context, must not be {@code null}
 * @param namedElement
 *          the named element (either EClass or EReference), must not be {@code null}
 * @param scopeName
 *          the scope name, must not be {@code null}
 * @param originalResource
 *          the original resource, must not be {@code null}
 * @return the scope, or {@code null} if none
 */
@SuppressWarnings({ "PMD.CompareObjectsWithEquals", "PMD.NPathComplexity" })
protected IScope internalGetScope(final EObject context, final ENamedElement namedElement, final String scopeName, final Resource originalResource) {
    IScope result = null;
    IResourceSetScopeCache resourceSetScopeCache = null;
    ResourceCache<Object, IScope> resourceScopeCache = null;
    Object cacheKey = null;
    Object globalCacheKey = null;
    if (originalResource != null) {
        final ResourceSet resourceSet = originalResource.getResourceSet();
        if (resourceSet != null) {
            final Adapter scopeCacheAdapter = EcoreUtil.getAdapter(resourceSet.eAdapters(), IResourceSetScopeCache.class);
            if (scopeCacheAdapter instanceof IResourceSetScopeCache && doGlobalCache(context, namedElement, scopeName, originalResource)) {
                resourceSetScopeCache = (IResourceSetScopeCache) scopeCacheAdapter;
                globalCacheKey = getGlobalCacheKey(context, namedElement, scopeName);
                result = resourceSetScopeCache.get(globalCacheKey);
            }
        }
        if (result == null && doCache(context, namedElement, scopeName, originalResource)) {
            // $NON-NLS-1$
            resourceScopeCache = CacheManager.getInstance().getOrCreateResourceCache("AbstractPolymorphicScopeProvider#resourceScopeCache", originalResource);
            cacheKey = getCacheKey(context, namedElement, scopeName);
            result = resourceScopeCache.get(cacheKey);
        }
        if (result != null) {
            return result == NO_SCOPE ? null : result;
        }
    }
    result = doGetScope(context, namedElement, scopeName, originalResource);
    if (resourceSetScopeCache != null) {
        resourceSetScopeCache.put(globalCacheKey, result == null ? NO_SCOPE : result);
    } else if (resourceScopeCache != null) {
        resourceScopeCache.set(cacheKey, result == null ? NO_SCOPE : result);
    }
    return result;
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) EObject(org.eclipse.emf.ecore.EObject) Adapter(org.eclipse.emf.common.notify.Adapter) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 88 with IScope

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

the class AbstractPolymorphicScopeProvider method newPrefixedContainerScope.

/**
 * Create a new prefix container scope using the results of a given query as its contents, using the root object of the given resource
 * as its context.
 *
 * @param id
 *          Human-readable name of the scope, typically used to identify where the scope was created. Useful for debugging.
 * @param outer
 *          The outer scope of the new scope.
 * @param rsc
 *          The context resource
 * @param query
 *          The query that defines the scope's contents
 * @param originalResource
 *          The original resource
 * @param prefix
 *          prefix to apply for single element lookups
 * @param recursive
 *          whether the qualified name pattern used to search the index should be
 *          {@link com.avaloq.tools.ddk.xtext.naming.QualifiedNamePattern#RECURSIVE_WILDCARD_SEGMENT recursive}
 * @param nameFunctions
 *          The name functions to apply
 * @param caseInsensitive
 *          indicates whether the new scope shall be case insensitive
 * @return The new scope
 */
protected IScope newPrefixedContainerScope(final String id, final IScope outer, final Resource rsc, final ContainerQuery.Builder query, final Resource originalResource, final String prefix, final boolean recursive, final Iterable<INameFunction> nameFunctions, final boolean caseInsensitive) {
    IScope result = outer;
    // We always need a context. Default to the top element of the resource.
    final EList<EObject> contents = rsc.getContents();
    if (contents.size() > 0) {
        result = newPrefixedContainerScope(id, result, contents.get(0), query, originalResource, prefix, recursive, nameFunctions, caseInsensitive);
    }
    return result;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope)

Example 89 with IScope

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

the class AbstractPolymorphicScopeProvider method newDataMatchScope.

/**
 * Create a new container scope using the results of a given query as its contents, using the root object of the given resource
 * as its context.
 *
 * @param id
 *          Human-readable name of the scope, typically used to identify where the scope was created. Useful for debugging.
 * @param outer
 *          The outer scope of the new scope.
 * @param rsc
 *          The context resource
 * @param query
 *          The query that defines the scope's contents
 * @param originalResource
 *          The original resource
 * @param filters
 *          to apply
 * @param nameFunctions
 *          The name functions to apply
 * @param caseInsensitive
 *          indicates whether the new scope shall be case insensitive
 * @return The new scope
 */
protected IScope newDataMatchScope(final String id, final IScope outer, final Resource rsc, final ContainerQuery query, final Resource originalResource, final Iterable<Predicate<IEObjectDescription>> filters, final Iterable<INameFunction> nameFunctions, final boolean caseInsensitive) {
    IScope result = outer;
    // We always need a context. Default to the top element of the resource.
    final EList<EObject> contents = rsc.getContents();
    if (contents.size() > 0) {
        result = newDataMatchScope(id, result, contents.get(0), query, originalResource, filters, nameFunctions, caseInsensitive);
    }
    return result;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope)

Example 90 with IScope

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

the class AbstractRecursiveScope method toString.

/**
 * For debugging.
 *
 * @return A string representation of the scope useful for debugging.
 */
@SuppressWarnings("nls")
@Override
public String toString() {
    final StringBuilder result = new StringBuilder(getClass().getName());
    result.append('@');
    result.append(Integer.toHexString(hashCode()));
    result.append(" (id: ");
    result.append(getId());
    result.append(')');
    final IScope outerScope = getParent();
    if (outerScope != IScope.NULLSCOPE) {
        result.append("\n  >> ");
        result.append(outerScope.toString().replaceAll("\\\n", "\n  "));
    }
    return result.toString();
}
Also used : IScope(org.eclipse.xtext.scoping.IScope)

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