Search in sources :

Example 91 with IScope

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();
}
Also used : IScope(org.eclipse.xtext.scoping.IScope)

Example 92 with IScope

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;
}
Also used : AbstractScope(org.eclipse.xtext.scoping.impl.AbstractScope) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) QualifiedName(org.eclipse.xtext.naming.QualifiedName) Resource(org.eclipse.emf.ecore.resource.Resource) Registry(org.eclipse.emf.ecore.EPackage.Registry) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Grammar(org.eclipse.xtext.Grammar) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) EPackage(org.eclipse.emf.ecore.EPackage) ScopeBasedSelectable(org.eclipse.xtext.scoping.impl.ScopeBasedSelectable) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) IScope(org.eclipse.xtext.scoping.IScope) URIConverter(org.eclipse.emf.ecore.resource.URIConverter)

Example 93 with IScope

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();
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) WrappingTypedScope(com.avaloq.tools.ddk.xtext.linking.ImportedNamesTypesAdapter.WrappingTypedScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 94 with IScope

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;
}
Also used : IModelLocation(com.avaloq.tools.ddk.check.runtime.configuration.IModelLocation) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IScope(org.eclipse.xtext.scoping.IScope)

Example 95 with IScope

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;
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) 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