Search in sources :

Example 11 with ITextReplacerContext

use of org.eclipse.xtext.formatting2.ITextReplacerContext 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 12 with ITextReplacerContext

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

the class TextReplacerContext method getLeadingCharsInLineCount.

@Override
public int getLeadingCharsInLineCount() {
    ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
    int lastOffset = replacer.getRegion().getOffset();
    ITextReplacerContext current = this;
    int count = 0;
    while (current != null) {
        Iterator<ITextReplacement> localReplacements = current.getLocalReplacementsReverse().iterator();
        while (localReplacements.hasNext()) {
            ITextReplacement rep = localReplacements.next();
            int endOffset = rep.getEndOffset();
            if (endOffset > lastOffset) {
                // System.out.println("error");
                continue;
            }
            String between = access.textForOffset(endOffset, lastOffset - endOffset);
            int idx = between.lastIndexOf('\n');
            if (idx >= 0)
                return count + logicalLength(between.substring(idx + 1));
            count += logicalLength(between);
            String text = rep.getReplacementText();
            int idx2 = text.lastIndexOf('\n');
            if (idx2 >= 0)
                return count + logicalLength(text.substring(idx2 + 1));
            count += logicalLength(text);
            lastOffset = rep.getOffset();
        }
        current = current.getPreviousContext();
    }
    String rest = access.textForOffset(0, lastOffset);
    int idx = rest.lastIndexOf('\n');
    if (idx >= 0)
        return count + logicalLength(rest.substring(idx + 1));
    count += lastOffset;
    return count;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) String(java.lang.String) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Aggregations

ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)10 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)5 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)4 FormattingNotApplicableException (org.eclipse.xtext.formatting2.FormattingNotApplicableException)2 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)2 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)2 String (java.lang.String)1 List (java.util.List)1 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)1 IAutowrapFormatter (org.eclipse.xtext.formatting2.IAutowrapFormatter)1 ISubFormatter (org.eclipse.xtext.formatting2.ISubFormatter)1 HiddenRegionFormattingToString (org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString)1 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)1 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)1 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)1 IWhitespace (org.eclipse.xtext.formatting2.regionaccess.IWhitespace)1