Search in sources :

Example 16 with IEObjectRegion

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

the class StringBasedTextRegionAccessDiffBuilder method create.

@Override
public StringBasedTextRegionAccessDiff create() {
    StringBasedTextRegionAccessDiffAppender appender = createAppender();
    IEObjectRegion root = original.regionForRootEObject();
    appender.copyAndAppend(root.getPreviousHiddenRegion(), PREVIOUS);
    appender.copyAndAppend(root.getPreviousHiddenRegion(), CONTAINER);
    List<Rewrite> rws = createList();
    IHiddenRegion last = null;
    for (Rewrite rw : rws) {
        boolean diff = rw.isDiff();
        if (diff) {
            appender.beginDiff();
        }
        if (rw instanceof Insert) {
            Insert ins = (Insert) rw;
            IHiddenRegion f = ins.getInsertFirst();
            IHiddenRegion l = ins.getInsertLast();
            appender.copyAndAppend(f, NEXT);
            if (f != l) {
                appender.copyAndAppend(f.getNextSemanticRegion(), l.getPreviousSemanticRegion());
            }
            appender.copyAndAppend(l, PREVIOUS);
        }
        if (diff) {
            appender.endDiff();
        }
        if (rw.originalLast != last) {
            appender.copyAndAppend(rw.originalLast, CONTAINER);
        }
        last = rw.originalLast;
    }
    appender.copyAndAppend(root.getNextHiddenRegion(), NEXT);
    StringBasedTextRegionAccessDiff result = appender.finish();
    AbstractEObjectRegion newRoot = result.regionForEObject(root.getSemanticElement());
    result.setRootEObject(newRoot);
    return result;
}
Also used : IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Example 17 with IEObjectRegion

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

the class RegionAccessDiffTest method testSerializeRootObject.

@Test
public void testSerializeRootObject() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("3 foo");
    _builder.newLine();
    final ITextRegionAccess access = this._regionAccessTestHelper.toTextRegionAccess(_builder);
    final Procedure1<ITextRegionDiffBuilder> _function = (ITextRegionDiffBuilder it) -> {
        final IEObjectRegion root = access.regionForRootEObject();
        EObject _semanticElement = root.getSemanticElement();
        final Delegate rootObj = ((Delegate) _semanticElement);
        rootObj.setName("baaaz");
        final ITextRegionAccess textRegions = this.serializer.serializeToRegions(rootObj);
        it.replace(root.getPreviousHiddenRegion(), root.getNextHiddenRegion(), textRegions);
    };
    ITextRegionAccess _modify = this._regionAccessTestHelper.modify(access, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("0 0 1 H");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("B Delegate\'baaaz\' Root");
    _builder_1.newLine();
    _builder_1.append("0 1 1  S \"3\"        Unassigned:\'3\'");
    _builder_1.newLine();
    _builder_1.append("1 1 1  H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("2 5 1  S \"baaaz\"    Delegate:name=ID");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("E Delegate\'baaaz\' Root");
    _builder_1.newLine();
    _builder_1.append("7 0 1 H");
    _builder_1.newLine();
    _builder_1.append("------------ diff 1 ------------");
    _builder_1.newLine();
    _builder_1.append("0 0 H");
    _builder_1.newLine();
    _builder_1.append("0 1 S \"3\"        Unassigned:\'3\'");
    _builder_1.newLine();
    _builder_1.append("1 1 H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("2 3 S \"foo\"      Delegate:name=ID");
    _builder_1.newLine();
    _builder_1.append("5 0 H");
    _builder_1.newLine();
    this._regionAccessTestHelper.operator_tripleEquals(_modify, _builder_1);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) Delegate(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Delegate) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder) Test(org.junit.Test)

Example 18 with IEObjectRegion

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

the class RegionAccessDiffTest method testSerializeChildObject.

@Test
public void testSerializeChildObject() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("2 foo");
    _builder.newLine();
    final ITextRegionAccess access = this._regionAccessTestHelper.toTextRegionAccess(_builder);
    final Procedure1<ITextRegionDiffBuilder> _function = (ITextRegionDiffBuilder it) -> {
        EObject _semanticElement = access.regionForRootEObject().getSemanticElement();
        final Delegate child = ((Delegation) _semanticElement).getDelegate();
        final IEObjectRegion childRegion = access.regionForEObject(child);
        child.setName("baaaz");
        final ITextRegionAccess textRegions = this.serializer.serializeToRegions(child);
        it.replace(childRegion.getPreviousHiddenRegion(), childRegion.getNextHiddenRegion(), textRegions);
    };
    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 Delegation Root");
    _builder_1.newLine();
    _builder_1.append("0 1    S \"2\"        Delegation:\'2\'");
    _builder_1.newLine();
    _builder_1.append("1 1 1  H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("       ");
    _builder_1.append("B Delegate\'baaaz\' Delegate path:Delegation/delegate");
    _builder_1.newLine();
    _builder_1.append("2 5 1   S \"baaaz\"    Delegate:name=ID");
    _builder_1.newLine();
    _builder_1.append("       ");
    _builder_1.append("E Delegate\'baaaz\' Delegate path:Delegation/delegate");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("E Delegation Root");
    _builder_1.newLine();
    _builder_1.append("7 0 1 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 3  S \"foo\"      Delegate:name=ID");
    _builder_1.newLine();
    _builder_1.append("5 0  H");
    _builder_1.newLine();
    this._regionAccessTestHelper.operator_tripleEquals(_modify, _builder_1);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) Delegate(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Delegate) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder) Test(org.junit.Test)

Example 19 with IEObjectRegion

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

the class SemanticRegionFinderTest method regionForFeatureCrossReference.

@Test
public void regionForFeatureCrossReference() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("6 (ref foo) action (foo) end");
    final AssignedAction mixed = this.<AssignedAction>parseAs(_builder, AssignedAction.class);
    final IEObjectRegion finder = this.toAccess(mixed).regionForEObject(mixed.getChild());
    final ISemanticRegion actual = finder.getRegionFor().feature(RegionaccesstestlanguagePackage.Literals.MIXED__REF);
    final List<ISemanticRegion> actuals = finder.getRegionFor().features(RegionaccesstestlanguagePackage.Literals.MIXED__REF);
    this.assertEquals("foo", actual, actuals);
}
Also used : AssignedAction(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.AssignedAction) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) Test(org.junit.Test)

Example 20 with IEObjectRegion

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

the class AbstractFormatter2 method isInRequestedRange.

protected boolean isInRequestedRange(EObject obj) {
    Collection<ITextRegion> regions = request.getRegions();
    if (regions.isEmpty())
        return true;
    ITextRegionAccess access = request.getTextRegionAccess();
    IEObjectRegion objRegion = access.regionForEObject(obj);
    if (objRegion == null)
        return false;
    IHiddenRegion previousHidden = objRegion.getPreviousHiddenRegion();
    IHiddenRegion nextHidden = objRegion.getNextHiddenRegion();
    int objOffset = previousHidden != null ? previousHidden.getOffset() : 0;
    int objEnd = nextHidden != null ? nextHidden.getEndOffset() : access.regionForRootEObject().getEndOffset();
    for (ITextRegion region : regions) {
        int regionOffset = region.getOffset();
        int regionEnd = regionOffset + region.getLength();
        if (regionOffset <= objEnd && regionEnd >= objOffset)
            return true;
    }
    return false;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Aggregations

IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)16 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)9 EObject (org.eclipse.emf.ecore.EObject)8 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)7 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)5 EReference (org.eclipse.emf.ecore.EReference)4 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 IAstRegion (org.eclipse.xtext.formatting2.regionaccess.IAstRegion)3 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 EList (org.eclipse.emf.common.util.EList)2 ListChange (org.eclipse.emf.ecore.change.ListChange)2 AbstractElement (org.eclipse.xtext.AbstractElement)2 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)2 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)2 Delegate (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Delegate)2