Search in sources :

Example 1 with IEObjectRegion

use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion in project xtext-xtend by eclipse.

the class RichStringToLineModel method announceNextLiteral.

@Override
public void announceNextLiteral(final RichStringLiteral object) {
    super.announceNextLiteral(object);
    if (((this.lastLiteralEndOffset > 0) && (this.contentStartOffset < 0))) {
        this.contentStartOffset = this.lastLiteralEndOffset;
    }
    IEObjectRegion _regionForEObject = this.nodeModelAccess.regionForEObject(object);
    ISemanticRegionsFinder _regionFor = null;
    if (_regionForEObject != null) {
        _regionFor = _regionForEObject.getRegionFor();
    }
    ISemanticRegion _feature = null;
    if (_regionFor != null) {
        _feature = _regionFor.feature(XbasePackage.Literals.XSTRING_LITERAL__VALUE);
    }
    final ISemanticRegion node = _feature;
    if ((node != null)) {
        int _offset = node.getOffset();
        int _literalPrefixLenght = this.literalPrefixLenght(node);
        int _plus = (_offset + _literalPrefixLenght);
        this.offset = _plus;
        int _endOffset = node.getEndOffset();
        int _literalPostfixLenght = this.literalPostfixLenght(node);
        int _minus = (_endOffset - _literalPostfixLenght);
        this.lastLiteralEndOffset = _minus;
    }
    this.content = true;
}
Also used : IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) ISemanticRegionsFinder(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder)

Example 2 with IEObjectRegion

use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion 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 3 with IEObjectRegion

use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion 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 4 with IEObjectRegion

use of org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion 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 5 with IEObjectRegion

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

the class PartialSerializer method serializeChanges.

public void serializeChanges(ResourceRecording changes, ITextRegionDiffBuilder result) {
    assertLanguage(changes);
    SerializationContextMap<IConstraint> constraints = constraintProvider.getConstraints(grammar.getGrammar());
    List<EObjectChange> rootChanges = collectRootChanges(changes.getRootEObjectRecordings());
    List<SerializationStrategy> strategies = Lists.newArrayList();
    while (!rootChanges.isEmpty()) {
        List<EObjectChange> nextRoots = Lists.newArrayList();
        for (EObjectChange change : rootChanges) {
            EObject obj = change.getEObject();
            IEObjectRegion originalEObjectRegion = result.getOriginalTextRegionAccess().regionForEObject(obj);
            ISerializationContext modified = getSerializationContext(obj);
            if (originalEObjectRegion == null) {
                strategies.add(new SerializeRecursiveStrategy(originalEObjectRegion, obj, modified));
                continue;
            }
            ISerializationContext original = getSerializationContext(originalEObjectRegion);
            if (!original.equals(modified)) {
                strategies.add(new SerializeRecursiveStrategy(originalEObjectRegion, obj, modified));
                // TODO: need to serialize container sometimes?
                continue;
            }
            IConstraint constraint = constraints.get(modified);
            List<SerializationStrategy> features = trySerializeIndividualFeatures(change, originalEObjectRegion, modified, constraint);
            if (features != null) {
                strategies.addAll(features);
                nextRoots.addAll(change.getChildren());
            } else {
                strategies.add(new SerializeRecursiveStrategy(originalEObjectRegion, obj, modified));
            }
        }
        rootChanges = collectRootChanges(nextRoots);
    }
    for (SerializationStrategy strategy : strategies) {
        strategy.serialize(result);
    }
}
Also used : IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) EObjectChange(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.EObjectChange)

Aggregations

IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)16 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)9 EObject (org.eclipse.emf.ecore.EObject)8 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)7 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)5 EReference (org.eclipse.emf.ecore.EReference)4 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 IAstRegion (org.eclipse.xtext.formatting2.regionaccess.IAstRegion)3 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)3 Test (org.junit.Test)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 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)2 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)2 Delegate (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Delegate)2