Search in sources :

Example 21 with IEObjectRegion

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

the class TextRegionAccessToString method appendRegions.

protected void appendRegions(TextRegionListToString result, List<ITextSegment> list, DiffColumn diff, boolean isDiffAppendix) {
    Multimap<IHiddenRegion, IEObjectRegion> hiddens = LinkedListMultimap.create();
    List<String> errors = Lists.newArrayList();
    ITextRegionAccess access = list.get(0).getTextRegionAccess();
    TreeIterator<EObject> all = EcoreUtil2.eAll(access.regionForRootEObject().getSemanticElement());
    while (all.hasNext()) {
        EObject element = all.next();
        IEObjectRegion obj = access.regionForEObject(element);
        if (obj == null)
            continue;
        IHiddenRegion previous = obj.getPreviousHiddenRegion();
        IHiddenRegion next = obj.getNextHiddenRegion();
        if (previous == null)
            errors.add("ERROR: " + EmfFormatter.objPath(element) + " has no leading HiddenRegion.");
        else
            hiddens.put(previous, obj);
        if (previous != next) {
            if (next == null)
                errors.add("ERROR: " + EmfFormatter.objPath(element) + " has no trailing HiddenRegion.");
            else
                hiddens.put(next, obj);
        }
    }
    for (String error : errors) result.add(error, false);
    int indentation = 0, min = 0;
    for (ITextSegment region : list) {
        if (region instanceof IHiddenRegion) {
            Collection<IEObjectRegion> found = hiddens.get((IHiddenRegion) region);
            for (IEObjectRegion obj : found) {
                boolean p = region.equals(obj.getNextHiddenRegion());
                boolean n = region.equals(obj.getPreviousHiddenRegion());
                if (p)
                    indentation--;
                else if (n)
                    indentation++;
                if (indentation < min)
                    min = indentation;
            }
        }
    }
    indentation = min < 0 ? min * -1 : 0;
    for (ITextSegment region : list) {
        List<IEObjectRegion> previous = Lists.newArrayList();
        List<IEObjectRegion> next = Lists.newArrayList();
        List<String> middle = Lists.newArrayList(toString(region));
        if (region instanceof IHiddenRegion) {
            Collection<IEObjectRegion> found = hiddens.get((IHiddenRegion) region);
            for (IEObjectRegion obj : found) {
                boolean p = region.equals(obj.getNextHiddenRegion());
                boolean n = region.equals(obj.getPreviousHiddenRegion());
                if (p && n)
                    middle.add(EMPTY_TITLE + "Semantic " + toString(obj));
                else if (p)
                    previous.add(obj);
                else if (n)
                    next.add(obj);
            }
            Collections.sort(previous, AstRegionComparator.CHILDREN_FIRST);
            Collections.sort(next, AstRegionComparator.CONTAINER_FIRST);
        }
        if (!isDiffAppendix) {
            for (IEObjectRegion obj : previous) {
                indentation--;
                result.add(diff.empty + indent(indentation) + EOBJECT_END_PADDED + toString(obj));
            }
        }
        String indent = indent(indentation);
        result.add(region, diff.get(region) + indent + Joiner.on("\n").join(middle).replace("\n", "\n" + indent));
        if (!isDiffAppendix) {
            for (IEObjectRegion obj : next) {
                result.add(diff.empty + indent(indentation) + EOBJECT_BEGIN_PADDED + toString(obj));
                indentation++;
            }
        }
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) EObject(org.eclipse.emf.ecore.EObject) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

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