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")));
}
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;
}
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;
}
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;
}
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();
}
Aggregations