Search in sources :

Example 31 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet in project xtext-core by eclipse.

the class ChangeSerializer method applyModifications.

@Override
public void applyModifications(IAcceptor<IEmfResourceChange> changeAcceptor) {
    monitor.setTaskName("Preparing Text Changes...");
    Set<Resource> resources = Sets.newLinkedHashSet();
    for (Pair<Notifier, IModification<? extends Notifier>> p : modifications) {
        Notifier context = p.getFirst();
        if (context instanceof EObject)
            resources.add(((EObject) context).eResource());
        else if (context instanceof Resource)
            resources.add((Resource) context);
        else if (context instanceof ResourceSet) {
            throw new IllegalStateException("Not supported");
        }
    }
    for (Resource res : resources) {
        // TODO: use the exact context
        beginRecordChanges(res);
    }
    checkCanceled();
    for (Pair<Notifier, IModification<? extends Notifier>> entry : modifications) {
        apply(entry.getFirst(), entry.getSecond());
    }
    checkCanceled();
    endRecordChanges(changeAcceptor);
}
Also used : EObject(org.eclipse.emf.ecore.EObject) RelatedResource(org.eclipse.xtext.ide.serializer.impl.RelatedResourcesProvider.RelatedResource) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Notifier(org.eclipse.emf.common.notify.Notifier)

Example 32 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet in project xtext-core by eclipse.

the class EObjectSnapshotProvider method createEObjectSnapshots.

protected Map<EObject, IEObjectSnapshot> createEObjectSnapshots(Resource resource) {
    Map<EObject, IEObjectSnapshot> result = Maps.newLinkedHashMap();
    for (IEObjectDescription desc : descriptionProvider.getEObjectDescriptions(resource)) {
        EObject obj = EcoreUtil.resolve(desc.getEObjectOrProxy(), resource);
        EObjectSnapshot snapshot = getOrCreate(result, obj);
        snapshot.descriptions.add(desc);
    }
    ResourceSet resourceSet = resource.getResourceSet();
    IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(resourceSet);
    URI uri = resource.getURI();
    for (IResourceDescription desc : descriptions.getAllResourceDescriptions()) {
        for (IReferenceDescription ref : desc.getReferenceDescriptions()) {
            URI targetURI = ref.getTargetEObjectUri();
            URI sourceEObjectUri = ref.getSourceEObjectUri();
            EReference reference = ref.getEReference();
            if (targetURI == null || sourceEObjectUri == null || reference == null) {
                continue;
            }
            if (!uri.equals(targetURI.trimFragment())) {
                continue;
            }
            EObject target = resource.getEObject(targetURI.fragment());
            if (target == null || target.eIsProxy()) {
                continue;
            }
            EObjectSnapshot snapshot = getOrCreate(result, target);
            ReferenceSnapshot rd = new ReferenceSnapshot(sourceEObjectUri, snapshot, reference, ref.getIndexInList(), ref.getContainerEObjectURI());
            snapshot.incomingReferences.add(rd);
        }
    }
    return result;
}
Also used : IEObjectSnapshot(org.eclipse.xtext.ide.serializer.hooks.IEObjectSnapshot) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IEObjectSnapshot(org.eclipse.xtext.ide.serializer.hooks.IEObjectSnapshot) URI(org.eclipse.emf.common.util.URI) IReferenceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IReferenceSnapshot) EReference(org.eclipse.emf.ecore.EReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription)

Example 33 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet 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);
    }
    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 34 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet in project xtext-core by eclipse.

the class ChangeSerializerTest method testRenameGlobal1.

@Test
public void testRenameGlobal1() {
    final InMemoryURIHandler fs = new InMemoryURIHandler();
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("#1 root1;");
    Pair<String, String> _mappedTo = Pair.<String, String>of("inmemory:/file1.pstl", _builder.toString());
    this._changeSerializerTestHelper.operator_add(fs, _mappedTo);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("#1 root2 { ref root1 }");
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("inmemory:/file2.pstl", _builder_1.toString());
    this._changeSerializerTestHelper.operator_add(fs, _mappedTo_1);
    final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs);
    final Node model = this._changeSerializerTestHelper.<Node>contents(rs, "inmemory:/file1.pstl", Node.class);
    final IChangeSerializer serializer = this._changeSerializerTestHelper.newChangeSerializer();
    final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
        model.setName("newroot");
    };
    serializer.<Resource>addModification(model.eResource(), _function);
    Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
    Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
    StringConcatenation _builder_2 = new StringConcatenation();
    _builder_2.append("----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------");
    _builder_2.newLine();
    _builder_2.append("#1 <3:5|newroot>;");
    _builder_2.newLine();
    _builder_2.append("--------------------------------------------------------------------------------");
    _builder_2.newLine();
    _builder_2.append("3 5 \"root1\" -> \"newroot\"");
    _builder_2.newLine();
    _builder_2.append("----------------- inmemory:/file2.pstl (syntax: <offset|text>) -----------------");
    _builder_2.newLine();
    _builder_2.append("#1 root2 { ref <15:5|newroot> }");
    _builder_2.newLine();
    _builder_2.append("--------------------------------------------------------------------------------");
    _builder_2.newLine();
    _builder_2.append("15 5 \"root1\" -> \"newroot\"");
    _builder_2.newLine();
    this._changeSerializerTestHelper.operator_tripleEquals(_endRecordChangesToTextDocuments, _builder_2);
}
Also used : IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) InMemoryURIHandler(org.eclipse.xtext.testing.util.InMemoryURIHandler) Node(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IEmfResourceChange(org.eclipse.xtext.ide.serializer.IEmfResourceChange) Test(org.junit.Test)

Example 35 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet in project xtext-core by eclipse.

the class ChangeSerializerTest method testSimple.

@Test
public void testSimple() {
    final InMemoryURIHandler fs = new InMemoryURIHandler();
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("#2 foo");
    Pair<String, String> _mappedTo = Pair.<String, String>of("inmemory:/file1.pstl", _builder.toString());
    this._changeSerializerTestHelper.operator_add(fs, _mappedTo);
    final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs);
    final MandatoryValue model = this._changeSerializerTestHelper.<MandatoryValue>contents(rs, "inmemory:/file1.pstl", MandatoryValue.class);
    final IChangeSerializer serializer = this._changeSerializerTestHelper.newChangeSerializer();
    final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
        model.setName("bar");
    };
    serializer.<Resource>addModification(model.eResource(), _function);
    Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------");
    _builder_1.newLine();
    _builder_1.append("#2 <3:3|bar>");
    _builder_1.newLine();
    _builder_1.append("--------------------------------------------------------------------------------");
    _builder_1.newLine();
    _builder_1.append("3 3 \"foo\" -> \"bar\"");
    _builder_1.newLine();
    this._changeSerializerTestHelper.operator_tripleEquals(_endRecordChangesToTextDocuments, _builder_1);
}
Also used : MandatoryValue(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) InMemoryURIHandler(org.eclipse.xtext.testing.util.InMemoryURIHandler) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IEmfResourceChange(org.eclipse.xtext.ide.serializer.IEmfResourceChange) Test(org.junit.Test)

Aggregations

ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)123 Resource (org.eclipse.emf.ecore.resource.Resource)71 Test (org.junit.Test)51 EObject (org.eclipse.emf.ecore.EObject)34 URI (org.eclipse.emf.common.util.URI)32 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)32 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)21 InMemoryURIHandler (org.eclipse.xtext.testing.util.InMemoryURIHandler)21 IEmfResourceChange (org.eclipse.xtext.ide.serializer.IEmfResourceChange)20 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)17 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)17 XtextResource (org.eclipse.xtext.resource.XtextResource)16 EPackage (org.eclipse.emf.ecore.EPackage)12 IFile (org.eclipse.core.resources.IFile)10 Node (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node)10 ArrayList (java.util.ArrayList)9 EClass (org.eclipse.emf.ecore.EClass)9 ChangeSerializer (org.eclipse.xtext.ide.serializer.impl.ChangeSerializer)9 IOException (java.io.IOException)8 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)8