Search in sources :

Example 6 with ITextRegionDiffBuilder

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

the class RegionAccessCommentsTest method testDeleteMiddleRegion.

@Test
public void testDeleteMiddleRegion() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("8");
    _builder.newLine();
    _builder.append("a /*a*/ //a");
    _builder.newLine();
    _builder.append("/*b1*/");
    _builder.newLine();
    _builder.append("/*b2*/");
    _builder.newLine();
    _builder.append("b");
    _builder.newLine();
    final ITextRegionAccess access = this._regionAccessTestHelper.toTextRegionAccess(_builder);
    final Procedure1<ITextRegionDiffBuilder> _function = (ITextRegionDiffBuilder it) -> {
        final ISemanticRegion foo = IterableExtensions.<ISemanticRegion>head(access.regionForRootEObject().getAllRegionsFor().features(RegionaccesstestlanguagePackage.Literals.VALUE_LIST__NAME));
        it.remove(foo);
    };
    ITextRegionAccess _modify = this._regionAccessTestHelper.modify(access, _function);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append(" ");
    _builder_1.append("0  0   H");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("B ValueList\'[a, b]\' Root");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("0  1    S \"8\"        Root:\'8\'");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("1    1  H \"\\n\"       Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("         ");
    _builder_1.append("\"/*b1*/\"   Comment:TerminalRule\'ML_COMMENT\' Association:NEXT");
    _builder_1.newLine();
    _builder_1.append("         ");
    _builder_1.append("\"\\n\"       Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("         ");
    _builder_1.append("\"/*b2*/\"   Comment:TerminalRule\'ML_COMMENT\' Association:NEXT");
    _builder_1.newLine();
    _builder_1.append("   ");
    _builder_1.append("15    \"\\n\"       Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("16  1    S \"b\"        ValueList:name+=ID");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("E ValueList\'[a, b]\' Root");
    _builder_1.newLine();
    _builder_1.append("17  0   H");
    _builder_1.newLine();
    _builder_1.append("------------ diff 1 ------------");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("1  1 H \"\\n\"       Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("2  1 S \"a\"        ValueList:name+=ID");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("3    H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("\"/*a*/\"    Comment:TerminalRule\'ML_COMMENT\' Association:PREVIOUS");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("\" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("\"//a\\n\"    Comment:TerminalRule\'SL_COMMENT\' Association:PREVIOUS");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("\"/*b1*/\"   Comment:TerminalRule\'ML_COMMENT\' Association:NEXT");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("\"\\n\"       Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("\"/*b2*/\"   Comment:TerminalRule\'ML_COMMENT\' Association:NEXT");
    _builder_1.newLine();
    _builder_1.append("   ");
    _builder_1.append("25   \"\\n\"       Whitespace:TerminalRule\'WS\'");
    _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) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) Test(org.junit.Test)

Example 7 with ITextRegionDiffBuilder

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

the class RegionAccessDiffTest method testEmptyModification.

@Test
public void testEmptyModification() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("1 foo");
    _builder.newLine();
    final ITextRegionAccess access = this._regionAccessTestHelper.toTextRegionAccess(_builder);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("0 0 H");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("B Simple\'foo\' Root");
    _builder_1.newLine();
    _builder_1.append("0 1  S \"1\"        Simple:\'1\'");
    _builder_1.newLine();
    _builder_1.append("1 1  H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_1.newLine();
    _builder_1.append("2 3  S \"foo\"      Simple:name=ID");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("E Simple\'foo\' Root");
    _builder_1.newLine();
    _builder_1.append("5 0 H");
    _builder_1.newLine();
    this._regionAccessTestHelper.operator_tripleEquals(access, _builder_1);
    final Procedure1<ITextRegionDiffBuilder> _function = (ITextRegionDiffBuilder it) -> {
    };
    ITextRegionAccess _modify = this._regionAccessTestHelper.modify(access, _function);
    StringConcatenation _builder_2 = new StringConcatenation();
    _builder_2.append("0 0 H");
    _builder_2.newLine();
    _builder_2.append("    ");
    _builder_2.append("B Simple\'foo\' Root");
    _builder_2.newLine();
    _builder_2.append("0 1  S \"1\"        Simple:\'1\'");
    _builder_2.newLine();
    _builder_2.append("1 1  H \" \"        Whitespace:TerminalRule\'WS\'");
    _builder_2.newLine();
    _builder_2.append("2 3  S \"foo\"      Simple:name=ID");
    _builder_2.newLine();
    _builder_2.append("    ");
    _builder_2.append("E Simple\'foo\' Root");
    _builder_2.newLine();
    _builder_2.append("5 0 H");
    _builder_2.newLine();
    this._regionAccessTestHelper.operator_tripleEquals(_modify, _builder_2);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder) Test(org.junit.Test)

Example 8 with ITextRegionDiffBuilder

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

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

the class RecordingXtextResourceUpdater method beginRecording.

public ITextRegionDiffBuilder beginRecording(IChangeSerializer serializer, XtextResource resource) {
    this.serializer = serializer;
    this.snapshot = snapshotProvider.createResourceSnapshot(resource);
    this.document = new StringBasedTextRegionAccessDiffBuilder(this.snapshot.getRegions());
    EcoreUtil.resolveAll(resource);
    this.recorder = new ChangeRecorder(resource);
    return this.document;
}
Also used : ChangeRecorder(org.eclipse.emf.ecore.change.util.ChangeRecorder) StringBasedTextRegionAccessDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiffBuilder)

Example 10 with ITextRegionDiffBuilder

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

the class RelatedXtextResourceUpdater method applyChange.

@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
    XtextResource res = (XtextResource) lifecycleManager.openAndApplyReferences(getResourceSet(), getResource());
    if (!referenceUpdater.isAffected(deltas, getResource())) {
        return;
    }
    ITextRegionAccess base = textRegionBuilderProvider.get().forNodeModel(res).create();
    ITextRegionDiffBuilder rewriter = new StringBasedTextRegionAccessDiffBuilder(base);
    ReferenceUpdaterContext context = new ReferenceUpdaterContext(deltas, rewriter, getResource());
    referenceUpdater.update(context);
    if (!context.getModifications().isEmpty()) {
        ChangeRecorder rec = new ChangeRecorder(res);
        for (Runnable run : context.getModifications()) {
            run.run();
        }
        ChangeDescription recording = rec.endRecording();
        ResourceSet rs = res.getResourceSet();
        ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, Collections.emptyList(), recording);
        ResourceRecording recordedResource = tree.getRecordedResource(res);
        if (recordedResource != null) {
            serializer.serializeChanges(recordedResource, rewriter);
        }
    }
    for (IUpdatableReference upd : context.getUpdatableReferences()) {
        referenceUpdater.updateReference(rewriter, upd);
    }
    ITextRegionAccessDiff rewritten = rewriter.create();
    List<ITextReplacement> rep = formatter.format(rewritten);
    TextDocumentChange change = new TextDocumentChange(rewritten, getResource().getUri(), rep);
    changeAcceptor.accept(change);
}
Also used : ResourceRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording) ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringBasedTextRegionAccessDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiffBuilder) IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ResourceSetRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceSetRecording) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ChangeRecorder(org.eclipse.emf.ecore.change.util.ChangeRecorder) ChangeDescription(org.eclipse.emf.ecore.change.ChangeDescription) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)

Aggregations

ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)17 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)17 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)16 Test (org.junit.Test)16 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)14 EObject (org.eclipse.emf.ecore.EObject)9 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)6 ValueList (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.ValueList)4 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)3 StringBasedTextRegionAccessDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiffBuilder)3 List (java.util.List)2 ChangeRecorder (org.eclipse.emf.ecore.change.util.ChangeRecorder)2 Delegate (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Delegate)2 PrefixedUnassigned (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.PrefixedUnassigned)2 IUpdatableReference (org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference)2 ChangeDescription (org.eclipse.emf.ecore.change.ChangeDescription)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ITextRegionAccessDiff (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff)1 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)1 StringBasedTextRegionAccessDiff (org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiff)1