Search in sources :

Example 6 with IHiddenRegionPart

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

Example 7 with IHiddenRegionPart

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

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 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