Search in sources :

Example 6 with ITextRegionAccessDiff

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

Example 7 with ITextRegionAccessDiff

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

the class RegionAccessTestHelper method operator_tripleEquals.

public void operator_tripleEquals(final ITextRegionAccess access, final CharSequence expectation) {
    if ((access instanceof ITextRegionAccessDiff)) {
        this.assertDiffs(((ITextRegionAccessDiff) access));
    }
    TextRegionAccessToString _cfg = this.cfg(new TextRegionAccessToString().withRegionAccess(access));
    final String tra1 = (_cfg + "\n");
    Assert.assertEquals(Strings.toPlatformLineSeparator(expectation), Strings.toPlatformLineSeparator(tra1));
}
Also used : ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)

Example 8 with ITextRegionAccessDiff

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

the class RegionAccessTestHelper method assertDiffs.

public void assertDiffs(final ITextRegionAccessDiff regions) {
    final ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
    final ArrayList<ITextReplacement> result = Lists.<ITextReplacement>newArrayList();
    List<ITextSegmentDiff> _regionDifferences = regions.getRegionDifferences();
    for (final ITextSegmentDiff r : _regionDifferences) {
        {
            final ITextSegment originalRegion = r.getOriginalRegion();
            final ITextSegment modifiedRegion = r.getModifiedRegion();
            final ITextReplacement replacement = originalRegion.replaceWith(modifiedRegion.getText());
            result.add(replacement);
        }
    }
    final String actual = rewriter.renderToString(result);
    final String expected = regions.regionForDocument().getText();
    Assert.assertEquals(expected, actual);
}
Also used : ITextRegionRewriter(org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter) ITextSegmentDiff(org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)

Aggregations

ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)5 ITextRegionAccessDiff (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff)4 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)3 ITextSegmentDiff (org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff)3 ChangeDescription (org.eclipse.emf.ecore.change.ChangeDescription)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2 ITextRegionRewriter (org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter)2 IUpdatableReference (org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference)2 ResourceRecording (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording)2 ResourceSetRecording (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceSetRecording)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 URI (org.eclipse.emf.common.util.URI)1 ChangeRecorder (org.eclipse.emf.ecore.change.util.ChangeRecorder)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)1 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)1 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)1 StringBasedTextRegionAccessDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiffBuilder)1