Search in sources :

Example 21 with IHiddenRegion

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

the class StringBasedTextRegionAccessDiffAppender method updateEObjectRegions.

protected void updateEObjectRegions() {
    ISemanticRegion current = this.last.getPreviousSemanticRegion();
    while (true) {
        ISemanticRegion sem = (ISemanticRegion) current;
        EObject eobj = sem.getSemanticElement();
        IHiddenRegion nextHiddenRegion = sem.getNextHiddenRegion();
        while (eobj != null) {
            AbstractEObjectRegion eobjRegion = getOrCreateEObjectRegion(eobj, null);
            if (eobjRegion.getNextHiddenRegion() != null) {
                break;
            }
            eobjRegion.setTrailingHiddenRegion(nextHiddenRegion);
            eobj = eobj.eContainer();
        }
        ISemanticRegion prev = sem.getPreviousSemanticRegion();
        if (prev == null) {
            break;
        } else {
            current = prev;
        }
    }
    while (true) {
        ISemanticRegion sem = (ISemanticRegion) current;
        EObject eobj = sem.getSemanticElement();
        IHiddenRegion previousHiddenRegion = sem.getPreviousHiddenRegion();
        while (eobj != null) {
            AbstractEObjectRegion eobjRegion = result.regionForEObject(eobj);
            if (eobjRegion.getPreviousHiddenRegion() != null) {
                break;
            }
            eobjRegion.setLeadingHiddenRegion(previousHiddenRegion);
            eobj = eobj.eContainer();
        }
        ISemanticRegion next = sem.getNextSemanticRegion();
        if (next == null) {
            break;
        } else {
            current = next;
        }
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 22 with IHiddenRegion

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

the class StringBasedTextRegionAccessDiffAppender method finish.

public StringBasedTextRegionAccessDiff finish() {
    if (this.last instanceof ISemanticRegion) {
        appendHiddenRegion(false);
    }
    updateEObjectRegions();
    if (diffFirstOriginal != null && diffFirstCopy != null && diffFirstCopy != null) {
        IHiddenRegion orig = result.getOriginalTextRegionAccess().regionForRootEObject().getNextHiddenRegion();
        result.append(new SequentialRegionDiff(diffFirstOriginal, orig, diffFirstCopy, this.last));
        diffFirstCopy = null;
        diffFirstOriginal = null;
    }
    return result;
}
Also used : IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 23 with IHiddenRegion

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

the class AbstractFormatter2 method postProcess.

protected List<ITextReplacement> postProcess(IFormattableDocument document, List<ITextReplacement> replacements) {
    List<ITextSegment> expected = Lists.newArrayList();
    IHiddenRegion current = getTextRegionAccess().regionForRootEObject().getPreviousHiddenRegion();
    while (current != null) {
        if (current.isUndefined() && isInRequestedRange(current.getOffset(), current.getEndOffset()))
            expected.addAll(current.getMergedSpaces());
        current = current.getNextHiddenRegion();
    }
    if (expected.isEmpty())
        return replacements;
    List<ITextSegment> missing = TextRegions.difference(expected, replacements);
    if (missing.isEmpty())
        return replacements;
    List<ITextReplacement> result = Lists.newArrayList(replacements);
    for (ITextSegment seg : missing) {
        IHiddenRegion h = null;
        if (seg instanceof IHiddenRegion)
            h = (IHiddenRegion) seg;
        if (seg instanceof IHiddenRegionPart)
            h = ((IHiddenRegionPart) seg).getHiddenRegion();
        if (h != null && (h.getNextSemanticRegion() == null || h.getPreviousSemanticRegion() == null))
            result.add(seg.replaceWith(""));
        else
            result.add(seg.replaceWith(" "));
    }
    return result;
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 24 with IHiddenRegion

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

the class AbstractFormatter2 method isInRequestedRange.

protected boolean isInRequestedRange(EObject obj) {
    Collection<ITextRegion> regions = request.getRegions();
    if (regions.isEmpty())
        return true;
    ITextRegionAccess access = request.getTextRegionAccess();
    IEObjectRegion objRegion = access.regionForEObject(obj);
    if (objRegion == null)
        return false;
    IHiddenRegion previousHidden = objRegion.getPreviousHiddenRegion();
    IHiddenRegion nextHidden = objRegion.getNextHiddenRegion();
    int objOffset = previousHidden != null ? previousHidden.getOffset() : 0;
    int objEnd = nextHidden != null ? nextHidden.getEndOffset() : access.regionForRootEObject().getEndOffset();
    for (ITextRegion region : regions) {
        int regionOffset = region.getOffset();
        int regionEnd = regionOffset + region.getLength();
        if (regionOffset <= objEnd && regionEnd >= objOffset)
            return true;
    }
    return false;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Example 25 with IHiddenRegion

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

IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)21 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)8 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)6 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)4 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)4 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)4 EObject (org.eclipse.emf.ecore.EObject)3 ISequentialRegion (org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion)3 AbstractElement (org.eclipse.xtext.AbstractElement)2 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)2 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)2 IAstRegion (org.eclipse.xtext.formatting2.regionaccess.IAstRegion)2 ImmutableList (com.google.common.collect.ImmutableList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 EList (org.eclipse.emf.common.util.EList)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 ChangeKind (org.eclipse.emf.ecore.change.ChangeKind)1 ListChange (org.eclipse.emf.ecore.change.ListChange)1 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)1