Search in sources :

Example 1 with ITextSegment

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

the class FormatterTestHelper method assertAllWhitespaceIsFormatted.

protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
    List<ITextSegment> expected = Lists.newArrayList();
    IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
    while (current != null) {
        expected.addAll(current.getMergedSpaces());
        current = current.getNextHiddenRegion();
    }
    List<ITextSegment> missing = TextRegions.difference(expected, replacements);
    if (!missing.isEmpty()) {
        TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
        for (ITextSegment region : missing) toString.add(region, region.getClass().getSimpleName());
        String msg = "The following regions are not formatted:\n" + toString;
        System.err.println(msg);
        Assert.fail(msg);
    }
}
Also used : IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Example 2 with ITextSegment

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

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

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

the class RegionsOutsideFrameException method getMessage.

@Override
public String getMessage() {
    String regionsStr;
    if (regions.length == 1) {
        regionsStr = "region " + regions[0].getFirst();
    } else {
        List<String> titles = Lists.newArrayList();
        for (Pair<String, ITextSegment> p : regions) titles.add(p.getFirst());
        regionsStr = "regions " + Joiner.on(", ").join(titles);
    }
    StringBuilder builder = new StringBuilder();
    builder.append("The " + regionsStr + " must not be outside " + frameTitle + ".\n");
    TextRegionsWithTitleToString toStr = new TextRegionsWithTitleToString();
    toStr.add(frameTitle, frameRegion);
    for (Pair<String, ITextSegment> p : regions) toStr.add(p.getFirst(), p.getSecond());
    builder.append(toStr);
    return builder.toString();
}
Also used : TextRegionsWithTitleToString(org.eclipse.xtext.formatting2.debug.TextRegionsWithTitleToString) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TextRegionsWithTitleToString(org.eclipse.xtext.formatting2.debug.TextRegionsWithTitleToString)

Example 5 with ITextSegment

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

the class TextReplacerContext method isInUndefinedRegion.

protected boolean isInUndefinedRegion(ITextReplacement repl) {
    ITextSegment replacerRegion = replacer.getRegion();
    IHiddenRegion hidden = null;
    if (replacerRegion instanceof IHiddenRegionPart) {
        hidden = ((IHiddenRegionPart) replacerRegion).getHiddenRegion();
    } else if (replacerRegion instanceof IHiddenRegion) {
        hidden = (IHiddenRegion) replacerRegion;
    }
    return hidden == null || hidden.isUndefined();
}
Also used : IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Aggregations

ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)35 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)12 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)9 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)6 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)6 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)6 String (java.lang.String)4 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)4 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)3 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)3 ITextRegionRewriter (org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter)3 ITextSegmentDiff (org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff)3 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)2 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