use of org.eclipse.xtext.scoping.impl.ScopeBasedSelectable in project dsl-devkit by dsldevkit.
the class EPackageScopeProvider method scope_EPackage.
/**
* Scope for {@link EPackage}. These are read from the registry as well as from the {@link Grammar Xtext grammar} corresponding
* to the scope model (if any).
*
* @param context
* context scope DSL model (usually the root element, but any object will do)
* @param reference
* context reference (unused for the time being)
* @return scope with all available {@link EPackage EPackages} with {@link EPackage#getNsURI()} as name
*/
// CHECKSTYLE:OFF
public IScope scope_EPackage(final EObject context, final EReference reference) {
// CHECKSTYLE:ON
Resource rsc = context.eResource();
IScope result = IScope.NULLSCOPE;
// Add packages from the registry first.
final Registry packageRegistry = EPackage.Registry.INSTANCE;
result = new AbstractScope(result, false) {
@Override
protected IEObjectDescription getSingleLocalElementByName(final QualifiedName name) {
return getEPackage(nameConverter.toString(name));
}
@Override
protected Iterable<IEObjectDescription> getAllLocalElements() {
return Iterables.filter(Iterables.transform(Sets.newHashSet(packageRegistry.keySet()), this::getEPackage), Predicates.notNull());
}
private IEObjectDescription getEPackage(final String nsURI) {
try {
EPackage ePackage = packageRegistry.getEPackage(nsURI);
return ePackage != null ? EObjectDescription.create(QualifiedName.create(nsURI), ePackage) : null;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
// $NON-NLS-1$
LOG.warn("could not load package " + nsURI, e);
return null;
}
}
};
// Add the index
IResourceDescriptions descriptions = descriptionsProvider.getResourceDescriptions(context.eResource());
if (descriptions != null) {
result = SelectableBasedScope.createScope(result, descriptions, EcorePackage.Literals.EPACKAGE, false);
}
// Add the global scope
result = SelectableBasedScope.createScope(result, new ScopeBasedSelectable(globalScopeProvider.getScope(rsc, reference, null)), EcorePackage.Literals.EPACKAGE, false);
// Now add all packages from the grammar
final URI grammarUri = rsc.getURI().trimFileExtension().appendFileExtension(XTEXT_EXTENSION);
final ResourceSet resourceSet = rsc.getResourceSet();
final URIConverter uriConverter = resourceSet.getURIConverter();
if (uriConverter.exists(grammarUri, null)) {
final Resource grammarResource = resourceSet.getResource(grammarUri, true);
if (grammarResource != null && !grammarResource.getContents().isEmpty()) {
final Grammar grammar = (Grammar) grammarResource.getContents().get(0);
final IScope parent = result;
result = new SimpleScope(parent, Iterables.transform(Iterables.filter(getGrammarEPackages(grammar), Predicates.notNull()), new Function<EPackage, IEObjectDescription>() {
@Override
public IEObjectDescription apply(final EPackage param) {
return EObjectDescription.create(param.getNsURI(), param);
}
}));
}
}
return result;
}
Aggregations