Search in sources :

Example 6 with IUpdatableReference

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

the class PartialSerializationTestLanguageReferenceUpdater method update.

@Override
public void update(IReferenceUpdaterContext context) {
    super.update(context);
    Node node = IterableExtensions.head(Iterables.filter(context.getResource().getContents(), Node.class));
    if (node == null) {
        return;
    }
    Set<Import> toDelete = new HashSet<>();
    Set<QualifiedName> toAdd = new HashSet<>();
    Map<Import, QualifiedName> toChange = new HashMap<>();
    Map<QualifiedName, Import> imports = IterableExtensions.toMap(node.getImports(), (Import it) -> converter.toQualifiedName(it.getImportedNamespace()));
    for (IUpdatableReference target : context.getUpdatableReferences()) {
        EObjectDescriptionDeltaProvider.Delta delta = this.findContainingDelta(context.getEObjectDescriptionDeltas(), target.getTargetEObject());
        if (delta != null) {
            QualifiedName original = IterableExtensions.head(delta.getSnapshot().getDescriptions()).getQualifiedName();
            QualifiedName modified = IterableExtensions.head(delta.getDescriptions()).getQualifiedName();
            if (!Objects.equal(original, modified)) {
                Import imp = imports.get(original);
                if (imp != null) {
                    toChange.put(imp, modified);
                }
            }
        }
    }
    context.modifyModel(() -> {
        for (Import toDel : toDelete) {
            EcoreUtil.remove(toDel);
        }
        for (Map.Entry<Import, QualifiedName> toCh : toChange.entrySet()) {
            Import imp = toCh.getKey();
            QualifiedName name = toCh.getValue();
            imp.setImportedNamespace(converter.toString(name));
        }
        for (QualifiedName toA : toAdd) {
            Import newImport = PartialSerializationTestLanguageFactory.eINSTANCE.createImport();
            newImport.setImportedNamespace(converter.toString(toA));
            node.getImports().add(newImport);
        }
    });
}
Also used : Import(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Import) IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) HashMap(java.util.HashMap) Node(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node) QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObjectDescriptionDeltaProvider(org.eclipse.xtext.ide.serializer.impl.EObjectDescriptionDeltaProvider) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

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