Search in sources :

Example 26 with ITextSegment

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

the class TextReplacerContext method addReplacement.

@Override
public void addReplacement(ITextReplacement replacement) {
    Preconditions.checkNotNull(replacer);
    ITextSegment replacerRegion = replacer.getRegion();
    FormatterRequest request = document.getRequest();
    if (!replacerRegion.contains(replacement)) {
        String frameTitle = replacer.getClass().getSimpleName();
        ITextSegment frameRegion = replacer.getRegion();
        String replacerTitle = replacement.getReplacementText();
        RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, (ITextSegment) replacement));
        request.getExceptionHandler().accept(exception);
        return;
    }
    if (!isInRequestedRange(replacement)) {
        return;
    }
    if (!isInUndefinedRegion(replacement)) {
        if (request.isFormatUndefinedHiddenRegionsOnly()) {
            return;
        }
        if (!request.allowIdentityEdits() && isIdentityEdit(replacement)) {
            return;
        }
    }
    try {
        replacements.add(replacement);
    } catch (ConflictingRegionsException e) {
        request.getExceptionHandler().accept(e);
    }
}
Also used : ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) String(java.lang.String) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 27 with ITextSegment

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

the class TextReplacerContext method toStringLocal.

protected String toStringLocal() {
    List<String> items = Lists.newArrayList();
    if (autowrap)
        items.add("autowrap");
    if (canAutowrap != null && canAutowrap >= 0)
        items.add("canAutowrap");
    if (replacer != null) {
        ITextSegment region = replacer.getRegion();
        items.add(format("replacer=[%d-%d-%s|%s]", region.getOffset(), region.getLength(), replacer.getClass().getSimpleName(), replacer.toString()));
    }
    if (replacements != null)
        for (ITextReplacement r : replacements) {
            String fmt = "replacement=[%d-%d|%s]";
            items.add(format(fmt, r.getOffset(), r.getLength(), r.getReplacementText()));
        }
    return Joiner.on("; ").join(items);
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) String(java.lang.String) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 28 with ITextSegment

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

the class AbstractHiddenRegion method collectAlternatingSpaceAndComments.

protected List<ITextSegment> collectAlternatingSpaceAndComments(boolean includeComments) {
    List<IHiddenRegionPart> parts = getParts();
    if (parts.isEmpty()) {
        return Collections.<ITextSegment>singletonList(this);
    } else {
        ITextSegment lastWhitespace = null;
        List<ITextSegment> result = Lists.newArrayList();
        for (IHiddenRegionPart part : parts) {
            if (part instanceof IWhitespace) {
                if (lastWhitespace == null) {
                    result.add(part);
                    lastWhitespace = part;
                } else {
                    int mergedLength = lastWhitespace.getLength() + part.getLength();
                    lastWhitespace = new TextSegment(access, lastWhitespace.getOffset(), mergedLength);
                    result.set(result.size() - 1, lastWhitespace);
                }
            } else if (part instanceof IComment) {
                if (lastWhitespace == null) {
                    result.add(new TextSegment(access, part.getOffset(), 0));
                } else {
                    lastWhitespace = null;
                }
                if (includeComments) {
                    result.add(part);
                }
            }
        }
        if (lastWhitespace == null) {
            result.add(new TextSegment(access, getEndOffset(), 0));
        }
        return ImmutableList.copyOf(result);
    }
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace)

Example 29 with ITextSegment

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

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

ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)30 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)10 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)8 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)5 String (java.lang.String)4 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)4 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)3 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)3 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)3 ITextRegionRewriter (org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter)3 ITextSegmentDiff (org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff)3 IAutowrapFormatter (org.eclipse.xtext.formatting2.IAutowrapFormatter)2 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)2 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)2 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)2 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)2 HiddenRegionFormattingToString (org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2