Search in sources :

Example 1 with TextReplacementList

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

the class TextRegionRewriter method renderToAppendable.

@Override
public void renderToAppendable(ITextSegment input, Iterable<? extends ITextReplacement> rep, Appendable result) throws IOException {
    int offset = input.getOffset();
    String text = input.getText();
    List<ITextReplacement> list = new TextReplacementList<ITextReplacement>(rep);
    Collections.sort(list);
    int lastOffset = 0;
    for (ITextReplacement r : list) {
        result.append(text.subSequence(lastOffset, r.getOffset() - offset));
        result.append(r.getReplacementText());
        lastOffset = (r.getOffset() - offset) + r.getLength();
    }
    if (lastOffset <= text.length())
        result.append(text.subSequence(lastOffset, text.length()));
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) TextReplacementList(org.eclipse.xtext.formatting2.internal.TextReplacementList)

Example 2 with TextReplacementList

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

the class TextReplacerContext method getReplacementsUntil.

@Override
public List<ITextReplacement> getReplacementsUntil(ITextReplacerContext first) {
    ITextReplacerContext current = this;
    List<Iterable<ITextReplacement>> reversedReplacements = Lists.newArrayList();
    while (current != null) {
        Iterable<ITextReplacement> localReplacements = current.getLocalReplacements();
        if (!Iterables.isEmpty(localReplacements))
            reversedReplacements.add(localReplacements);
        if (current == first)
            break;
        current = current.getPreviousContext();
    }
    Collections.reverse(reversedReplacements);
    List<ITextReplacement> flattenedReplacements = new TextReplacementList<ITextReplacement>();
    for (Iterable<ITextReplacement> chunk : reversedReplacements) Iterables.addAll(flattenedReplacements, chunk);
    return flattenedReplacements;
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Aggregations

ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)2 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)1 TextReplacementList (org.eclipse.xtext.formatting2.internal.TextReplacementList)1