-
Notifications
You must be signed in to change notification settings - Fork 316
/
ReferenceUpdater.java
258 lines (237 loc) · 9.15 KB
/
ReferenceUpdater.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*******************************************************************************
* Copyright (c) 2017 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.serializer.impl;
import java.util.List;
import java.util.Objects;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.CrossReference;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.conversion.IValueConverterService;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion;
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion;
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder;
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess;
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder;
import org.eclipse.xtext.ide.serializer.hooks.IReferenceSnapshot;
import org.eclipse.xtext.ide.serializer.hooks.IReferenceUpdater;
import org.eclipse.xtext.ide.serializer.hooks.IReferenceUpdaterContext;
import org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference;
import org.eclipse.xtext.ide.serializer.impl.EObjectDescriptionDeltaProvider.Delta;
import org.eclipse.xtext.ide.serializer.impl.EObjectDescriptionDeltaProvider.Deltas;
import org.eclipse.xtext.ide.serializer.impl.RelatedResourcesProvider.RelatedResource;
import org.eclipse.xtext.linking.impl.LinkingHelper;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.IScopeProvider;
import org.eclipse.xtext.serializer.tokens.SerializerScopeProviderBinding;
import com.google.inject.Inject;
/**
* @author Moritz Eysholdt - Initial contribution and API
*
* @since 2.13
*/
public class ReferenceUpdater implements IReferenceUpdater {
@Inject
private IQualifiedNameConverter converter;
@Inject
private LinkingHelper linkingHelper;
@Inject
private IQualifiedNameConverter nameConverter;
@Inject
@SerializerScopeProviderBinding
private IScopeProvider scopeProvider;
@Inject
private IValueConverterService valueConverter;
protected boolean containsReferenceText(Delta delta, QualifiedName exp) {
DESC: for (IEObjectDescription desc : delta.getDescriptions()) {
QualifiedName cand = desc.getQualifiedName();
if (cand.getSegmentCount() >= exp.getSegmentCount()) {
for (int i = 1; i <= exp.getSegmentCount(); i++) {
String expSeg = exp.getSegment(exp.getSegmentCount() - i);
String candSeg = cand.getSegment(cand.getSegmentCount() - i);
if (!expSeg.equals(candSeg)) {
continue DESC;
}
}
}
return true;
}
return false;
}
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;
}
public Delta findContainingDelta(Deltas deltas, EObject obj) {
EObject current = obj;
while (current != null) {
Delta delta = deltas.getDelta(current);
if (delta != null && delta.hasSimpleNameOrUserdataChanged()) {
return delta;
}
current = current.eContainer();
}
return null;
}
protected String findValidName(IUpdatableReference updatable, IScope scope) {
Iterable<IEObjectDescription> elements = scope.getElements(updatable.getTargetEObject());
String ruleName = linkingHelper.getRuleNameFrom(updatable.getCrossReference());
for (IEObjectDescription desc : elements) {
try {
String unconverted = nameConverter.toString(desc.getName());
String string = valueConverter.toString(unconverted, ruleName);
return string;
} catch (ValueConverterException e) {
// do nothing
}
}
return null;
}
protected QualifiedName getQualifiedName(IUpdatableReference updatable) {
String text = updatable.getReferenceRegion().getText();
String ruleName = linkingHelper.getRuleNameFrom(updatable.getCrossReference());
try {
Object converted = valueConverter.toValue(text, ruleName, null);
if (converted != null) {
return converter.toQualifiedName(converted.toString());
}
} catch (ValueConverterException e) {
// do nothing
}
return null;
}
protected ISemanticRegion getRegion(ITextRegionAccess access, IReferenceSnapshot ref) {
XtextResource resource = access.getResource();
URI objectUri = ref.getSourceEObjectUri();
if (!resource.getURI().equals(objectUri.trimFragment())) {
return null;
}
EObject object = resource.getEObject(objectUri.fragment());
if (object == null) {
return null;
}
ISemanticRegionsFinder finder = access.getExtensions().regionFor(object);
int index = ref.getIndexInList();
if (index < 0) {
return finder.feature(ref.getEReference());
} else {
List<ISemanticRegion> list = finder.features(ref.getEReference());
if (list != null && index < list.size()) {
return list.get(index);
}
}
return null;
}
@Override
public boolean isAffected(Deltas deltas, RelatedResource resource) {
for (IReferenceSnapshot ref : resource.outgoingReferences) {
Delta delta = deltas.getDelta(ref.getTarget().getObject());
if (delta != null) {
return true;
}
}
return false;
}
protected boolean needsUpdating(IReferenceUpdaterContext context, IUpdatableReference ref) {
QualifiedName fqn = getQualifiedName(ref);
if (fqn == null) {
return false;
}
EObject target = ref.getTargetEObject();
Deltas deltas = context.getEObjectDescriptionDeltas();
Delta delta = deltas.getDelta(target);
if (delta != null && !containsReferenceText(delta, fqn)) {
return true;
}
Delta targetDelta = findContainingDelta(deltas, target);
if (targetDelta != null && targetDelta.getObject() == target) {
return true;
}
Delta sourceDelta = findContainingDelta(deltas, ref.getSourceEObject());
return !Objects.equals(sourceDelta, targetDelta);
}
@Override
public void update(IReferenceUpdaterContext context) {
RelatedResource relatedResource = context.getRelatedResource();
if (relatedResource == null) {
updateAllReferences(context);
} else {
updateExternalReferences(context, relatedResource);
}
}
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();
}
}
protected void updateExternalReferences(IReferenceUpdaterContext context, RelatedResource relatedResource) {
ITextRegionAccess document = context.getModifyableDocument().getOriginalTextRegionAccess();
for (IReferenceSnapshot ref : relatedResource.outgoingReferences) {
ISemanticRegion region = getRegion(document, ref);
if (region != null) {
IUpdatableReference updatable = createUpdatableReference(region);
if (updatable != null && needsUpdating(context, updatable)) {
context.updateReference(updatable);
}
}
}
}
@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);
}
}
}