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;
}
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;
}
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);
}
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());
}
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();
}
}
Aggregations