Search in sources :

Example 21 with IContainer

use of org.eclipse.xtext.resource.IContainer in project xtext-eclipse by eclipse.

the class StateBasedContainerManagerTest method testGetContainer_01.

@Test
public void testGetContainer_01() {
    IResourceDescription description = new URIBasedTestResourceDescription(uri1);
    IContainer container = containerManager.getContainer(description, this);
    assertEquals(2, Iterables.size(container.getResourceDescriptions()));
    assertNotNull(container.getResourceDescription(uri1));
    assertNotNull(container.getResourceDescription(uri2));
    assertNull(container.getResourceDescription(uri3));
}
Also used : URIBasedTestResourceDescription(org.eclipse.xtext.junit4.util.URIBasedTestResourceDescription) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IContainer(org.eclipse.xtext.resource.IContainer) Test(org.junit.Test)

Example 22 with IContainer

use of org.eclipse.xtext.resource.IContainer in project xtext-eclipse by eclipse.

the class StateBasedContainerManagerTest method testGetContainer_02.

@Test
public void testGetContainer_02() {
    IFile file = getFile(project1, "doesNotExist");
    URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
    IResourceDescription description = new URIBasedTestResourceDescription(uri);
    IContainer container = containerManager.getContainer(description, this);
    assertEquals(3, Iterables.size(container.getResourceDescriptions()));
    assertNotNull(container.getResourceDescription(uri));
    assertNotNull(container.getResourceDescription(uri1));
    assertNotNull(container.getResourceDescription(uri2));
    assertNull(container.getResourceDescription(uri3));
}
Also used : URIBasedTestResourceDescription(org.eclipse.xtext.junit4.util.URIBasedTestResourceDescription) IFile(org.eclipse.core.resources.IFile) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IContainer(org.eclipse.xtext.resource.IContainer) URI(org.eclipse.emf.common.util.URI) Test(org.junit.Test)

Example 23 with IContainer

use of org.eclipse.xtext.resource.IContainer in project dsl-devkit by dsldevkit.

the class CachingStateBasedContainerManager method createContainer.

@Override
protected synchronized IContainer createContainer(final String handle, final IResourceDescriptions resourceDescriptions) {
    final IResourceDescriptions descriptionsKey = resourceDescriptions instanceof CurrentDescriptions2.ResourceSetAware ? ((CurrentDescriptions2.ResourceSetAware) resourceDescriptions).getDelegate() : resourceDescriptions;
    Map<String, WeakReference<IContainer>> containersMap = descriptionsContainersCache.get(descriptionsKey);
    if (containersMap == null) {
        containersMap = new HashMap<String, WeakReference<IContainer>>();
        descriptionsContainersCache.put(descriptionsKey, containersMap);
    }
    WeakReference<IContainer> containerRef = containersMap.get(handle);
    IContainer container = containerRef != null ? containerRef.get() : null;
    if (container == null) {
        final IContainerState containerState = new ContainerState(handle, getStateProvider().get(descriptionsKey));
        container = new StateBasedContainerWithHandle(descriptionsKey, containerState, handle);
        new CacheInvalidator(descriptionsKey, handle, containerState);
        containerRef = new WeakReference<IContainer>(container);
        containersMap.put(handle, containerRef);
    }
    return container;
}
Also used : IContainerState(org.eclipse.xtext.resource.containers.IContainerState) IContainerState(org.eclipse.xtext.resource.containers.IContainerState) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) WeakReference(java.lang.ref.WeakReference) IContainer(org.eclipse.xtext.resource.IContainer)

Example 24 with IContainer

use of org.eclipse.xtext.resource.IContainer in project dsl-devkit by dsldevkit.

the class FixedDirtyStateEditorSupport method isReparseRequired.

/**
 * {@inheritDoc}
 */
@Override
protected boolean isReparseRequired(final XtextResource resource, final Event event) {
    // NOPMD (NPath complexity)
    IResourceDescription.Manager resourceDescriptionManager = resource.getResourceServiceProvider().getResourceDescriptionManager();
    final IResourceDescription description = resourceDescriptionManager.getResourceDescription(resource);
    // The DefaultResourceDescriptionManager honors container visibility only for platform and archive URIs... oh well; let's do it explicitly here.
    final IContainer.Manager containerManager = resource.getResourceServiceProvider().getContainerManager();
    // Unfortunately, we need a Collection in the call to isAffected() below...
    List<Delta> visibleChanges = Lists.newArrayList(Iterables.filter(event.getDeltas(), new Predicate<Delta>() {

        private List<IContainer> containers;

        private Iterable<IContainer> getContainers() {
            // Getting the containers is a relatively expensive operation. We delay doing this until we really need it.
            if (containers == null) {
                containers = containerManager.getVisibleContainers(description, resourceDescriptions);
            }
            return containers;
        }

        @Override
        public boolean apply(final Delta delta) {
            if (delta.haveEObjectDescriptionsChanged()) {
                if (delta.getNew() == null) {
                    // If it was deleted, we don't know whether it was visible. Hmphhh.
                    return true;
                }
                if (delta.getUri().isFile()) {
                    // Apparently these are java-resources, which are always visible?! (c.f. Xtext bug 383919.)
                    return true;
                }
                for (IContainer container : getContainers()) {
                    if (container.hasResourceDescription(delta.getUri())) {
                        return true;
                    }
                }
            }
            return false;
        }
    }));
    // Fix Xtext bug 383919: delegate the whole checking to the description manager, which may have optimized processing in place anyway.
    if (resourceDescriptionManager.isAffected(visibleChanges, description, resourceDescriptions)) {
        return true;
    }
    if (!isDirty() && !getDirtyStateManager().hasContent(resource.getURI())) {
        IResourceDescription originalDescription = resourceDescriptions.getResourceDescription(resource.getURI());
        if (originalDescription != null && descriptionUtils != null) {
            Set<URI> outgoingReferences = descriptionUtils.collectOutgoingReferences(originalDescription);
            for (Delta delta : visibleChanges) {
                if (outgoingReferences.contains(delta.getUri())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) Delta(org.eclipse.xtext.resource.IResourceDescription.Delta) List(java.util.List) IContainer(org.eclipse.xtext.resource.IContainer) URI(org.eclipse.emf.common.util.URI) Predicate(com.google.common.base.Predicate)

Example 25 with IContainer

use of org.eclipse.xtext.resource.IContainer in project dsl-devkit by dsldevkit.

the class AbstractPolymorphicScopeProvider method getVisibleContainers.

/**
 * Return the visible containers given a context object.
 *
 * @param context
 *          The context object
 * @param originalResource
 *          the original resource
 * @return The list of visible containers.
 */
protected List<IContainer> getVisibleContainers(final EObject context, final Resource originalResource) {
    // NOPMD by WTH on 26.01.11 09:26 (NPath
    // complexity...)
    final Resource ctxRsc = originalResource == null ? context.eResource() : originalResource;
    if (!(ctxRsc instanceof XtextResource)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        LOGGER.error(MessageFormat.format("Context {0} is not in an Xtext resource: {1}", context, ctxRsc != null ? ctxRsc.getURI() : "null"));
        throw new IllegalStateException();
    }
    final XtextResource rsc = (XtextResource) ctxRsc;
    // Cache these container lists, they're expensive to create
    // $NON-NLS-1$
    final String key = "CONTAINERCACHE&" + rsc.getURI().toString();
    List<IContainer> result = null;
    // $NON-NLS-1$
    final ResourceCache<String, List<IContainer>> cache = CacheManager.getInstance().getOrCreateResourceCache("AbstractPolymorphicScopeProvider#cache", rsc);
    if (cache != null) {
        result = cache.get(key);
        if (result != null) {
            return result;
        }
    }
    final EObject ctx = ctxRsc != context.eResource() ? ctxRsc.getContents().get(0) : context;
    // We need to get the container manager dynamically, otherwise we may end up using the wrong ResourceDescriptions if
    // the context object in actually from another resource.
    final IResourceServiceProvider resourceServiceProvider = rsc.getResourceServiceProvider();
    final IResourceDescription.Manager descriptionManager = resourceServiceProvider.getResourceDescriptionManager();
    final IContainer.Manager containerManager = resourceServiceProvider.getContainerManager();
    final IResourceDescription description = descriptionManager.getResourceDescription(ctx.eResource());
    final IResourceDescriptions resourceDescriptions = getResourceDescriptions(ctx.eResource());
    result = containerManager.getVisibleContainers(description, resourceDescriptions);
    if (cache != null) {
        cache.set(key, result);
    }
    return result;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) EObject(org.eclipse.emf.ecore.EObject) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) EList(org.eclipse.emf.common.util.EList) List(java.util.List) IContainer(org.eclipse.xtext.resource.IContainer)

Aggregations

IContainer (org.eclipse.xtext.resource.IContainer)25 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)13 URI (org.eclipse.emf.common.util.URI)7 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)7 Test (org.junit.Test)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)5 URIBasedTestResourceDescription (org.eclipse.xtext.junit4.util.URIBasedTestResourceDescription)4 Delta (org.eclipse.xtext.resource.IResourceDescription.Delta)4 IScope (org.eclipse.xtext.scoping.IScope)4 Resource (org.eclipse.emf.ecore.resource.Resource)3 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)3 LiveShadowedChunkedResourceDescriptions (org.eclipse.xtext.resource.impl.LiveShadowedChunkedResourceDescriptions)3 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 EObject (org.eclipse.emf.ecore.EObject)2 QualifiedName (org.eclipse.xtext.naming.QualifiedName)2 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 ChunkedResourceDescriptions (org.eclipse.xtext.resource.impl.ChunkedResourceDescriptions)2 ProjectDescription (org.eclipse.xtext.resource.impl.ProjectDescription)2