Search in sources :

Example 1 with IHiddenRegionPart

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

the class HiddenRegionReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    AbstractFormatter2 formatter = context.getFormatter();
    List<IHiddenRegionPart> hiddens = region.getParts();
    if (hiddens.isEmpty()) {
        return formatter.createWhitespaceReplacer(region, formatting).createReplacements(context);
    } else if ((hiddens.size() == 1 && hiddens.get(0) instanceof IWhitespace)) {
        return formatter.createWhitespaceReplacer(hiddens.get(0), formatting).createReplacements(context);
    } else {
        List<ITextReplacer> replacers = createReplacers(formatter);
        applyHiddenRegionFormatting(replacers);
        ITextReplacerContext current = context;
        current.setNextReplacerIsChild();
        for (ITextReplacer replacer : replacers) current = replacer.createReplacements(current.withReplacer(replacer));
        return current;
    }
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) AbstractFormatter2(org.eclipse.xtext.formatting2.AbstractFormatter2) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) List(java.util.List) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 2 with IHiddenRegionPart

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

the class StringBasedTextRegionAccessDiffAppender method copyAndAppend.

public IHiddenRegionPart copyAndAppend(IHiddenRegionPart part) {
    StringHiddenRegion region;
    if (this.last instanceof StringHiddenRegion) {
        region = (StringHiddenRegion) this.last;
    } else {
        region = appendHiddenRegion(true);
    }
    String text = part.getText();
    int offset = result.append(text);
    if (part instanceof IComment) {
        IComment comment = ((IComment) part);
        AbstractRule grammarElement = (AbstractRule) comment.getGrammarElement();
        StringComment newComment = new StringComment(region, grammarElement, offset, text.length());
        region.addPart(newComment);
        recordDiff(part, newComment);
        return newComment;
    } else if (part instanceof IWhitespace) {
        IWhitespace ws = (IWhitespace) part;
        AbstractRule grammarElement = (AbstractRule) ws.getGrammarElement();
        StringWhitespace newWs = new StringWhitespace(region, grammarElement, offset, text.length());
        region.addPart(newWs);
        recordDiff(part, newWs);
        return newWs;
    }
    throw new IllegalStateException();
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 3 with IHiddenRegionPart

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

the class TextReplacerContext method isInUndefinedRegion.

protected boolean isInUndefinedRegion(ITextReplacement repl) {
    ITextSegment replacerRegion = replacer.getRegion();
    IHiddenRegion hidden = null;
    if (replacerRegion instanceof IHiddenRegionPart) {
        hidden = ((IHiddenRegionPart) replacerRegion).getHiddenRegion();
    } else if (replacerRegion instanceof IHiddenRegion) {
        hidden = (IHiddenRegion) replacerRegion;
    }
    return hidden == null || hidden.isUndefined();
}
Also used : IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 4 with IHiddenRegionPart

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

the class TextRegionAccessToString method toTokenAndGapList.

protected List<ITextSegment> toTokenAndGapList() {
    int range = this.highlightOrigin ? 4 : (Integer.MAX_VALUE / 2);
    ITextSegment first = null;
    ITextSegment current = origin;
    for (int i = 0; i < range && current != null; i++) {
        first = current;
        if (current instanceof ITextRegionAccess)
            current = ((ITextRegionAccess) current).regionForRootEObject().getPreviousHiddenRegion();
        else if (current instanceof ISequentialRegion)
            current = ((ISequentialRegion) current).getPreviousHiddenRegion();
        else if (current instanceof IHiddenRegionPart)
            current = ((IHiddenRegionPart) current).getHiddenRegion();
        else
            throw new IllegalStateException("Unexpected Type: " + current.getClass());
    }
    if (first == null)
        return Collections.emptyList();
    List<ITextSegment> result = new ArrayList<>();
    ITextSegment currentRegion = first;
    for (int i = 0; i <= (range * 2) && currentRegion != null; i++) {
        result.add(currentRegion);
        if (currentRegion instanceof ISemanticRegion)
            currentRegion = ((ISemanticRegion) currentRegion).getNextHiddenRegion();
        else if (currentRegion instanceof IHiddenRegion)
            currentRegion = ((IHiddenRegion) currentRegion).getNextSemanticRegion();
        else
            throw new IllegalStateException("Unexpected Type: " + currentRegion.getClass());
    }
    return result;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ISequentialRegion(org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) ArrayList(java.util.ArrayList) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 5 with IHiddenRegionPart

use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart 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.singletonList(this);
    } else {
        ITextSegment lastWhitespace = null;
        List<ITextSegment> result = new ArrayList<>();
        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) ArrayList(java.util.ArrayList) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace)

Aggregations

IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)6 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)4 IWhitespace (org.eclipse.xtext.formatting2.regionaccess.IWhitespace)4 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)3 ArrayList (java.util.ArrayList)2 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)2 List (java.util.List)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)1 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)1 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)1 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)1 ISequentialRegion (org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion)1 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)1 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)1