Search in sources :

Example 1 with ITextSegmentDiff

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

the class RegionDiffFormatter method mergeReplacements.

protected List<ITextReplacement> mergeReplacements(ITextRegionAccessDiff regions, List<ITextReplacement> rep) {
    ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
    List<ITextReplacement> result = Lists.newArrayList();
    for (ITextSegmentDiff r : regions.getRegionDifferences()) {
        ITextSegment original = r.getOriginalRegion();
        ITextSegment modified = r.getModifiedRegion();
        List<ITextReplacement> local = Lists.newArrayList();
        for (ITextReplacement re : rep) {
            if (modified.contains(re)) {
                local.add(re);
            }
        }
        String text;
        if (local.isEmpty()) {
            text = modified.getText();
        } else {
            text = regions.getRewriter().renderToString(modified, local);
        }
        ITextReplacement replacement = rewriter.createReplacement(original.getOffset(), original.getLength(), text);
        result.add(replacement);
    }
    return result;
}
Also used : ITextRegionRewriter(org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter) ITextSegmentDiff(org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 2 with ITextSegmentDiff

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

the class RegionDiffFormatter method collectRegionsToFormat.

protected Collection<ITextRegion> collectRegionsToFormat(ITextRegionAccessDiff regions) {
    List<ITextRegion> result = Lists.newArrayList();
    for (ITextSegmentDiff diff : regions.getRegionDifferences()) {
        int offset = diff.getModifiedFirstRegion().getOffset();
        int length = diff.getModifiedLastRegion().getEndOffset() - offset;
        ITextSegment region = regions.regionForOffset(offset, length);
        result.add(region);
    }
    return result;
}
Also used : ITextSegmentDiff(org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 3 with ITextSegmentDiff

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

the class RegionAccessTestHelper method assertDiffs.

public void assertDiffs(final ITextRegionAccessDiff regions) {
    final ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
    final ArrayList<ITextReplacement> result = Lists.<ITextReplacement>newArrayList();
    List<ITextSegmentDiff> _regionDifferences = regions.getRegionDifferences();
    for (final ITextSegmentDiff r : _regionDifferences) {
        {
            final ITextSegment originalRegion = r.getOriginalRegion();
            final ITextSegment modifiedRegion = r.getModifiedRegion();
            final ITextReplacement replacement = originalRegion.replaceWith(modifiedRegion.getText());
            result.add(replacement);
        }
    }
    final String actual = rewriter.renderToString(result);
    final String expected = regions.regionForDocument().getText();
    Assert.assertEquals(expected, actual);
}
Also used : ITextRegionRewriter(org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter) ITextSegmentDiff(org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)

Aggregations

ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)3 ITextSegmentDiff (org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff)3 ITextRegionRewriter (org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter)2 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)1 ITextRegion (org.eclipse.xtext.util.ITextRegion)1