Search in sources :

Example 11 with ITextReplacer

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

the class HiddenRegionReplacer method createReplacers.

protected List<ITextReplacer> createReplacers(AbstractFormatter2 formatter) {
    List<ITextSegment> regions = region.getAlternatingMergedSpaceAndComments();
    List<ITextReplacer> replacers = Lists.newArrayListWithCapacity(regions.size());
    for (ITextSegment region : regions) {
        if (region instanceof IComment)
            replacers.add(formatter.createCommentReplacer((IComment) region));
        else
            replacers.add(formatter.createWhitespaceReplacer(region, formatter.createHiddenRegionFormatting()));
    }
    return replacers;
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 12 with ITextReplacer

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

the class HiddenRegionReplacer method applyHiddenRegionFormatting.

/**
 * This method is when we have multiple TextReplacers for this HiddenRegion because the HiddenRegion contains
 * comments. It applies the formatting configuration from {@link #formatting} to the {@link WhitespaceReplacer} that
 * surround the comment.
 */
protected void applyHiddenRegionFormatting(List<ITextReplacer> replacers) {
    IHiddenRegionFormatting separator = findWhitespaceThatSeparatesSemanticRegions(replacers).getFormatting();
    // 1. apply indentation
    Integer inc = formatting.getIndentationIncrease();
    Integer dec = formatting.getIndentationDecrease();
    if (inc != null && dec != null) {
        ((WhitespaceReplacer) replacers.get(0)).getFormatting().setIndentationIncrease(inc);
        ((WhitespaceReplacer) replacers.get(replacers.size() - 1)).getFormatting().setIndentationDecrease(dec);
    } else if (inc != null) {
        separator.setIndentationIncrease(inc);
    } else if (dec != null) {
        separator.setIndentationDecrease(dec);
    }
    // 2. apply NewLine and space configuration to the first whitespace region that follows or contains a linewrap.
    separator.setNewLinesDefault(formatting.getNewLineDefault());
    separator.setNewLinesMax(formatting.getNewLineMax());
    separator.setNewLinesMin(formatting.getNewLineMin());
    separator.setSpace(formatting.getSpace());
    // 3. apply the 'priority' configuration for all and set a default formatting
    for (ITextReplacer replacer : replacers) if (replacer instanceof WhitespaceReplacer) {
        IHiddenRegionFormatting formatting2 = ((WhitespaceReplacer) replacer).getFormatting();
        formatting2.setPriority(formatting.getPriority());
        if (formatting2 != separator) {
            formatting2.setSpace(replacer.getRegion().getOffset() > 0 ? " " : "");
            formatting2.setNewLinesMin(0);
            formatting2.setNewLinesMax(1);
        }
    }
    // 4. make sure whitespace before and after multiline comments introduce a NewLine
    for (int i = 0; i < replacers.size(); i++) {
        ITextReplacer replacer = replacers.get(i);
        if (replacer instanceof CommentReplacer) {
            CommentReplacer commentReplacer = ((CommentReplacer) replacer);
            WhitespaceReplacer leading = (WhitespaceReplacer) replacers.get(i - 1);
            WhitespaceReplacer trailing = (WhitespaceReplacer) replacers.get(i + 1);
            commentReplacer.configureWhitespace(leading, trailing);
        }
    }
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer)

Example 13 with ITextReplacer

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

the class HiddenRegionReplacer method findWhitespaceThatSeparatesSemanticRegions.

protected WhitespaceReplacer findWhitespaceThatSeparatesSemanticRegions(List<ITextReplacer> replacers) {
    boolean hasSeenWrap = false;
    for (ITextReplacer replacer : replacers) {
        if (replacer instanceof WhitespaceReplacer) {
            WhitespaceReplacer whitespaceReplacer = (WhitespaceReplacer) replacer;
            hasSeenWrap |= whitespaceReplacer.getRegion().isMultiline();
            if (hasSeenWrap)
                return whitespaceReplacer;
        }
    }
    return (WhitespaceReplacer) replacers.get(replacers.size() - 1);
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer)

Example 14 with ITextReplacer

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

the class TextReplacerContext method withReplacer.

@Override
public ITextReplacerContext withReplacer(ITextReplacer replacer) {
    ITextReplacerContext current = this;
    while (current != null) {
        ITextReplacer lastReplacer = current.getReplacer();
        if (lastReplacer != null) {
            if (nextReplacerIsChild) {
                Preconditions.checkArgument(lastReplacer.getRegion().contains(replacer.getRegion()));
            } else {
                Preconditions.checkArgument(lastReplacer.getRegion().getEndOffset() <= replacer.getRegion().getOffset());
            }
            break;
        }
        current = current.getPreviousContext();
    }
    return new TextReplacerContext(document, this, indentation, replacer);
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 15 with ITextReplacer

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

the class TextReplacerMerger method mergeHiddenRegionReplacers.

protected ITextReplacer mergeHiddenRegionReplacers(List<? extends ITextReplacer> conflicting) {
    List<IHiddenRegionFormatting> formattings = Lists.newArrayList();
    IHiddenRegion region = null;
    for (ITextReplacer replacer : conflicting) {
        if (replacer instanceof HiddenRegionReplacer) {
            HiddenRegionReplacer hiddenRegionReplacer = (HiddenRegionReplacer) replacer;
            formattings.add(hiddenRegionReplacer.getFormatting());
            if (region == null)
                region = hiddenRegionReplacer.getRegion();
            else if (region != hiddenRegionReplacer.getRegion())
                return null;
        } else
            return null;
    }
    IHiddenRegionFormatting mergedFormatting = merger.merge(formattings);
    if (mergedFormatting != null)
        return formatter.createHiddenRegionReplacer(region, mergedFormatting);
    return null;
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Aggregations

ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)13 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)5 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)5 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)4 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)3 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)2 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)2 TextSegment (org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)2 List (java.util.List)1 EObject (org.eclipse.emf.ecore.EObject)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 FormattingNotApplicableException (org.eclipse.xtext.formatting2.FormattingNotApplicableException)1 ISubFormatter (org.eclipse.xtext.formatting2.ISubFormatter)1 HiddenRegionFormattingToString (org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString)1 MultilineCommentReplacer (org.eclipse.xtext.formatting2.internal.MultilineCommentReplacer)1 SinglelineCodeCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer)1 SinglelineDocCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer)1 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)1 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)1 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)1