Search in sources :

Example 1 with ITextRegionAccessDiff

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

the class RecordingXtextResourceUpdater method applyChange.

@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
    Resource resource = snapshot.getResource();
    ResourceSet rs = resource.getResourceSet();
    ReferenceUpdaterContext ctx = new ReferenceUpdaterContext(deltas, document);
    if (serializer.isUpdateCrossReferences()) {
        referenceUpdater.update(ctx);
        for (Runnable run : ctx.getModifications()) {
            run.run();
        }
    }
    ChangeDescription recording = recorder.endRecording();
    if (recording != null) {
        List<IResourceSnapshot> snapshots = Collections.singletonList(snapshot);
        ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, snapshots, recording);
        ResourceRecording recordedResource = tree.getRecordedResource(resource);
        partialSerializer.serializeChanges(recordedResource, document);
    }
    recorder.dispose();
    List<IUpdatableReference> updatableReferences = ctx.getUpdatableReferences();
    for (IUpdatableReference upd : updatableReferences) {
        referenceUpdater.updateReference(document, upd);
    }
    ITextRegionAccessDiff rewritten = document.create();
    List<ITextReplacement> rep = formatter.format(rewritten);
    URI oldUri = snapshot.getURI();
    TextDocumentChange change = new TextDocumentChange(rewritten, oldUri, rep);
    changeAcceptor.accept(change);
}
Also used : ResourceRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording) ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) 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) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot) ChangeDescription(org.eclipse.emf.ecore.change.ChangeDescription)

Example 2 with ITextRegionAccessDiff

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

the class RegionAccessTestHelper method assertRegions.

public void assertRegions(ITextRegionAccess access, CharSequence expectation) {
    if (access instanceof ITextRegionAccessDiff) {
        assertDiffs((ITextRegionAccessDiff) access);
    }
    String tra1 = cfg(new TextRegionAccessToString().withRegionAccess(access)) + "\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 3 with ITextRegionAccessDiff

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

the class RegionDiffFormatter method mergeReplacements.

protected List<ITextReplacement> mergeReplacements(ITextRegionAccessDiff regions, List<ITextReplacement> rep) {
    ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
    List<ITextReplacement> result = Lists.newArrayList();
    for (ITextSegmentDiff r : regions.getRegionDifferences()) {
        ITextSegment original = r.getOriginalRegion();
        ITextSegment modified = r.getModifiedRegion();
        List<ITextReplacement> local = Lists.newArrayList();
        for (ITextReplacement re : rep) {
            if (modified.contains(re)) {
                local.add(re);
            }
        }
        String text;
        if (local.isEmpty()) {
            text = modified.getText();
        } else {
            text = regions.getRewriter().renderToString(modified, local);
        }
        ITextReplacement replacement = rewriter.createReplacement(original.getOffset(), original.getLength(), text);
        result.add(replacement);
    }
    return result;
}
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)

Example 4 with ITextRegionAccessDiff

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

the class RegionDiffFormatter method createFormatterRequest.

protected FormatterRequest createFormatterRequest(ITextRegionAccessDiff rewritten) {
    FormatterRequest request = formatterRequest.get();
    request.setTextRegionAccess(rewritten);
    request.setRegions(collectRegionsToFormat(rewritten));
    return request;
}
Also used : FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 5 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)

Aggregations

ITextRegionAccessDiff (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff)5 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)5 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)3 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 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 ArrayList (java.util.ArrayList)1 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