Search in sources :

Example 6 with ITextSegment

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

the class TextReplacerContext method getRegion.

protected ITextSegment getRegion(int index) {
    ITextReplacerContext current = this;
    while (current != null) {
        ITextReplacer replacer2 = current.getReplacer();
        if (replacer2 != null) {
            if (index == 0) {
                return replacer2.getRegion();
            } else
                index--;
        }
        current = current.getPreviousContext();
    }
    return null;
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 7 with ITextSegment

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

the class TextReplacerContext method isWrapInRegion.

@Override
public boolean isWrapInRegion() {
    ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
    ITextSegment region = getReplacer().getRegion();
    int lastOffset = region.getOffset();
    for (ITextReplacement rep : this.getLocalReplacements()) {
        int endOffset = rep.getOffset();
        String between = access.textForOffset(lastOffset, endOffset - lastOffset);
        if (between.contains("\n") || rep.getReplacementText().contains("\n")) {
            return true;
        }
        lastOffset = rep.getEndOffset();
    }
    String rest = access.textForOffset(lastOffset, region.getEndOffset() - lastOffset);
    if (rest.contains("\n")) {
        return true;
    }
    return false;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) String(java.lang.String)

Example 8 with ITextSegment

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

the class TextRegionAccessToString method toTokenAndGapList.

protected List<ITextSegment> toTokenAndGapList() {
    final int range = this.hightlightOrigin ? 4 : (Integer.MAX_VALUE / 2);
    ITextSegment first = null;
    {
        ITextSegment current = origin;
        for (int i = 0; i < range && current != null; i++) {
            first = current;
            if (current instanceof ITextRegionAccess)
                current = ((ITextRegionAccess) current).regionForRootEObject().getPreviousHiddenRegion();
            else if (current instanceof ISequentialRegion)
                current = ((ISequentialRegion) current).getPreviousHiddenRegion();
            else if (current instanceof IHiddenRegionPart)
                current = ((IHiddenRegionPart) current).getHiddenRegion();
            else
                throw new IllegalStateException("Unexpected Type: " + current.getClass());
        }
    }
    if (first == null)
        return Collections.emptyList();
    List<ITextSegment> result = Lists.newArrayList();
    {
        ITextSegment current = first;
        for (int i = 0; i <= (range * 2) && current != null; i++) {
            result.add(current);
            if (current instanceof ISemanticRegion)
                current = ((ISemanticRegion) current).getNextHiddenRegion();
            else if (current instanceof IHiddenRegion)
                current = ((IHiddenRegion) current).getNextSemanticRegion();
            else
                throw new IllegalStateException("Unexpected Type: " + current.getClass());
        }
    }
    return result;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ISequentialRegion(org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 9 with ITextSegment

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

the class TextRegionAccessToString method toString.

@Override
public String toString() {
    List<ITextSegment> list = toTokenAndGapList();
    if (list.isEmpty())
        return "(empty)";
    ITextRegionAccess access = list.get(0).getTextRegionAccess();
    DiffColumn diff = new DiffColumn(access);
    TextRegionListToString result = new TextRegionListToString();
    if (!hideColumnExplanation) {
        if (diff.isDiff()) {
            result.add("Columns: 1:offset 2:length 3:diff 4:kind 5: text 6:grammarElement", false);
        } else {
            result.add("Columns: 1:offset 2:length 3:kind 4: text 5:grammarElement", false);
        }
        result.add("Kind: H=IHiddenRegion S=ISemanticRegion B/E=IEObjectRegion", false);
        result.add("", false);
    }
    appendRegions(result, list, diff, false);
    diff.appendDiffs(result, this);
    return result.toString();
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 10 with ITextSegment

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

the class TextRegionsInTextToString method toString.

@Override
public String toString() {
    try {
        ITextRegionAccess access = getTextRegionAccess();
        ITextSegment frame = getFrame();
        if (access == null || frame == null)
            return box(title, "(no changes)");
        StringBuilder builder = new StringBuilder();
        String vizualized = access.getRewriter().renderToString(frame, items);
        builder.append(box(title, vizualized));
        return builder.toString();
    } catch (Exception e) {
        return box("error", e.getMessage() + "\n" + Throwables.getStackTraceAsString(e));
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) 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