Search in sources :

Example 41 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class ResourceDescriptionsProvider method getResourceDescriptions.

/**
 * Provides the proper resource descriptions according to the context in which the resource set is used.
 *
 * The context is indicated by the {@link ResourceSet#getLoadOptions() load options} of the resource set. Supported
 * options are:
 *
 * <ol>
 * <li>{@link #NAMED_BUILDER_SCOPE}</li>
 * <li>{@link #LIVE_SCOPE}</li>
 * <li>{@link #PERSISTED_DESCRIPTIONS}</li>
 * <li>If no such option is present the dirty editors are taken into account to contribute their contents to the
 * index</li>
 * </ol>
 *
 * The result is never <code>null</code>.
 *
 * @param resourceSet
 *            the resource set that is currently used.
 * @return the {@link IResourceDescriptions} according to the usage context.
 *
 * @since 2.1
 */
/* @NonNull */
@Override
public IResourceDescriptions getResourceDescriptions(/* @NonNull */
ResourceSet resourceSet) {
    String flag = getFlagFromLoadOptions(resourceSet);
    IResourceDescriptions result;
    if (NAMED_BUILDER_SCOPE.equals(flag)) {
        result = createBuilderScopeResourceDescriptions();
    } else if (LIVE_SCOPE.equals(flag)) {
        result = createLiveScopeResourceDescriptions();
    } else if (PERSISTED_DESCRIPTIONS.equals(flag)) {
        result = createPersistedResourceDescriptions();
    } else {
        result = ChunkedResourceDescriptions.findInEmfObject(resourceSet);
        if (result == null) {
            result = createResourceDescriptions();
        }
    }
    if (result instanceof IResourceDescriptions.IContextAware) {
        ((IResourceDescriptions.IContextAware) result).setContext(resourceSet);
    }
    return result;
}
Also used : IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions)

Example 42 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class PortableURIs method getPortableURIFragment.

/**
 * @return a portable URI fragment, or <code>null</code> if the give EObject isn't itself or is not contained in an
 *         exported EObjectDescription
 */
protected String getPortableURIFragment(EObject obj) {
    IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(obj.eResource());
    IResourceDescription desc = descriptions.getResourceDescription(obj.eResource().getURI());
    if (desc == null) {
        return null;
    }
    return Streams.stream(desc.getExportedObjects()).filter(description -> {
        EObject possibleContainer = EcoreUtil.resolve(description.getEObjectOrProxy(), obj.eResource());
        return EcoreUtil.isAncestor(obj, possibleContainer);
    }).map(containerDesc -> {
        PortableFragmentDescription fragmentDescription = createPortableFragmentDescription(containerDesc, obj);
        return toFragmentString(fragmentDescription);
    }).findFirst().orElse(null);
}
Also used : EcoreFactory(org.eclipse.emf.ecore.EcoreFactory) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) URI(org.eclipse.emf.common.util.URI) EcorePackage(org.eclipse.emf.ecore.EcorePackage) Inject(com.google.inject.Inject) IGlobalScopeProvider(org.eclipse.xtext.scoping.IGlobalScopeProvider) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) Strings(com.google.common.base.Strings) EClass(org.eclipse.emf.ecore.EClass) Predicates(com.google.common.base.Predicates) EReference(org.eclipse.emf.ecore.EReference) Splitter(com.google.common.base.Splitter) QualifiedName(org.eclipse.xtext.naming.QualifiedName) Iterator(java.util.Iterator) IScope(org.eclipse.xtext.scoping.IScope) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) EObject(org.eclipse.emf.ecore.EObject) Streams(com.google.common.collect.Streams) EPackage(org.eclipse.emf.ecore.EPackage) List(java.util.List) ResourceDescriptionsProvider(org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Joiner(com.google.common.base.Joiner) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) EObject(org.eclipse.emf.ecore.EObject)

Example 43 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class DefaultGlobalScopeProvider method getVisibleContainers.

protected List<IContainer> getVisibleContainers(Resource resource) {
    IResourceDescription description = descriptionManager.getResourceDescription(resource);
    IResourceDescriptions resourceDescriptions = getResourceDescriptions(resource);
    String cacheKey = getCacheKey("VisibleContainers", resource.getResourceSet());
    OnChangeEvictingCache.CacheAdapter cache = new OnChangeEvictingCache().getOrCreate(resource);
    List<IContainer> result = null;
    result = cache.get(cacheKey);
    if (result == null) {
        result = containerManager.getVisibleContainers(description, resourceDescriptions);
        // container manager, but it is not aware of a CacheAdapter
        if (resourceDescriptions instanceof IResourceDescription.Event.Source) {
            IResourceDescription.Event.Source eventSource = (Source) resourceDescriptions;
            DelegatingEventSource delegatingEventSource = new DelegatingEventSource(eventSource);
            delegatingEventSource.addListeners(Lists.newArrayList(Iterables.filter(result, IResourceDescription.Event.Listener.class)));
            delegatingEventSource.initialize();
            cache.addCacheListener(delegatingEventSource);
        }
        cache.set(cacheKey, result);
    }
    return result;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) OnChangeEvictingCache(org.eclipse.xtext.util.OnChangeEvictingCache) Source(org.eclipse.xtext.resource.IResourceDescription.Event.Source) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) IContainer(org.eclipse.xtext.resource.IContainer) Source(org.eclipse.xtext.resource.IResourceDescription.Event.Source)

Example 44 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class PartialLinkingTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    with(ReferenceGrammarTestLanguageStandaloneSetup.class);
    IQualifiedNameConverter nameConverter = new IQualifiedNameConverter.DefaultImpl();
    IQualifiedNameProvider nameProvider = new DefaultDeclarativeQualifiedNameProvider();
    ImportUriGlobalScopeProvider globalScopeProvider = new ImportUriGlobalScopeProvider();
    globalScopeProvider.setImportResolver(new ImportUriResolver());
    globalScopeProvider.setCache(IResourceScopeCache.NullImpl.INSTANCE);
    final ResourceDescriptionsProvider provider = new ResourceDescriptionsProvider();
    provider.setResourceDescriptions(new Provider<IResourceDescriptions>() {

        @Override
        public IResourceDescriptions get() {
            return new IResourceDescriptions.NullImpl();
        }
    });
    globalScopeProvider.setResourceDescriptionsProvider(provider);
    globalScopeProvider.setLoadOnDemandDescriptions(new Provider<LoadOnDemandResourceDescriptions>() {

        @Override
        public LoadOnDemandResourceDescriptions get() {
            return PartialLinkingTest.this.get(LoadOnDemandResourceDescriptions.class);
        }
    });
    CaseInsensitivityHelper caseInsensitivityHelper = new CaseInsensitivityHelper();
    globalScopeProvider.setCaseInsensitivityHelper(caseInsensitivityHelper);
    scopeProvider = new ImportedNamespaceAwareLocalScopeProvider(globalScopeProvider, nameProvider, nameConverter, caseInsensitivityHelper);
    modelAsText = "spielplatz 1 {\n" + "  kind( Bommel1 1)\n" + "  kind( Bommel2 2)\n" + "  kind( Bommel3 3)\n" + "  erwachsener( Bob 4)\n" + "  erwachsener( Joe 5)\n" + "  familie( Familienname Bob Joe Bommel2 )\n" + "}";
    resource = getResourceFromString(modelAsText);
    assertTrue(resource.getErrors().toString(), resource.getErrors().isEmpty());
    model = resource.getParseResult().getRootASTElement();
    final Iterator<EObject> iter = model.eAllContents();
    while (iter.hasNext() && context == null) {
        final EObject candidate = iter.next();
        final String className = candidate.eClass().getName();
        if (className.endsWith("Familie"))
            context = candidate;
    }
    reference = (EReference) context.eClass().getEStructuralFeature("kinder");
}
Also used : DefaultDeclarativeQualifiedNameProvider(org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider) ImportedNamespaceAwareLocalScopeProvider(org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider) IQualifiedNameConverter(org.eclipse.xtext.naming.IQualifiedNameConverter) ImportUriResolver(org.eclipse.xtext.scoping.impl.ImportUriResolver) ResourceDescriptionsProvider(org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider) ImportUriGlobalScopeProvider(org.eclipse.xtext.scoping.impl.ImportUriGlobalScopeProvider) IQualifiedNameProvider(org.eclipse.xtext.naming.IQualifiedNameProvider) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) CaseInsensitivityHelper(org.eclipse.xtext.scoping.impl.CaseInsensitivityHelper) EObject(org.eclipse.emf.ecore.EObject) LoadOnDemandResourceDescriptions(org.eclipse.xtext.scoping.impl.LoadOnDemandResourceDescriptions)

Example 45 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-core by eclipse.

the class AbstractLiveContainerTest method testContainerAddRemove.

@Test
public void testContainerAddRemove() throws Exception {
    ResourceSet resourceSet = new XtextResourceSet();
    Resource res = parse("local", resourceSet).eResource();
    parse("other", resourceSet);
    IResourceDescription resourceDescription = descriptionManager.getResourceDescription(res);
    IResourceDescriptions resourceDescriptions = descriptionsProvider.getResourceDescriptions(res);
    List<IContainer> containers = containerManager.getVisibleContainers(resourceDescription, resourceDescriptions);
    assertEquals(1, containers.size());
    IContainer container = containers.get(0);
    assertEquals("local, other", format(container.getExportedObjects()));
    Resource foo = parse("foo", resourceSet).eResource();
    assertEquals("foo, local, other", format(container.getExportedObjects()));
    resourceSet.getResources().remove(foo);
    assertEquals("local, other", format(container.getExportedObjects()));
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IContainer(org.eclipse.xtext.resource.IContainer) Test(org.junit.Test)

Aggregations

IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)46 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)23 URI (org.eclipse.emf.common.util.URI)18 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)15 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)13 Resource (org.eclipse.emf.ecore.resource.Resource)12 EObject (org.eclipse.emf.ecore.EObject)11 QualifiedName (org.eclipse.xtext.naming.QualifiedName)7 IContainer (org.eclipse.xtext.resource.IContainer)7 List (java.util.List)6 Test (org.junit.Test)6 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)5 IScope (org.eclipse.xtext.scoping.IScope)5 IProject (org.eclipse.core.resources.IProject)4 EClass (org.eclipse.emf.ecore.EClass)4 XtextResource (org.eclipse.xtext.resource.XtextResource)4 IFile (org.eclipse.core.resources.IFile)3 CoreException (org.eclipse.core.runtime.CoreException)3 EPackage (org.eclipse.emf.ecore.EPackage)3 TModule (org.eclipse.n4js.ts.types.TModule)3