Search in sources :

Example 6 with TextSegment

use of org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment in project xtext-xtend by eclipse.

the class RichStringFormatter method setSpace.

protected void setSpace(final IFormattableDocument doc, final int offset, final int length, final String space) {
    IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
    final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
        it.setSpace(space);
    };
    final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
    AbstractFormatter2 _formatter = doc.getFormatter();
    ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
    TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
    final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
    doc.addReplacer(replacer);
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) AbstractFormatter2(org.eclipse.xtext.formatting2.AbstractFormatter2) TextSegment(org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)

Example 7 with TextSegment

use of org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment 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 8 with TextSegment

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

the class SinglelineCommentReplacer method getFirstSpace.

protected ITextSegment getFirstSpace() {
    IComment comment = getComment();
    String text = comment.getText();
    if (!text.startsWith(prefix))
        return null;
    int start = prefix.length();
    for (int i = start; i < text.length(); i++) {
        char charAt = text.charAt(i);
        if (!Character.isWhitespace(charAt) || charAt == '\r' || charAt == '\n')
            return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, i - start);
    }
    return new TextSegment(comment.getTextRegionAccess(), comment.getOffset() + start, text.length() - start);
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) TextSegment(org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 9 with TextSegment

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

the class AbstractHiddenRegion method collectAlternatingSpaceAndComments.

protected List<ITextSegment> collectAlternatingSpaceAndComments(boolean includeComments) {
    List<IHiddenRegionPart> parts = getParts();
    if (parts.isEmpty()) {
        return Collections.<ITextSegment>singletonList(this);
    } else {
        ITextSegment lastWhitespace = null;
        List<ITextSegment> result = Lists.newArrayList();
        for (IHiddenRegionPart part : parts) {
            if (part instanceof IWhitespace) {
                if (lastWhitespace == null) {
                    result.add(part);
                    lastWhitespace = part;
                } else {
                    int mergedLength = lastWhitespace.getLength() + part.getLength();
                    lastWhitespace = new TextSegment(access, lastWhitespace.getOffset(), mergedLength);
                    result.set(result.size() - 1, lastWhitespace);
                }
            } else if (part instanceof IComment) {
                if (lastWhitespace == null) {
                    result.add(new TextSegment(access, part.getOffset(), 0));
                } else {
                    lastWhitespace = null;
                }
                if (includeComments) {
                    result.add(part);
                }
            }
        }
        if (lastWhitespace == null) {
            result.add(new TextSegment(access, getEndOffset(), 0));
        }
        return ImmutableList.copyOf(result);
    }
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace)

Example 10 with TextSegment

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

the class RegionSetTest method testMergeConflic3.

@Test
public void testMergeConflic3() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("issues=mergeConflict:[20,2]<>[21,0]");
    _builder.newLine();
    _builder.append("set=[[20,2]]");
    _builder.newLine();
    final Procedure1<TestableTextSegmentSet> _function = (TestableTextSegmentSet it) -> {
        TextSegment _textSegment = new TextSegment(null, 21, 0);
        it.add(_textSegment);
        TextSegment _textSegment_1 = new TextSegment(null, 20, 2);
        it.add(_textSegment_1);
    };
    this.test(_builder, _function);
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TextSegment(org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TestableTextSegmentSet(org.eclipse.xtext.formatting2.internal.TestableTextSegmentSet) Test(org.junit.Test)

Aggregations

ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)12 TextSegment (org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)12 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)8 TestableTextSegmentSet (org.eclipse.xtext.formatting2.internal.TestableTextSegmentSet)8 Test (org.junit.Test)8 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)2 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)2 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)2 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)2 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)2 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)1 IWhitespace (org.eclipse.xtext.formatting2.regionaccess.IWhitespace)1