Search in sources :

Example 1 with IUpdatableReference

use of org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference 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);
    }
    recorder.dispose();
    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 2 with IUpdatableReference

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

the class ReferenceUpdater method updateReference.

@Override
public void updateReference(ITextRegionDiffBuilder rewriter, IUpdatableReference upd) {
    IUpdatableReference updatable = upd;
    if (rewriter.isModified(updatable.getReferenceRegion())) {
        return;
    }
    IScope scope = scopeProvider.getScope(updatable.getSourceEObject(), updatable.getEReference());
    ISemanticRegion region = updatable.getReferenceRegion();
    QualifiedName oldName = nameConverter.toQualifiedName(region.getText());
    IEObjectDescription oldDesc = scope.getSingleElement(oldName);
    if (oldDesc != null && oldDesc.getEObjectOrProxy() == updatable.getTargetEObject()) {
        return;
    }
    String newName = findValidName(updatable, scope);
    if (newName != null) {
        rewriter.replace(region, newName);
    }
}
Also used : IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IScope(org.eclipse.xtext.scoping.IScope) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 3 with IUpdatableReference

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

the class ReferenceUpdater method createUpdatableReference.

protected IUpdatableReference createUpdatableReference(ISemanticRegion current) {
    EReference ref = (EReference) current.getContainingFeature();
    CrossReference crossRef = GrammarUtil.containingCrossReference(current.getGrammarElement());
    EObject owner = current.getContainingRegion().getSemanticElement();
    Object value = owner.eGet(ref);
    if (value instanceof List<?>) {
        List<?> targets = (List<?>) value;
        int i = current.getIndexInContainingFeature();
        EObject t = (EObject) targets.get(i);
        if (t != null && !t.eIsProxy()) {
            return new UpdatableReference(owner, ref, i, t, crossRef, current);
        }
    } else if (value instanceof EObject) {
        EObject t = (EObject) value;
        if (!t.eIsProxy()) {
            return new UpdatableReference(owner, ref, -1, t, crossRef, current);
        }
    } else {
        throw new IllegalStateException();
    }
    return null;
}
Also used : IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 4 with IUpdatableReference

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

the class ReferenceUpdater method updateAllReferences.

protected void updateAllReferences(IReferenceUpdaterContext context) {
    IEObjectRegion root = context.getModifyableDocument().getOriginalTextRegionAccess().regionForRootEObject();
    ISemanticRegion current = root.getPreviousHiddenRegion().getNextSemanticRegion();
    while (current != null) {
        EStructuralFeature feature = current.getContainingFeature();
        if (feature instanceof EReference && !((EReference) feature).isContainment()) {
            IUpdatableReference updatable = createUpdatableReference(current);
            if (updatable != null && needsUpdating(context, updatable)) {
                context.updateReference(updatable);
            }
        }
        current = current.getNextSemanticRegion();
    }
}
Also used : IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) EReference(org.eclipse.emf.ecore.EReference)

Example 5 with IUpdatableReference

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

the class RelatedXtextResourceUpdater method applyChange.

@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
    XtextResource res = (XtextResource) lifecycleManager.openAndApplyReferences(getResourceSet(), getResource());
    if (!referenceUpdater.isAffected(deltas, getResource())) {
        return;
    }
    ITextRegionAccess base = textRegionBuilderProvider.get().forNodeModel(res).create();
    ITextRegionDiffBuilder rewriter = new StringBasedTextRegionAccessDiffBuilder(base);
    ReferenceUpdaterContext context = new ReferenceUpdaterContext(deltas, rewriter, getResource());
    referenceUpdater.update(context);
    if (!context.getModifications().isEmpty()) {
        ChangeRecorder rec = createChangeRecorder(res);
        try {
            for (Runnable run : context.getModifications()) {
                run.run();
            }
            ChangeDescription recording = rec.endRecording();
            ResourceSet rs = res.getResourceSet();
            ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, Collections.emptyList(), recording);
            ResourceRecording recordedResource = tree.getRecordedResource(res);
            if (recordedResource != null) {
                serializer.serializeChanges(recordedResource, rewriter);
            }
        } finally {
            rec.dispose();
        }
    }
    for (IUpdatableReference upd : context.getUpdatableReferences()) {
        referenceUpdater.updateReference(rewriter, upd);
    }
    ITextRegionAccessDiff rewritten = rewriter.create();
    List<ITextReplacement> rep = formatter.format(rewritten);
    TextDocumentChange change = new TextDocumentChange(rewritten, getResource().getUri(), rep);
    changeAcceptor.accept(change);
}
Also used : ResourceRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording) ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringBasedTextRegionAccessDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiffBuilder) 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) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ChangeRecorder(org.eclipse.emf.ecore.change.util.ChangeRecorder) ChangeDescription(org.eclipse.emf.ecore.change.ChangeDescription) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)

Aggregations

IUpdatableReference (org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference)6 EReference (org.eclipse.emf.ecore.EReference)2 ChangeDescription (org.eclipse.emf.ecore.change.ChangeDescription)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)2 ITextRegionAccessDiff (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff)2 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)2 ResourceRecording (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording)2 ResourceSetRecording (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceSetRecording)2 QualifiedName (org.eclipse.xtext.naming.QualifiedName)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 ChangeRecorder (org.eclipse.emf.ecore.change.util.ChangeRecorder)1 Resource (org.eclipse.emf.ecore.resource.Resource)1