Search in sources :

Example 16 with ITextSegment

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

the class TextRegions method merge.

public static ITextSegment merge(Iterable<? extends ITextSegment> segments) {
    Iterator<? extends ITextSegment> it = segments.iterator();
    if (!it.hasNext())
        throw new IllegalStateException();
    ITextSegment first = it.next();
    int minOffset = first.getOffset();
    int maxEndOffset = first.getEndOffset();
    while (it.hasNext()) {
        ITextSegment next = it.next();
        int offset = next.getOffset();
        int endOffset = next.getEndOffset();
        if (offset < minOffset)
            minOffset = offset;
        if (endOffset > maxEndOffset)
            maxEndOffset = endOffset;
    }
    return new TextSegment(first.getTextRegionAccess(), minOffset, maxEndOffset - minOffset);
}
Also used : ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 17 with ITextSegment

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

Example 18 with ITextSegment

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

the class TextRegionsInTextToString method getFrame.

public ITextSegment getFrame() {
    if (this.frame != null)
        return this.frame;
    ITextRegionAccess access = getTextRegionAccess();
    if (access != null) {
        ITextSegment impactRegion = TextRegions.merge(this.items);
        List<ILineRegion> expandToLines = TextRegions.expandToLines(impactRegion, getLeadingLines(), getTrailingLines());
        return TextRegions.merge(expandToLines);
    }
    return null;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 19 with ITextSegment

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

the class TextRegionsWithTitleToString method getFrame.

public ITextSegment getFrame() {
    if (this.frame != null)
        return this.frame;
    ITextRegionAccess access = getTextRegionAccess();
    if (access != null) {
        List<ITextSegment> segments = Lists.newArrayList();
        for (Item item : items) segments.add(item.getRegion());
        ITextSegment impactRegion = merge(segments);
        List<ILineRegion> expandToLines = expandToLines(impactRegion, getLeadingLines(), getTrailingLines());
        return merge(expandToLines);
    }
    return null;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 20 with ITextSegment

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

the class TextRegionsWithTitleToString method toString.

@Override
public String toString() {
    ITextRegionAccess access = getTextRegionAccess();
    ITextSegment frame = getFrame();
    if (access == null || frame == null)
        return "(null)";
    ITextRegionRewriter rewriter = access.getRewriter();
    StringBuilder builder = new StringBuilder();
    List<ITextReplacement> replacements = Lists.newArrayList();
    for (int i = 0; i < this.items.size(); i++) {
        Item item = this.items.get(i);
        ITextSegment region = item.getRegion();
        String regionStr = "offset=" + region.getOffset() + " length=" + region.getLength();
        String open = i < BRACKETS_OPEN.length ? BRACKETS_OPEN[i] : "[" + i + "[";
        String close = i < BRACKETS_CLOSE.length ? BRACKETS_CLOSE[i] : "]" + i + "]";
        builder.append(open + close + ": " + item.getTitle() + " at " + regionStr + "\n");
        replacements.add(rewriter.createReplacement(region.getOffset(), 0, open));
        replacements.add(rewriter.createReplacement(region.getEndOffset(), 0, close));
    }
    String vizualized = rewriter.renderToString(frame, replacements);
    builder.append(box("document snippet", vizualized));
    return builder.toString();
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextRegionRewriter(org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Aggregations

ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)30 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)10 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)8 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)5 String (java.lang.String)4 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)4 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)3 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)3 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)3 ITextRegionRewriter (org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter)3 ITextSegmentDiff (org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff)3 IAutowrapFormatter (org.eclipse.xtext.formatting2.IAutowrapFormatter)2 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)2 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)2 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)2 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)2 HiddenRegionFormattingToString (org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2