Search in sources :

Example 1 with IAstRegion

use of org.eclipse.xtext.formatting2.regionaccess.IAstRegion in project xtext-core by eclipse.

the class InsertionPointFinder method findInsertionPoint.

public IHiddenRegion findInsertionPoint(ISerializationContext ctx, IEObjectRegion obj, AbstractElement ins) {
    ISerState insertionState = findState(ctx, ins);
    Set<AbstractElement> followers = collectAdjacent(insertionState, s -> s.getFollowers());
    Set<AbstractElement> precendents = collectAdjacent(insertionState, s -> s.getPrecedents());
    List<IAstRegion> regions = Lists.newArrayList(obj.getAstRegions());
    if (regions.isEmpty()) {
        return obj.getPreviousHiddenRegion();
    }
    if (followers.contains(regions.get(0).getGrammarElement())) {
        return obj.getPreviousHiddenRegion();
    }
    if (precendents.contains(regions.get(regions.size() - 1).getGrammarElement())) {
        return obj.getNextHiddenRegion();
    }
    for (int i = 0; i < regions.size() - 1; i++) {
        IAstRegion leading = regions.get(i);
        IAstRegion trailing = regions.get(i + 1);
        if (precendents.contains(leading.getGrammarElement()) && followers.contains(trailing.getGrammarElement())) {
            return leading.getNextHiddenRegion();
        }
    }
    return null;
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) AbstractElement(org.eclipse.xtext.AbstractElement) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion)

Example 2 with IAstRegion

use of org.eclipse.xtext.formatting2.regionaccess.IAstRegion in project xtext-core by eclipse.

the class PartialSerializer method trySerializeSingleValue.

protected List<SerializationStrategy> trySerializeSingleValue(EObject owner, FeatureChange change, IEObjectRegion ownerRegion, IConstraint constraint) {
    List<SerializationStrategy> result = Lists.newArrayList();
    EStructuralFeature feature = change.getFeature();
    IFeatureInfo featureInfo = constraint.getFeatures()[owner.eClass().getFeatureID(feature)];
    List<IConstraintElement> assignments = featureInfo.getAssignments();
    if (assignments.size() != 1) {
        return null;
    }
    boolean optional = assignments.get(0).isOptional();
    IAstRegion featureRegion = findRegion(ownerRegion, change);
    ValueTransient valueTransient = transientValues.isValueTransient(owner, feature);
    switch(valueTransient) {
        case YES:
            if (featureRegion != null) {
                if (optional) {
                    result.add(new DeleteRegionStrategy(featureRegion));
                } else {
                    return null;
                }
            }
            break;
        case PREFERABLY:
            if (featureRegion != null) {
                if (optional) {
                    result.add(new DeleteRegionStrategy(featureRegion));
                } else {
                    SerializationStrategy update = updateSingleValue(owner, feature, featureRegion);
                    if (update != null) {
                        result.add(update);
                    } else {
                        return null;
                    }
                }
            }
            break;
        case NO:
            if (featureRegion == null) {
                return null;
            }
            SerializationStrategy update = updateSingleValue(owner, feature, featureRegion);
            if (update != null) {
                result.add(update);
            } else {
                return null;
            }
            break;
    }
    return result;
}
Also used : IConstraintElement(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement) ValueTransient(org.eclipse.xtext.serializer.sequencer.ITransientValueService.ValueTransient) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IFeatureInfo(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo)

Example 3 with IAstRegion

use of org.eclipse.xtext.formatting2.regionaccess.IAstRegion in project xtext-core by eclipse.

the class PartialSerializer method findRegion.

protected IAstRegion findRegion(IEObjectRegion owner, FeatureChange change) {
    EStructuralFeature feature = change.getFeature();
    if (feature instanceof EReference && ((EReference) feature).isContainment()) {
        ITextRegionAccess access = owner.getTextRegionAccess();
        EObject oldValue = change.getReferenceValue();
        if (oldValue != null) {
            return access.regionForEObject(oldValue);
        }
        EObject value = (EObject) owner.getSemanticElement().eGet(feature);
        if (value != null) {
            return access.regionForEObject(value);
        }
        return null;
    } else {
        return owner.getRegionFor().feature(feature);
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EReference(org.eclipse.emf.ecore.EReference)

Example 4 with IAstRegion

use of org.eclipse.xtext.formatting2.regionaccess.IAstRegion 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;
}
Also used : EAttribute(org.eclipse.emf.ecore.EAttribute) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) EObject(org.eclipse.emf.ecore.EObject) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) RuleCall(org.eclipse.xtext.RuleCall) EReference(org.eclipse.emf.ecore.EReference)

Example 5 with IAstRegion

use of org.eclipse.xtext.formatting2.regionaccess.IAstRegion 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;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) IFeatureInfo(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo) IConstraintElement(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement) ISequentialRegion(org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) EList(org.eclipse.emf.common.util.EList) ListChange(org.eclipse.emf.ecore.change.ListChange) ChangeKind(org.eclipse.emf.ecore.change.ChangeKind)

Aggregations

EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)5 EObject (org.eclipse.emf.ecore.EObject)4 IAstRegion (org.eclipse.xtext.formatting2.regionaccess.IAstRegion)4 EReference (org.eclipse.emf.ecore.EReference)3 ImmutableList (com.google.common.collect.ImmutableList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 EList (org.eclipse.emf.common.util.EList)2 ListChange (org.eclipse.emf.ecore.change.ListChange)2 AbstractElement (org.eclipse.xtext.AbstractElement)2 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)2 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)2 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)2 IConstraintElement (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement)2 IFeatureInfo (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo)2 EAttribute (org.eclipse.emf.ecore.EAttribute)1 EClass (org.eclipse.emf.ecore.EClass)1 ChangeKind (org.eclipse.emf.ecore.change.ChangeKind)1 CrossReference (org.eclipse.xtext.CrossReference)1 RuleCall (org.eclipse.xtext.RuleCall)1