-
Notifications
You must be signed in to change notification settings - Fork 316
/
PartialSerializationTestLanguageReferenceUpdater.java
83 lines (78 loc) · 3.21 KB
/
PartialSerializationTestLanguageReferenceUpdater.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.ide.tests.testlanguage.ide.serializer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.ide.serializer.hooks.IReferenceUpdaterContext;
import org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference;
import org.eclipse.xtext.ide.serializer.impl.EObjectDescriptionDeltaProvider;
import org.eclipse.xtext.ide.serializer.impl.ReferenceUpdater;
import org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Import;
import org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node;
import org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.PartialSerializationTestLanguageFactory;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
/**
* @author Moritz Eysholdt - Initial contribution and API
*/
public class PartialSerializationTestLanguageReferenceUpdater extends ReferenceUpdater {
@Inject
private IQualifiedNameConverter converter;
@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.equals(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);
}
});
}
}