use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion in project xtext-core by eclipse.
the class PartialSerializer method updateSingleValue.
protected SerializationStrategy updateSingleValue(EObject object, EStructuralFeature feature, IAstRegion region) {
Preconditions.checkArgument(!feature.isMany());
Object value = object.eGet(feature);
EObject grammarElement = region.getGrammarElement();
if (feature instanceof EAttribute) {
if (grammarElement instanceof RuleCall) {
RuleCall rc = (RuleCall) grammarElement;
String newValue = valueSerializer.serializeAssignedValue(object, rc, value, null, errorAcceptor);
if (newValue != null) {
return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
}
}
return null;
} else if (feature instanceof EReference) {
if (((EReference) feature).isContainment()) {
IEObjectRegion reg = (IEObjectRegion) region;
EObject newEObject = (EObject) object.eGet(feature);
ISerializationContext newContext = getSerializationContext(newEObject);
ISerializationContext oldContext = getSerializationContext(reg);
if (!oldContext.equals(newContext)) {
return null;
}
return new SerializeRecursiveStrategy(reg, newEObject, newContext);
} else {
CrossReference cr = GrammarUtil.containingCrossReference(grammarElement);
if (cr != null) {
EObject target = (EObject) value;
String newValue = crossRefSerializer.serializeCrossRef(object, cr, target, null, errorAcceptor);
if (newValue != null) {
return new ReplaceRegionStrategy((ISemanticRegion) region, newValue);
}
}
}
return null;
}
return null;
}
use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion in project xtext-core by eclipse.
the class PartialSerializer method trySerializeMultiValue.
protected List<SerializationStrategy> trySerializeMultiValue(EObject owner, FeatureChange change, IEObjectRegion ownerRegion, IConstraint constraint) {
EStructuralFeature feature = change.getFeature();
List<SerializationStrategy> result = Lists.newArrayList();
IFeatureInfo featureInfo = constraint.getFeatures()[owner.eClass().getFeatureID(feature)];
List<IConstraintElement> assignments = featureInfo.getAssignments();
if (assignments.size() != 1) {
return null;
}
IConstraintElement assignment = assignments.get(0);
if (!assignment.isMany()) {
return null;
}
List<IAstRegion> originals = findRegions(ownerRegion, change);
EList<ListChange> listChanges = change.getListChanges();
if (listChanges.isEmpty() && originals.isEmpty()) {
ISerializationContext ctx = getSerializationContext(owner);
AbstractElement ins = assignment.getGrammarElement();
IHiddenRegion insertAt = insertionPointFinder.findInsertionPoint(ctx, ownerRegion, ins);
if (insertAt == null) {
return null;
}
for (Object value : (List<?>) owner.eGet(feature)) {
EObject obj = (EObject) value;
ISerializationContext context = getSerializationContext(obj);
result.add(new SerializeRecursiveStrategy(insertAt, obj, context));
}
return result;
}
// ListTransient listTransient = transientValues.isListTransient(owner, feature);
List<Object> modifying = Lists.newArrayList(((List<?>) owner.eGet(feature)));
for (ListChange lc : listChanges) {
ChangeKind kind = lc.getKind();
if (kind == ADD_LITERAL) {
IAstRegion region = originals.get(lc.getIndex());
result.add(new DeleteRegionStrategy(region));
} else if (kind == MOVE_LITERAL || kind == REMOVE_LITERAL) {
if (originals.isEmpty()) {
return null;
}
int index = lc.getIndex();
IHiddenRegion insertAt;
if (index >= originals.size()) {
insertAt = ((ISequentialRegion) originals.get(originals.size() - 1)).getNextHiddenRegion();
} else {
insertAt = ((ISequentialRegion) originals.get(index)).getPreviousHiddenRegion();
}
EObject value = (EObject) modifying.get(index);
modifying.remove(index);
if (kind == REMOVE_LITERAL) {
ISerializationContext context = getSerializationContext(value);
result.add(new SerializeRecursiveStrategy(insertAt, value, context));
} else if (kind == ChangeKind.MOVE_LITERAL) {
int moveToIndex = lc.getMoveToIndex();
IAstRegion source = originals.get(moveToIndex);
result.add(new DeleteRegionStrategy(source));
result.add(new InsertRegionStrategy(insertAt, source));
}
}
}
return result;
}
use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion in project xtext-core by eclipse.
the class PartialSerializer method findRegions.
protected List<IAstRegion> findRegions(IEObjectRegion owner, FeatureChange change) {
EStructuralFeature feature = change.getFeature();
if (feature instanceof EReference && ((EReference) feature).isContainment()) {
ITextRegionAccess access = owner.getTextRegionAccess();
Set<EObject> children = Sets.newHashSet();
for (ListChange lc : change.getListChanges()) {
children.addAll(lc.getReferenceValues());
}
for (Object obj : (List<?>) owner.getSemanticElement().eGet(feature)) {
children.add((EObject) obj);
}
List<IEObjectRegion> result = Lists.newArrayList();
for (EObject obj : children) {
IEObjectRegion region = access.regionForEObject(obj);
if (region != null) {
result.add(region);
}
}
Collections.sort(result, (a, b) -> a.getOffset() - b.getOffset());
return ImmutableList.copyOf(result);
} else {
return ImmutableList.copyOf(owner.getRegionFor().features(feature));
}
}
use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion 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();
}
}
use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion in project xtext-core by eclipse.
the class StringBasedTextRegionAccessDiffBuilder method replace.
@Override
public void replace(IHiddenRegion originalFirst, IHiddenRegion originalLast, ITextRegionAccess acc) {
checkOriginal(originalFirst);
checkOriginal(originalLast);
IEObjectRegion substituteRoot = acc.regionForRootEObject();
IHiddenRegion substituteFirst = substituteRoot.getPreviousHiddenRegion();
IHiddenRegion substituteLast = substituteRoot.getNextHiddenRegion();
replace(originalFirst, originalLast, substituteFirst, substituteLast);
}
Aggregations