Search in sources :

Example 1 with IResourceSnapshot

use of org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot in project xtext-core by eclipse.

the class ChangeTreeProvider method createChangeTree.

public ResourceSetRecording createChangeTree(ResourceSet resourceSet, Collection<IResourceSnapshot> snapshots, ChangeDescription desc) {
    ResourceSetRecording rs = new ResourceSetRecording(resourceSet);
    Map<Resource, ResourceRecording> result = Maps.newLinkedHashMap();
    Map<EObject, EObjectChange> objectChanges = Maps.newLinkedHashMap();
    for (IResourceSnapshot snap : snapshots) {
        Resource resource = snap.getResource();
        ResourceRecording recording = new ResourceRecording(rs, resource);
        recording.snapshot = snap;
        result.put(resource, recording);
    }
    for (ResourceChange rc : desc.getResourceChanges()) {
        Resource resource = rc.getResource();
        ResourceRecording recording = result.get(resource);
        if (recording == null) {
            recording = new ResourceRecording(rs, resource);
            result.put(resource, recording);
        }
        recording.resourceChange = rc;
    }
    for (Entry<EObject, EList<FeatureChange>> e : desc.getObjectChanges().entrySet()) {
        EList<FeatureChange> featureChange = e.getValue();
        EObject object = e.getKey();
        objectChanges.put(object, new EObjectChange(object, featureChange));
    }
    for (EObjectChange change : Lists.newArrayList(objectChanges.values())) {
        EObjectChange current = change;
        while (true) {
            EObject container = current.eObject.eContainer();
            if (container != null) {
                EObjectChange next = objectChanges.get(container);
                if (next == null) {
                    next = new EObjectChange(container);
                    next.children.add(current);
                    objectChanges.put(container, next);
                } else {
                    if (!next.children.add(current)) {
                        break;
                    }
                }
                current = next;
            } else {
                Resource resource = current.eObject.eResource();
                ResourceRecording resourceChange = result.get(resource);
                if (resourceChange == null) {
                    resourceChange = new ResourceRecording(rs, resource);
                    result.put(resource, resourceChange);
                // TODO: check that only recorded resources contain
                // modification
                }
                resourceChange.roots.add(current);
                break;
            }
        }
    }
    rs.resources = ImmutableList.copyOf(result.values());
    rs.objects = ImmutableMap.copyOf(objectChanges);
    return rs;
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) FeatureChange(org.eclipse.emf.ecore.change.FeatureChange) EList(org.eclipse.emf.common.util.EList) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot) EObject(org.eclipse.emf.ecore.EObject) ResourceChange(org.eclipse.emf.ecore.change.ResourceChange)

Example 2 with IResourceSnapshot

use of org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot in project xtext-core by eclipse.

the class EObjectDescriptionDeltaProvider method getDelta.

public Deltas getDelta(ChangeSerializer serializer, Collection<IResourceSnapshot> snapshots) {
    Map<EObject, IEObjectSnapshot> old = Maps.newHashMap();
    Map<EObject, Group> groups = Maps.newHashMap();
    for (IResourceSnapshot rs : snapshots) {
        old.putAll(rs.getObjects());
    }
    for (IResourceSnapshot rs : snapshots) {
        IEObjectDescriptionProvider p = serializer.getService(rs.getResource(), IEObjectDescriptionProvider.class);
        Iterable<IEObjectDescription> descriptions = p.getEObjectDescriptions(rs.getResource());
        for (IEObjectDescription desc : descriptions) {
            EObject obj = EcoreUtil.resolve(desc.getEObjectOrProxy(), rs.getResource());
            Group delta = groups.get(obj);
            if (delta == null) {
                delta = new Group(obj);
                delta.snapshot = old.get(obj);
                groups.put(obj, delta);
            }
            delta.descriptions.add(desc);
        }
    }
    Deltas result = new Deltas();
    result.snapshots = snapshots;
    for (Group g : groups.values()) {
        Delta delta = createDelta(g.object, g.snapshot, g.descriptions);
        if (delta.hasQualifiedNameChanged() || delta.hasSimpleNameOrUserdataChanged()) {
            result.deltas.put(delta.object, delta);
        }
    }
    return result;
}
Also used : IEObjectSnapshot(org.eclipse.xtext.ide.serializer.hooks.IEObjectSnapshot) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot) EObject(org.eclipse.emf.ecore.EObject) IEObjectDescriptionProvider(org.eclipse.xtext.ide.serializer.hooks.IEObjectDescriptionProvider) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 3 with IResourceSnapshot

use of org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot in project xtext-core by eclipse.

the class RecordingXtextResourceUpdater method applyChange.

@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
    Resource resource = snapshot.getResource();
    ResourceSet rs = resource.getResourceSet();
    ReferenceUpdaterContext ctx = new ReferenceUpdaterContext(deltas, document);
    if (serializer.isUpdateCrossReferences()) {
        referenceUpdater.update(ctx);
        for (Runnable run : ctx.getModifications()) {
            run.run();
        }
    }
    ChangeDescription recording = recorder.endRecording();
    if (recording != null) {
        List<IResourceSnapshot> snapshots = Collections.singletonList(snapshot);
        ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, snapshots, recording);
        ResourceRecording recordedResource = tree.getRecordedResource(resource);
        partialSerializer.serializeChanges(recordedResource, document);
    }
    List<IUpdatableReference> updatableReferences = ctx.getUpdatableReferences();
    for (IUpdatableReference upd : updatableReferences) {
        referenceUpdater.updateReference(document, upd);
    }
    ITextRegionAccessDiff rewritten = document.create();
    List<ITextReplacement> rep = formatter.format(rewritten);
    URI oldUri = snapshot.getURI();
    TextDocumentChange change = new TextDocumentChange(rewritten, oldUri, rep);
    changeAcceptor.accept(change);
}
Also used : ResourceRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording) ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ResourceSetRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceSetRecording) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot) ChangeDescription(org.eclipse.emf.ecore.change.ChangeDescription)

Example 4 with IResourceSnapshot

use of org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot in project xtext-core by eclipse.

the class RelatedResourcesProvider method getRelatedResources.

public List<RelatedResource> getRelatedResources(Collection<IResourceSnapshot> snapshots) {
    Map<URI, RelatedResource> result = Maps.newLinkedHashMap();
    for (IResourceSnapshot res : snapshots) {
        for (IEObjectSnapshot obj : res.getObjects().values()) {
            for (IReferenceSnapshot ref : obj.getIncomingReferences()) {
                URI source = ref.getSourceEObjectUri().trimFragment();
                RelatedResource related = result.get(source);
                if (related == null) {
                    related = new RelatedResource(source);
                    result.put(source, related);
                }
                related.outgoingReferences.add(ref);
            }
        }
    }
    Set<URI> added = Sets.newHashSet();
    Set<URI> removed = Sets.newHashSet();
    for (IResourceSnapshot res : snapshots) {
        URI oldUri = res.getURI();
        URI newUri = res.getResource().getURI();
        if (!oldUri.equals(newUri)) {
            added.add(newUri);
            removed.add(oldUri);
        } else {
            result.remove(newUri);
        }
    }
    removed.removeAll(added);
    for (URI uri : removed) {
        result.remove(uri);
    }
    return ImmutableList.copyOf(result.values());
}
Also used : IEObjectSnapshot(org.eclipse.xtext.ide.serializer.hooks.IEObjectSnapshot) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot) URI(org.eclipse.emf.common.util.URI) IReferenceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IReferenceSnapshot)

Example 5 with IResourceSnapshot

use of org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot in project xtext-core by eclipse.

the class ChangeSerializer method endRecordChanges.

protected void endRecordChanges(IAcceptor<IEmfResourceChange> changeAcceptor) {
    if (updaters.isEmpty()) {
        return;
    }
    List<IResourceSnapshot> snapshots = getSnapshots();
    Deltas deltas = deltaProvider.getDelta(this, snapshots);
    List<ResourceUpdater> updaters = Lists.newArrayList(this.updaters.values());
    if (updateRelatedFiles && updateCrossReferences) {
        List<RelatedResource> related = relatedResourcesProvider.getRelatedResources(deltas.getSnapshots());
        for (RelatedResource ref : related) {
            RelatedResourceUpdater updater = createResourceUpdater(ref);
            updaters.add(updater);
        }
    }
    monitor.beginTask("Creating Text Changes...", updaters.size());
    for (ResourceUpdater updater : updaters) {
        updater.applyChange(deltas, changeAcceptor);
        monitor.worked(1);
        checkCanceled();
    }
    for (ResourceUpdater updater : updaters) {
        updater.unload();
    }
}
Also used : Deltas(org.eclipse.xtext.ide.serializer.impl.EObjectDescriptionDeltaProvider.Deltas) RelatedResource(org.eclipse.xtext.ide.serializer.impl.RelatedResourcesProvider.RelatedResource) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot)

Aggregations

IResourceSnapshot (org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot)5 URI (org.eclipse.emf.common.util.URI)2 EObject (org.eclipse.emf.ecore.EObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 IEObjectSnapshot (org.eclipse.xtext.ide.serializer.hooks.IEObjectSnapshot)2 EList (org.eclipse.emf.common.util.EList)1 ChangeDescription (org.eclipse.emf.ecore.change.ChangeDescription)1 FeatureChange (org.eclipse.emf.ecore.change.FeatureChange)1 ResourceChange (org.eclipse.emf.ecore.change.ResourceChange)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ITextRegionAccessDiff (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff)1 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)1 IEObjectDescriptionProvider (org.eclipse.xtext.ide.serializer.hooks.IEObjectDescriptionProvider)1 IReferenceSnapshot (org.eclipse.xtext.ide.serializer.hooks.IReferenceSnapshot)1 IUpdatableReference (org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference)1 ResourceRecording (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording)1 ResourceSetRecording (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceSetRecording)1 Deltas (org.eclipse.xtext.ide.serializer.impl.EObjectDescriptionDeltaProvider.Deltas)1 RelatedResource (org.eclipse.xtext.ide.serializer.impl.RelatedResourcesProvider.RelatedResource)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1