Search in sources :

Example 1 with IWhitespace

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

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

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

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

the class HiddenRegionPartAssociator method associate.

@Override
public void associate(IHiddenRegion region, BiConsumer<IHiddenRegionPart, HiddenRegionPartAssociation> handler) {
    List<IHiddenRegionPart> parts = region.getParts();
    if (!region.isMultiline()) {
        // rather than one of the surrounding semantic tokens.
        for (IHiddenRegionPart p : parts) {
            handler.accept(p, HiddenRegionPartAssociation.CONTAINER);
        }
    } else {
        int i = 0;
        if (region.getPreviousSemanticRegion() != null) {
            // collect whitespace and comments from the same line as belonging to the PREVIOUS semantic token
            while (i < parts.size()) {
                IHiddenRegionPart part = parts.get(i);
                if (part.isMultiline()) {
                    if (part.getText().endsWith("\n")) {
                        handler.accept(part, HiddenRegionPartAssociation.PREVIOUS);
                        i++;
                    }
                    break;
                } else {
                    handler.accept(part, HiddenRegionPartAssociation.PREVIOUS);
                    i++;
                }
            }
        }
        int j = parts.size() - 1;
        if (region.getNextSemanticRegion() != null) {
            // associated with the NEXT semantic token.
            while (j >= i) {
                IHiddenRegionPart part = parts.get(j);
                if (part instanceof IWhitespace && part.getLineCount() > 2) {
                    break;
                } else {
                    handler.accept(part, HiddenRegionPartAssociation.NEXT);
                    j--;
                }
            }
        }
        // Associate the remaining tokens with the CONTAINER
        for (int k = i; k <= j; k++) {
            IHiddenRegionPart part = parts.get(k);
            handler.accept(part, HiddenRegionPartAssociation.CONTAINER);
        }
    }
}
Also used : IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace)

Aggregations

IWhitespace (org.eclipse.xtext.formatting2.regionaccess.IWhitespace)4 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)3 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 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)1