use of org.eclipse.xtext.scoping.IScope in project dsl-devkit by dsldevkit.
the class ContainerBasedScope method toString.
@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(", query: ");
result.append(criteria);
result.append(", container: ");
result.append(container);
result.append(')');
final IScope parent = getParent();
if (parent != IScope.NULLSCOPE) {
result.append("\n >> ");
result.append(parent.toString().replaceAll("\\\n", "\n "));
}
return result.toString();
}
use of org.eclipse.xtext.scoping.IScope 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;
}
use of org.eclipse.xtext.scoping.IScope in project dsl-devkit by dsldevkit.
the class LinkingService method getSingleElement.
/**
* Gets an {@link EObject} that best matches a given context, reference, and qualified name.
* <p>
* The concept of a "best match" is "in the eye of the beholder", that is, the context and reference. The default case is the first element returned from the
* scope for the given context and reference that matches the given qualified name.
* </p>
*
* @param context
* the context of the {@code reference}, must not be {@code null}
* @param reference
* the reference for which the result element should be suitable, must not be {@code null}
* @param qualifiedLinkName
* the name that the result element should match, must not be {@code null}
* @return an eObject that best matches {@code qualifiedLinkName} in {@code scope} given the {@code context} and {@code reference},
* may by {@code null}
*/
protected EObject getSingleElement(final EObject context, final EReference reference, final QualifiedName qualifiedLinkName) {
IEObjectDescription desc = null;
IScope scope = getScope(context, reference);
if (scope instanceof WrappingTypedScope) {
desc = ((WrappingTypedScope) scope).getSingleElement(qualifiedLinkName, reference);
} else {
desc = scope.getSingleElement(qualifiedLinkName);
}
return desc == null ? null : desc.getEObjectOrProxy();
}
use of org.eclipse.xtext.scoping.IScope in project dsl-devkit by dsldevkit.
the class CatalogFromExtensionPointScopeHelper method createExtensionScope.
/**
* Creates an extension scope for each registered check catalog.
*
* @param parent
* the parent scope
* @param context
* the context object
* @return the check catalog scope
*/
public IScope createExtensionScope(final IScope parent, final Resource context) {
IScope result = parent;
NavigableSet<IModelLocation> locations = ICheckCatalogRegistry.INSTANCE.getAllCheckModelLocations();
for (IModelLocation locationData : locations.descendingSet()) {
// The UI proposes parent scope contents after each scope in the chain, so this scope chain must be created in reverse.
result = new CatalogFromExtensionPointScope(result, locationData, (XtextResourceSet) context.getResourceSet());
}
return result;
}
use of org.eclipse.xtext.scoping.IScope in project dsl-devkit by dsldevkit.
the class CommonCrossReferenceSerializer method getUnconvertedLinkText.
/**
* Returns the unconverted link text for the grammar element's scope. Based on
* {@link org.eclipse.xtext.parsetree.reconstr.impl.CrossReferenceSerializer#getUnconvertedLinkText}
*
* @param object
* the object, must not be {@code null}
* @param reference
* the reference, must not be {@code null}
* @param context
* the context, must not be{@code null}
* @return the unconverted link text or {@code null} if the {@code object} is not in the {@code context}'s scope
*/
protected String getUnconvertedLinkText(final EObject object, final EReference reference, final EObject context) {
IScope scope = scopeProvider.getScope(context, reference);
if (scope == null || scope == IScope.NULLSCOPE) {
return null;
}
IEObjectDescription eObjectDescription = scope.getSingleElement(object);
if (eObjectDescription != null) {
return qualifiedNameConverter.toString(eObjectDescription.getName());
}
return null;
}
Aggregations