Search in sources :

Example 31 with ITextRegionAccess

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

the class RegionAccessDiffTest method testMove.

@Test
public void testMove() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("8 a b c");
    _builder.newLine();
    final ITextRegionAccess access = this._regionAccessTestHelper.toTextRegionAccess(_builder);
    final Procedure1<ITextRegionDiffBuilder> _function = (ITextRegionDiffBuilder it) -> {
        final List<ISemanticRegion> regions = access.regionForRootEObject().getAllRegionsFor().features(RegionaccesstestlanguagePackage.Literals.VALUE_LIST__NAME);
        final ISemanticRegion a = regions.get(0);
        final ISemanticRegion b = regions.get(1);
        it.move(b.getNextHiddenRegion(), a.getPreviousHiddenRegion(), a.getNextHiddenRegion());
    };
    ITextRegionAccess _modify = this._regionAccessTestHelper.modify(access, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("0 0   H");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("B ValueList\'[a, b, c]\' Root");
    _builder_1.newLine();
    _builder_1.append("0 1    S \"8\"        Root:\'8\'");
    _builder_1.newLine();
    _builder_1.append("1   1  H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("2    \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("3 1    S \"b\"        ValueList:name+=ID");
    _builder_1.newLine();
    _builder_1.append("4 1 2  H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("5 1 2  S \"a\"        ValueList:name+=ID");
    _builder_1.newLine();
    _builder_1.append("6 0 2  H");
    _builder_1.newLine();
    _builder_1.append("6 1    S \"c\"        ValueList:name+=ID");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("E ValueList\'[a, b, c]\' Root");
    _builder_1.newLine();
    _builder_1.append("7 0   H");
    _builder_1.newLine();
    _builder_1.append("------------ diff 1 ------------");
    _builder_1.newLine();
    _builder_1.append("1 1 H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("2 1 S \"a\"        ValueList:name+=ID");
    _builder_1.newLine();
    _builder_1.append("3 1 H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("------------ diff 2 ------------");
    _builder_1.newLine();
    _builder_1.append("5 1 H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("6 1 S \"c\"        ValueList:name+=ID");
    _builder_1.newLine();
    this._regionAccessTestHelper.operator_tripleEquals(_modify, _builder_1);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder) List(java.util.List) ValueList(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.ValueList) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) Test(org.junit.Test)

Example 32 with ITextRegionAccess

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

the class RegionAccessTestHelper method toTextRegionAccess.

public ITextRegionAccess toTextRegionAccess(final CharSequence file) {
    try {
        final Root obj = this.parseHelper.parse(file.toString().replace("\r\n", "\n").trim());
        this.validationTestHelper.assertNoErrors(obj);
        Resource _eResource = obj.eResource();
        return this.textRegionAccessBuilder.get().forNodeModel(((XtextResource) _eResource)).create();
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Root(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Root) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource)

Example 33 with ITextRegionAccess

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

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

the class LineRegion method getPreviousLine.

@Override
public ILineRegion getPreviousLine() {
    ITextRegionAccess access = getTextRegionAccess();
    int end = getOffset() - 1;
    String text = access.regionForDocument().getText();
    while (true) {
        if (end < 0)
            return null;
        char c = text.charAt(end);
        if (c == '\n' || c == '\r')
            end--;
        else
            break;
    }
    int start = text.lastIndexOf('\n', end);
    if (start < 0)
        start = 0;
    return new LineRegion(access, start, end - start);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion)

Example 35 with ITextRegionAccess

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

Aggregations

ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)71 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)37 Test (org.junit.Test)37 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)21 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)17 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)16 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)16 EObject (org.eclipse.emf.ecore.EObject)14 OptionalChildList (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.OptionalChildList)12 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)11 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)10 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)10 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)9 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)9 EList (org.eclipse.emf.common.util.EList)8 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)7 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)7 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)6 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)6 XtextResource (org.eclipse.xtext.resource.XtextResource)5