[api] refactored the change serializer API

This commit is contained in:
Jan Koehnlein 2017-09-29 13:45:17 +02:00 committed by Moritz Eysholdt
parent b31949c99b
commit fdd7c61451
36 changed files with 509 additions and 533 deletions

View file

@ -52,8 +52,7 @@ class FileAwareTestLanguageImportTest {
val model = rs.contents("inmemory:/file1.fileawaretestlanguage", PackageDeclaration)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model.eResource)
model.name = "newpackage"
serializer.addModification(model, [model.name = "newpackage"])
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
serializer.endRecordChangesToTextDocuments === '''
-------- inmemory:/file1.fileawaretestlanguage (syntax: <offset|text>) ---------
@ -97,8 +96,7 @@ class FileAwareTestLanguageImportTest {
val model = rs.contents("inmemory:/file1.fileawaretestlanguage", PackageDeclaration)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model.eResource)
model.name = "newpackage"
serializer.addModification(model, [model.name = "newpackage"])
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
serializer.endRecordChangesToTextDocuments === '''
-------- inmemory:/file1.fileawaretestlanguage (syntax: <offset|text>) ---------
@ -144,8 +142,7 @@ class FileAwareTestLanguageImportTest {
val model = rs.contents("inmemory:/file1.fileawaretestlanguage", PackageDeclaration)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model.eResource)
model.name = "pkg1"
serializer.addModification(model, [model.name = "pkg1"])
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
serializer.endRecordChangesToTextDocuments === '''
-------- inmemory:/file1.fileawaretestlanguage (syntax: <offset|text>) ---------
@ -183,10 +180,13 @@ class FileAwareTestLanguageImportTest {
val model2 = rs.contents("inmemory:/foo/bar/Y.fileawaretestlanguage", PackageDeclaration)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model1.eResource)
serializer.beginRecordChanges(model2.eResource)
model1.name = "foo2"
model2.name = "foo2.bar"
serializer.addModification(model1) [
name = "foo2"
]
serializer.addModification(model2) [
model2.name = "foo2.bar"
model2.name = "foo2.bar"
]
serializer.endRecordChangesToTextDocuments === '''
-------- inmemory:/foo/X.fileawaretestlanguage (syntax: <offset|text>) ---------
package <8:3|foo2>

View file

@ -84,7 +84,7 @@ class ImportTestHelper {
def Collection<IEmfResourceChange> endRecordChangesToTextDocuments(IChangeSerializer ser) {
val list = newArrayList()
ser.endRecordChanges(CollectionBasedAcceptor.of(list))
ser.applyModifications(CollectionBasedAcceptor.of(list))
return list
}
}

View file

@ -8,7 +8,6 @@
package org.eclipse.xtext.ide.tests.serializer
import com.google.inject.Inject
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue
import org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node
@ -21,6 +20,7 @@ import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
/**
* @author Moritz Eysholdt - Initial contribution and API
*/
@ -47,8 +47,9 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", MandatoryValue)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.name = "bar"
serializer.addModification(model.eResource) [
model.name = "bar"
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#2 <3:3|bar>
@ -66,9 +67,10 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.children.get(0).name = "bazz4"
model.children.get(1).name = "bazz5"
serializer.addModification(model.eResource) [
model.children.get(0).name = "bazz4"
model.children.get(1).name = "bazz5"
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root { <10:4|bazz4>; <16:4|bazz5>; }
@ -87,8 +89,9 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.children.get(0).children += createNode => [name = "bazz"]
serializer.addModification(model.eResource) [
model.children.get(0).children += createNode => [name = "bazz"]
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root { child1 { foo1;<24:1| bazz; >} }
@ -111,8 +114,9 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.children.add(0, createNode => [name = "bazz"])
serializer.addModification(model.eResource) [
model.children.add(0, createNode => [name = "bazz"])
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root {<9:9| bazz; /**/ >child1;
@ -131,9 +135,10 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.children.get(0).children += createNode => [name = "bazz1"]
model.children.get(0).children += createNode => [name = "bazz2"]
serializer.addModification(model.eResource) [
model.children.get(0).children += createNode => [name = "bazz1"]
model.children.get(0).children += createNode => [name = "bazz2"]
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root { child1 { foo1;<24:1| bazz1; bazz2; >} }
@ -151,8 +156,9 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
EcoreUtil.remove(model.children.get(0).children.get(0))
serializer.addModification(model.eResource) [
EcoreUtil.remove(model.children.get(0).children.get(0))
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root { child1 {<18:7| >} }
@ -170,9 +176,10 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
EcoreUtil.remove(model.children.get(1))
EcoreUtil.remove(model.children.get(0))
serializer.addModification(model.eResource) [
EcoreUtil.remove(model.children.get(1))
EcoreUtil.remove(model.children.get(0))
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root {<9:17| >}
@ -190,8 +197,9 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.children.get(0).name = "bazz4"
serializer.addModification(model.eResource) [
model.children.get(0).name = "bazz4"
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root { <10:4|bazz4>; foo2 { ref <27:4|bazz4> } }
@ -211,8 +219,9 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.name = "newroot"
serializer.addModification(model.eResource) [
model.name = "newroot"
]
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
@ -235,8 +244,9 @@ class ChangeSerializerTest {
val model = rs.contents("inmemory:/f.pstl", Node)
val serializer = newChangeSerializer()
serializer.beginRecordChanges(model.eResource)
model.eResource.URI = URI.createURI("inmemory:/x.pstl")
serializer.addModification(model.eResource) [
model.eResource.URI = org.eclipse.emf.common.util.URI.createURI("inmemory:/x.pstl")
]
serializer.endRecordChangesToTextDocuments === '''
----- renamed inmemory:/f.pstl to inmemory:/x.pstl (syntax: <offset|text>) -----
(no changes)

View file

@ -84,7 +84,7 @@ class ChangeSerializerTestHelper {
def Collection<IEmfResourceChange> endRecordChangesToTextDocuments(IChangeSerializer ser) {
val list = newArrayList()
ser.endRecordChanges(CollectionBasedAcceptor.of(list))
ser.applyModifications(CollectionBasedAcceptor.of(list))
return list
}

View file

@ -50,8 +50,9 @@ class ChangeSerializerWithEmfTest {
val model = rs.contents("inmemory:/file1.pstl", EClassRef)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model.eResource)
model.ref = model.ref.EPackage.EClassifiers.get(1) as EClass
serializer.addModification(model) [
model.ref = model.ref.EPackage.EClassifiers.get(1) as EClass
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#21 <4:18|MyPackage.MyClass2>
@ -76,10 +77,11 @@ class ChangeSerializerWithEmfTest {
val model = rs.contents("inmemory:/file1.pstl", Model)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model.eResource)
model.clazz.get(0).name = "ChangedName"
model.clazz.add(0, EcoreFactory.eINSTANCE.createEClass => [name = "NewName"])
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
serializer.addModification(model.eResource) [
model.clazz.get(0).name = "ChangedName"
model.clazz.add(0, EcoreFactory.eINSTANCE.createEClass => [name = "NewName"])
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#20<3:1| NewName ><4:9| ChangedName>

View file

@ -40,9 +40,10 @@ class ChangeSerializerWithImports {
val model = rs.contents("inmemory:/file1.pstl", Node)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model.eResource)
model.children.get(0).name = "newchild"
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
serializer.addModification(model.eResource) [
model.children.get(0).name = "newchild"
Assert.assertEquals(1, model.eResource.resourceSet.resources.size)
]
serializer.endRecordChangesToTextDocuments === '''
----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------
#1 root1 { <11:6|newchild>; }

View file

@ -26,6 +26,7 @@ import org.eclipse.xtext.testing.util.InMemoryURIHandler
import org.junit.Test
import org.junit.runner.RunWith
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtext.ide.serializer.IChangeSerializer
/**
* @author Moritz Eysholdt - Initial contribution and API
@ -521,7 +522,7 @@ class PartialSerializerTest {
}
def private <T extends EObject> ITextRegionAccess recordDiff(Class<T> modelType, CharSequence modelText,
(T)=>void modification) {
IChangeSerializer.IModification<T> modification) {
val fs = new InMemoryURIHandler()
fs += "inmemory:/file1.pstl" -> modelText.toString
@ -529,8 +530,7 @@ class PartialSerializerTest {
val model = rs.contents("inmemory:/file1.pstl", modelType)
val serializer = serializerProvider.get()
serializer.beginRecordChanges(model.eResource)
modification.apply(model)
serializer.addModification(model, modification)
return serializer.endRecordChangesToTextRegions
}

View file

@ -9,6 +9,7 @@ package org.eclipse.xtext.ide.tests.testlanguage.ide.server
import com.google.inject.Inject
import org.eclipse.emf.common.util.ECollections
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.lsp4j.CodeActionParams
import org.eclipse.lsp4j.Command
import org.eclipse.lsp4j.Diagnostic
@ -16,6 +17,7 @@ import org.eclipse.lsp4j.Range
import org.eclipse.lsp4j.TextEdit
import org.eclipse.lsp4j.WorkspaceEdit
import org.eclipse.xtext.ide.serializer.IChangeSerializer
import org.eclipse.xtext.ide.serializer.IEmfResourceChange
import org.eclipse.xtext.ide.serializer.ITextDocumentChange
import org.eclipse.xtext.ide.server.Document
import org.eclipse.xtext.ide.server.codeActions.ICodeActionService
@ -27,7 +29,6 @@ import org.eclipse.xtext.util.CollectionBasedAcceptor
import org.eclipse.xtext.util.StringInputStream
import static org.eclipse.xtext.ide.tests.testlanguage.validation.TestLanguageValidator.*
import org.eclipse.xtext.ide.serializer.IEmfResourceChange
/**
* @author Sven Efftinge - Initial contribution and API
@ -78,14 +79,13 @@ class CodeActionService implements ICodeActionService {
]
}
def private WorkspaceEdit recordWorkspaceEdit(Document doc, XtextResource resource, (XtextResource)=>void mod) {
def private WorkspaceEdit recordWorkspaceEdit(Document doc, XtextResource resource, IChangeSerializer.IModification<Resource> mod) {
val rs = new XtextResourceSet()
val copy = rs.createResource(resource.URI)
copy.load(new StringInputStream(resource.parseResult.rootNode.text), emptyMap)
serializer.beginRecordChanges(copy)
mod.apply(copy as XtextResource)
serializer.addModification(copy, mod)
val documentchanges = <IEmfResourceChange>newArrayList()
serializer.endRecordChanges(CollectionBasedAcceptor.of(documentchanges))
serializer.applyModifications(CollectionBasedAcceptor.of(documentchanges))
return new WorkspaceEdit => [
for (documentchange : documentchanges.filter(ITextDocumentChange)) {
val edits = documentchange.replacements.map [ replacement |

View file

@ -12,6 +12,7 @@ import com.google.inject.Provider;
import java.util.Collection;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.ide.serializer.IEmfResourceChange;
import org.eclipse.xtext.ide.serializer.impl.ChangeSerializer;
import org.eclipse.xtext.ide.tests.importHandling.ImportTestHelper;
@ -72,8 +73,10 @@ public class FileAwareTestLanguageImportTest {
final ResourceSet rs = this._importTestHelper.createResourceSet(fs);
final PackageDeclaration model = this._importTestHelper.<PackageDeclaration>contents(rs, "inmemory:/file1.fileawaretestlanguage", PackageDeclaration.class);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model.eResource());
model.setName("newpackage");
final IChangeSerializer.IModification<PackageDeclaration> _function = (PackageDeclaration it) -> {
model.setName("newpackage");
};
serializer.<PackageDeclaration>addModification(model, _function);
Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._importTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_2 = new StringConcatenation();
@ -141,8 +144,10 @@ public class FileAwareTestLanguageImportTest {
final ResourceSet rs = this._importTestHelper.createResourceSet(fs);
final PackageDeclaration model = this._importTestHelper.<PackageDeclaration>contents(rs, "inmemory:/file1.fileawaretestlanguage", PackageDeclaration.class);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model.eResource());
model.setName("newpackage");
final IChangeSerializer.IModification<PackageDeclaration> _function = (PackageDeclaration it) -> {
model.setName("newpackage");
};
serializer.<PackageDeclaration>addModification(model, _function);
Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._importTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_2 = new StringConcatenation();
@ -213,8 +218,10 @@ public class FileAwareTestLanguageImportTest {
final ResourceSet rs = this._importTestHelper.createResourceSet(fs);
final PackageDeclaration model = this._importTestHelper.<PackageDeclaration>contents(rs, "inmemory:/file1.fileawaretestlanguage", PackageDeclaration.class);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model.eResource());
model.setName("pkg1");
final IChangeSerializer.IModification<PackageDeclaration> _function = (PackageDeclaration it) -> {
model.setName("pkg1");
};
serializer.<PackageDeclaration>addModification(model, _function);
Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._importTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_2 = new StringConcatenation();
@ -271,10 +278,15 @@ public class FileAwareTestLanguageImportTest {
final PackageDeclaration model1 = this._importTestHelper.<PackageDeclaration>contents(rs, "inmemory:/foo/X.fileawaretestlanguage", PackageDeclaration.class);
final PackageDeclaration model2 = this._importTestHelper.<PackageDeclaration>contents(rs, "inmemory:/foo/bar/Y.fileawaretestlanguage", PackageDeclaration.class);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model1.eResource());
serializer.beginRecordChanges(model2.eResource());
model1.setName("foo2");
model2.setName("foo2.bar");
final IChangeSerializer.IModification<PackageDeclaration> _function = (PackageDeclaration it) -> {
it.setName("foo2");
};
serializer.<PackageDeclaration>addModification(model1, _function);
final IChangeSerializer.IModification<PackageDeclaration> _function_1 = (PackageDeclaration it) -> {
model2.setName("foo2.bar");
model2.setName("foo2.bar");
};
serializer.<PackageDeclaration>addModification(model2, _function_1);
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._importTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("-------- inmemory:/foo/X.fileawaretestlanguage (syntax: <offset|text>) ---------");

View file

@ -104,7 +104,7 @@ public class ImportTestHelper {
public Collection<IEmfResourceChange> endRecordChangesToTextDocuments(final IChangeSerializer ser) {
final ArrayList<IEmfResourceChange> list = CollectionLiterals.<IEmfResourceChange>newArrayList();
ser.endRecordChanges(CollectionBasedAcceptor.<IEmfResourceChange>of(list));
ser.applyModifications(CollectionBasedAcceptor.<IEmfResourceChange>of(list));
return list;
}
}

View file

@ -64,8 +64,10 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
model.setName("bar");
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>) -----------------");
@ -89,11 +91,13 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
Node _get = model.getChildren().get(0);
_get.setName("bazz4");
Node _get_1 = model.getChildren().get(1);
_get_1.setName("bazz5");
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
Node _get = model.getChildren().get(0);
_get.setName("bazz4");
Node _get_1 = model.getChildren().get(1);
_get_1.setName("bazz5");
};
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>) -----------------");
@ -119,14 +123,16 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
EList<Node> _children = model.getChildren().get(0).getChildren();
Node _createNode = this.fac.createNode();
final Procedure1<Node> _function = (Node it) -> {
it.setName("bazz");
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
EList<Node> _children = model.getChildren().get(0).getChildren();
Node _createNode = this.fac.createNode();
final Procedure1<Node> _function_1 = (Node it_1) -> {
it_1.setName("bazz");
};
Node _doubleArrow = ObjectExtensions.<Node>operator_doubleArrow(_createNode, _function_1);
_children.add(_doubleArrow);
};
Node _doubleArrow = ObjectExtensions.<Node>operator_doubleArrow(_createNode, _function);
_children.add(_doubleArrow);
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>) -----------------");
@ -159,14 +165,16 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
EList<Node> _children = model.getChildren();
Node _createNode = this.fac.createNode();
final Procedure1<Node> _function = (Node it) -> {
it.setName("bazz");
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
EList<Node> _children = model.getChildren();
Node _createNode = this.fac.createNode();
final Procedure1<Node> _function_1 = (Node it_1) -> {
it_1.setName("bazz");
};
Node _doubleArrow = ObjectExtensions.<Node>operator_doubleArrow(_createNode, _function_1);
_children.add(0, _doubleArrow);
};
Node _doubleArrow = ObjectExtensions.<Node>operator_doubleArrow(_createNode, _function);
_children.add(0, _doubleArrow);
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>) -----------------");
@ -192,21 +200,23 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
EList<Node> _children = model.getChildren().get(0).getChildren();
Node _createNode = this.fac.createNode();
final Procedure1<Node> _function = (Node it) -> {
it.setName("bazz1");
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
EList<Node> _children = model.getChildren().get(0).getChildren();
Node _createNode = this.fac.createNode();
final Procedure1<Node> _function_1 = (Node it_1) -> {
it_1.setName("bazz1");
};
Node _doubleArrow = ObjectExtensions.<Node>operator_doubleArrow(_createNode, _function_1);
_children.add(_doubleArrow);
EList<Node> _children_1 = model.getChildren().get(0).getChildren();
Node _createNode_1 = this.fac.createNode();
final Procedure1<Node> _function_2 = (Node it_1) -> {
it_1.setName("bazz2");
};
Node _doubleArrow_1 = ObjectExtensions.<Node>operator_doubleArrow(_createNode_1, _function_2);
_children_1.add(_doubleArrow_1);
};
Node _doubleArrow = ObjectExtensions.<Node>operator_doubleArrow(_createNode, _function);
_children.add(_doubleArrow);
EList<Node> _children_1 = model.getChildren().get(0).getChildren();
Node _createNode_1 = this.fac.createNode();
final Procedure1<Node> _function_1 = (Node it) -> {
it.setName("bazz2");
};
Node _doubleArrow_1 = ObjectExtensions.<Node>operator_doubleArrow(_createNode_1, _function_1);
_children_1.add(_doubleArrow_1);
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>) -----------------");
@ -230,8 +240,10 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
EcoreUtil.remove(model.getChildren().get(0).getChildren().get(0));
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
EcoreUtil.remove(model.getChildren().get(0).getChildren().get(0));
};
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>) -----------------");
@ -255,9 +267,11 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
EcoreUtil.remove(model.getChildren().get(1));
EcoreUtil.remove(model.getChildren().get(0));
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
EcoreUtil.remove(model.getChildren().get(1));
EcoreUtil.remove(model.getChildren().get(0));
};
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>) -----------------");
@ -281,9 +295,11 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
Node _get = model.getChildren().get(0);
_get.setName("bazz4");
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
Node _get = model.getChildren().get(0);
_get.setName("bazz4");
};
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>) -----------------");
@ -313,8 +329,10 @@ public class ChangeSerializerTest {
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();
serializer.beginRecordChanges(model.eResource());
model.setName("newroot");
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();
@ -347,9 +365,11 @@ public class ChangeSerializerTest {
final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs);
final Node model = this._changeSerializerTestHelper.<Node>contents(rs, "inmemory:/f.pstl", Node.class);
final IChangeSerializer serializer = this._changeSerializerTestHelper.newChangeSerializer();
serializer.beginRecordChanges(model.eResource());
Resource _eResource = model.eResource();
_eResource.setURI(URI.createURI("inmemory:/x.pstl"));
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
Resource _eResource = model.eResource();
_eResource.setURI(URI.createURI("inmemory:/x.pstl"));
};
serializer.<Resource>addModification(model.eResource(), _function);
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("----- renamed inmemory:/f.pstl to inmemory:/x.pstl (syntax: <offset|text>) -----");

View file

@ -104,7 +104,7 @@ public class ChangeSerializerTestHelper {
public Collection<IEmfResourceChange> endRecordChangesToTextDocuments(final IChangeSerializer ser) {
final ArrayList<IEmfResourceChange> list = CollectionLiterals.<IEmfResourceChange>newArrayList();
ser.endRecordChanges(CollectionBasedAcceptor.<IEmfResourceChange>of(list));
ser.applyModifications(CollectionBasedAcceptor.<IEmfResourceChange>of(list));
return list;
}

View file

@ -14,8 +14,10 @@ import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.ide.serializer.IEmfResourceChange;
import org.eclipse.xtext.ide.serializer.impl.ChangeSerializer;
import org.eclipse.xtext.ide.tests.serializer.ChangeSerializerTestHelper;
@ -75,9 +77,11 @@ public class ChangeSerializerWithEmfTest {
final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs);
final EClassRef model = this._changeSerializerTestHelper.<EClassRef>contents(rs, "inmemory:/file1.pstl", EClassRef.class);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model.eResource());
EClassifier _get = model.getRef().getEPackage().getEClassifiers().get(1);
model.setRef(((EClass) _get));
final IChangeSerializer.IModification<EClassRef> _function = (EClassRef it) -> {
EClassifier _get = model.getRef().getEPackage().getEClassifiers().get(1);
model.setRef(((EClass) _get));
};
serializer.<EClassRef>addModification(model, _function);
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------");
@ -116,17 +120,19 @@ public class ChangeSerializerWithEmfTest {
final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs);
final Model model = this._changeSerializerTestHelper.<Model>contents(rs, "inmemory:/file1.pstl", Model.class);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model.eResource());
EClass _get = model.getClazz().get(0);
_get.setName("ChangedName");
EList<EClass> _clazz = model.getClazz();
EClass _createEClass = EcoreFactory.eINSTANCE.createEClass();
final Procedure1<EClass> _function = (EClass it) -> {
it.setName("NewName");
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
EClass _get = model.getClazz().get(0);
_get.setName("ChangedName");
EList<EClass> _clazz = model.getClazz();
EClass _createEClass = EcoreFactory.eINSTANCE.createEClass();
final Procedure1<EClass> _function_1 = (EClass it_1) -> {
it_1.setName("NewName");
};
EClass _doubleArrow = ObjectExtensions.<EClass>operator_doubleArrow(_createEClass, _function_1);
_clazz.add(0, _doubleArrow);
Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
};
EClass _doubleArrow = ObjectExtensions.<EClass>operator_doubleArrow(_createEClass, _function);
_clazz.add(0, _doubleArrow);
Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
serializer.<Resource>addModification(model.eResource(), _function);
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------");

View file

@ -10,8 +10,10 @@ package org.eclipse.xtext.ide.tests.serializer;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.Collection;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.ide.serializer.IEmfResourceChange;
import org.eclipse.xtext.ide.serializer.impl.ChangeSerializer;
import org.eclipse.xtext.ide.tests.serializer.ChangeSerializerTestHelper;
@ -54,10 +56,12 @@ public class ChangeSerializerWithImports {
final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs);
final Node model = this._changeSerializerTestHelper.<Node>contents(rs, "inmemory:/file1.pstl", Node.class);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model.eResource());
Node _get = model.getChildren().get(0);
_get.setName("newchild");
Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
Node _get = model.getChildren().get(0);
_get.setName("newchild");
Assert.assertEquals(1, model.eResource().getResourceSet().getResources().size());
};
serializer.<Resource>addModification(model.eResource(), _function);
Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------");

View file

@ -15,6 +15,7 @@ import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.ide.serializer.impl.ChangeSerializer;
import org.eclipse.xtext.ide.tests.serializer.ChangeSerializerTestHelper;
import org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryChild;
@ -54,7 +55,7 @@ public class PartialSerializerTest {
@Test
public void testMandatoryValueChange() {
final Procedure1<MandatoryValue> _function = (MandatoryValue it) -> {
final IChangeSerializer.IModification<MandatoryValue> _function = (MandatoryValue it) -> {
it.setName("bar");
};
ITextRegionAccess _recordDiff = this.<MandatoryValue>recordDiff(MandatoryValue.class, "#2 foo", _function);
@ -84,7 +85,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalValueInsert() {
final Procedure1<OptionalValue> _function = (OptionalValue it) -> {
final IChangeSerializer.IModification<OptionalValue> _function = (OptionalValue it) -> {
it.setName("foo");
};
ITextRegionAccess _recordDiff = this.<OptionalValue>recordDiff(OptionalValue.class, "#3", _function);
@ -118,7 +119,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalValueChange() {
final Procedure1<OptionalValue> _function = (OptionalValue it) -> {
final IChangeSerializer.IModification<OptionalValue> _function = (OptionalValue it) -> {
it.setName("baz");
};
ITextRegionAccess _recordDiff = this.<OptionalValue>recordDiff(OptionalValue.class, "#3 foo", _function);
@ -148,7 +149,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalValueRemove() {
final Procedure1<OptionalValue> _function = (OptionalValue it) -> {
final IChangeSerializer.IModification<OptionalValue> _function = (OptionalValue it) -> {
it.setName(null);
};
ITextRegionAccess _recordDiff = this.<OptionalValue>recordDiff(OptionalValue.class, "#3 foo", _function);
@ -178,7 +179,7 @@ public class PartialSerializerTest {
@Test
public void testMandatoryChildChange() {
final Procedure1<MandatoryChild> _function = (MandatoryChild it) -> {
final IChangeSerializer.IModification<MandatoryChild> _function = (MandatoryChild it) -> {
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
it_1.setName("baz");
@ -223,7 +224,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildInsert() {
final Procedure1<OptionalChild> _function = (OptionalChild it) -> {
final IChangeSerializer.IModification<OptionalChild> _function = (OptionalChild it) -> {
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
it_1.setName("baz");
@ -268,7 +269,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildChange() {
final Procedure1<OptionalChild> _function = (OptionalChild it) -> {
final IChangeSerializer.IModification<OptionalChild> _function = (OptionalChild it) -> {
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
it_1.setName("baz");
@ -313,7 +314,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildRemove() {
final Procedure1<OptionalChild> _function = (OptionalChild it) -> {
final IChangeSerializer.IModification<OptionalChild> _function = (OptionalChild it) -> {
it.setChild(null);
};
ITextRegionAccess _recordDiff = this.<OptionalChild>recordDiff(OptionalChild.class, "#5 foo", _function);
@ -343,7 +344,7 @@ public class PartialSerializerTest {
@Test
public void testMoveInList1() {
final Procedure1<Node> _function = (Node it) -> {
final IChangeSerializer.IModification<Node> _function = (Node it) -> {
it.getChildren().move(0, 1);
};
ITextRegionAccess _recordDiff = this.<Node>recordDiff(Node.class, "#1 root { Foo; Bar; }", _function);
@ -431,7 +432,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildListInsertIntoEmpty() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EList<MandatoryValue> _children = it.getChildren();
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
@ -473,7 +474,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildListInsertIntoEmpty2() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EList<MandatoryValue> _children = it.getChildren();
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
@ -532,7 +533,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildListInsertIntoFirst() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EList<MandatoryValue> _children = it.getChildren();
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
@ -584,7 +585,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildListInsertIntoMiddle() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EList<MandatoryValue> _children = it.getChildren();
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
@ -654,7 +655,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildListInsertIntoEndOne() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EList<MandatoryValue> _children = it.getChildren();
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
@ -706,7 +707,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildListInsertIntoEndTwo() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EList<MandatoryValue> _children = it.getChildren();
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
@ -775,7 +776,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildListInsertIntoEndThree() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EList<MandatoryValue> _children = it.getChildren();
MandatoryValue _createMandatoryValue = this.fac.createMandatoryValue();
final Procedure1<MandatoryValue> _function_1 = (MandatoryValue it_1) -> {
@ -861,7 +862,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildRemoveListAllOne() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EcoreUtil.remove(it.getChildren().get(0));
};
ITextRegionAccess _recordDiff = this.<OptionalChildList>recordDiff(OptionalChildList.class, "#13 x1", _function);
@ -891,7 +892,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildRemoveListAllTwo() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EcoreUtil.remove(it.getChildren().get(1));
EcoreUtil.remove(it.getChildren().get(0));
};
@ -926,7 +927,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildRemoveListFirstTwo() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EcoreUtil.remove(it.getChildren().get(1));
EcoreUtil.remove(it.getChildren().get(0));
};
@ -974,7 +975,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildRemoveListLastTwo() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EcoreUtil.remove(it.getChildren().get(2));
EcoreUtil.remove(it.getChildren().get(1));
};
@ -1019,7 +1020,7 @@ public class PartialSerializerTest {
@Test
public void testOptionalChildRemoveListMiddleTwo() {
final Procedure1<OptionalChildList> _function = (OptionalChildList it) -> {
final IChangeSerializer.IModification<OptionalChildList> _function = (OptionalChildList it) -> {
EcoreUtil.remove(it.getChildren().get(2));
EcoreUtil.remove(it.getChildren().get(1));
};
@ -1075,7 +1076,7 @@ public class PartialSerializerTest {
this._changeSerializerTestHelper.operator_tripleEquals(_recordDiff, _builder);
}
private <T extends EObject> ITextRegionAccess recordDiff(final Class<T> modelType, final CharSequence modelText, final Procedure1<? super T> modification) {
private <T extends EObject> ITextRegionAccess recordDiff(final Class<T> modelType, final CharSequence modelText, final IChangeSerializer.IModification<T> modification) {
final InMemoryURIHandler fs = new InMemoryURIHandler();
String _string = modelText.toString();
Pair<String, String> _mappedTo = Pair.<String, String>of("inmemory:/file1.pstl", _string);
@ -1083,8 +1084,7 @@ public class PartialSerializerTest {
final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs);
final T model = this._changeSerializerTestHelper.<T>contents(rs, "inmemory:/file1.pstl", modelType);
final ChangeSerializer serializer = this.serializerProvider.get();
serializer.beginRecordChanges(model.eResource());
modification.apply(model);
serializer.<T>addModification(model, modification);
return this._changeSerializerTestHelper.endRecordChangesToTextRegions(serializer);
}
}

View file

@ -105,7 +105,7 @@ public class CodeActionService implements ICodeActionService {
}
private Command fixUnsortedMembers(final Diagnostic d, final Document doc, final XtextResource res, final CodeActionParams params) {
final Procedure1<XtextResource> _function = (XtextResource copiedResource) -> {
final IChangeSerializer.IModification<Resource> _function = (Resource copiedResource) -> {
final Model model = IterableExtensions.<Model>head(Iterables.<Model>filter(copiedResource.getContents(), Model.class));
EList<TypeDeclaration> _types = model.getTypes();
for (final TypeDeclaration type : _types) {
@ -129,17 +129,16 @@ public class CodeActionService implements ICodeActionService {
return ObjectExtensions.<Command>operator_doubleArrow(_command, _function_1);
}
private WorkspaceEdit recordWorkspaceEdit(final Document doc, final XtextResource resource, final Procedure1<? super XtextResource> mod) {
private WorkspaceEdit recordWorkspaceEdit(final Document doc, final XtextResource resource, final IChangeSerializer.IModification<Resource> mod) {
try {
final XtextResourceSet rs = new XtextResourceSet();
final Resource copy = rs.createResource(resource.getURI());
String _text = resource.getParseResult().getRootNode().getText();
StringInputStream _stringInputStream = new StringInputStream(_text);
copy.load(_stringInputStream, CollectionLiterals.<Object, Object>emptyMap());
this.serializer.beginRecordChanges(copy);
mod.apply(((XtextResource) copy));
this.serializer.<Resource>addModification(copy, mod);
final ArrayList<IEmfResourceChange> documentchanges = CollectionLiterals.<IEmfResourceChange>newArrayList();
this.serializer.endRecordChanges(CollectionBasedAcceptor.<IEmfResourceChange>of(documentchanges));
this.serializer.applyModifications(CollectionBasedAcceptor.<IEmfResourceChange>of(documentchanges));
WorkspaceEdit _workspaceEdit = new WorkspaceEdit();
final Procedure1<WorkspaceEdit> _function = (WorkspaceEdit it) -> {
Iterable<ITextDocumentChange> _filter = Iterables.<ITextDocumentChange>filter(documentchanges, ITextDocumentChange.class);

View file

@ -37,7 +37,7 @@ interface IRenameNameValidator {
issues.add(ERROR, '''Illegal name: '«newName»'. Consider using '«text»' instead.''')
}
} catch (ValueConverterException e) {
issues.add(FATAL, '''Illegal name: «e?.message»''')
issues.add(FATAL, '''Illegal name: «e.message»''')
}
}

View file

@ -9,36 +9,32 @@ package org.eclipse.xtext.ide.refactoring
import com.google.inject.ImplementedBy
import com.google.inject.Inject
import java.util.List
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EcorePackage
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity
import org.eclipse.xtext.ide.serializer.IChangeSerializer
import org.eclipse.xtext.resource.IResourceServiceProvider
import org.eclipse.xtext.ide.serializer.IChangeSerializer
/**
* Called to rename an element in the {@link IChangeSerializer} based refactoring.
*
* Clients may extend the {@link DefaultImpl} to customize the behavior or implement
* this interface directly.
*
* Changes are usually performed in the The {@link RenameContext}
*
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@ImplementedBy(IRenameStrategy.Impl)
@ImplementedBy(IRenameStrategy.DefaultImpl)
interface IRenameStrategy {
def void loadAndWatchResources(RenameContext context)
def void applyRename(RenameContext context)
def void applySideEffects(RenameContext context)
def String getCurrentName(EObject element)
class Impl implements IRenameStrategy {
class DefaultImpl implements IRenameStrategy {
@Inject IResourceServiceProvider resourceServiceProvider
@ -46,20 +42,9 @@ interface IRenameStrategy {
resourceServiceProvider.canHandle(change.targetURI)
}
override void loadAndWatchResources(RenameContext context) {
context.changes.filter[ canHandle ].forEach [ change |
val targetResource = context.resourceSet.getResource(change.targetURI.trimFragment, true)
EcoreUtil.resolveAll(targetResource)
context.changeSerializer.beginRecordChanges(targetResource)
]
}
override applyRename(RenameContext context) {
context.changes.filter[ canHandle ].forEach [ change |
val target = context.resourceSet.getEObject(change.targetURI, false)
if (target instanceof EObject) {
doRename(target, change, context)
}
context.addModification(change) [ doRename(change, context) ]
]
}
@ -78,31 +63,7 @@ interface IRenameStrategy {
override getCurrentName(EObject element) {
element.eGet(element.nameEAttribute).toString
}
override applySideEffects(RenameContext context) {
}
}
}
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@FinalFieldsConstructor
@Accessors(PUBLIC_GETTER)
class RenameContext {
val List<? extends RenameChange> changes
val ResourceSet resourceSet
val IChangeSerializer changeSerializer
val RefactoringIssueAcceptor issues
}
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
class RenameChange {
String newName
URI targetURI
}

View file

@ -7,9 +7,6 @@
*******************************************************************************/
package org.eclipse.xtext.ide.refactoring
import com.google.inject.Inject
import org.eclipse.xtext.resource.IResourceServiceProvider
/**
* Allows a language to execute side-effects when the URI of a resource changes.
*
@ -18,8 +15,9 @@ import org.eclipse.xtext.resource.IResourceServiceProvider
* would make sense is Java, where the package name and the name of the first
* public top-level class must match the resource's path.
*
* Clients should not directly implement this interface but extend the
* {@link IResourceRelocationStrategy.AbstractImpl}.
* Clients usually call {@link ResourceRelocationContext.addModification()} to
* register their side effects. This way it is ensured that the resource is
* properly loaded and watched for changes.
*
* In Eclipse, {@link IResourceRelocationStrategy} are registered to an extension
* point.
@ -29,50 +27,8 @@ import org.eclipse.xtext.resource.IResourceServiceProvider
*/
interface IResourceRelocationStrategy {
def void loadAndWatchResources(ResourceRelocationContext context)
def void applyChange(ResourceRelocationContext context)
def void applySideEffects(ResourceRelocationContext context)
/**
* Clients should extend this class to register side-effects on resource relocation changes.
*/
abstract class AbstractImpl implements IResourceRelocationStrategy {
@Inject IResourceServiceProvider resourceServiceProvider
def boolean canHandle(ResourceRelocationChange change) {
resourceServiceProvider.canHandle(change.fromURI)
}
override void loadAndWatchResources(ResourceRelocationContext context) {
context.changes.filter[ isFile && canHandle ].forEach [ change |
if (context.changeType === ResourceRelocationContext.ChangeType.COPY) {
val fromResource = context.resourceSet.createResource(change.fromURI)
fromResource.load(context.resourceSet.URIConverter.createInputStream(change.fromURI), null)
fromResource.URI = change.toURI
context.changeSerializer.beginRecordChanges(fromResource)
} else {
val fromResource = context.resourceSet.getResource(change.fromURI, true)
context.changeSerializer.beginRecordChanges(fromResource)
}
]
}
override void applyChange(ResourceRelocationContext context) {
context.changes.filter[ isFile && canHandle ].forEach[ change |
switch context.changeType {
case MOVE,
case RENAME: {
val fromResource = context.resourceSet.getResource(change.fromURI, false)
fromResource.URI = change.toURI
}
case COPY: {}
}
]
}
}
}

View file

@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.ide.refactoring
import org.eclipse.emf.common.util.URI
import org.eclipse.xtend.lib.annotations.Data
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
class RenameChange {
String newName
URI targetURI
}

View file

@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.ide.refactoring
import java.util.List
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity
import org.eclipse.xtext.ide.serializer.IChangeSerializer
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@FinalFieldsConstructor
@Accessors(PUBLIC_GETTER)
class RenameContext {
val List<? extends RenameChange> changes
val ResourceSet resourceSet
val IChangeSerializer changeSerializer
val RefactoringIssueAcceptor issues
def void addModification(RenameChange change, IChangeSerializer.IModification<EObject> modification) {
val target = resourceSet.getEObject(change.targetURI, true)
if (target instanceof EObject) {
EcoreUtil.resolveAll(target.eResource)
changeSerializer.addModification(target, modification)
} else {
issues.add(Severity.ERROR, 'Element cannot be found', change.targetURI)
}
}
}

View file

@ -8,6 +8,7 @@
package org.eclipse.xtext.ide.refactoring
import java.util.List
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
@ -21,15 +22,52 @@ import org.eclipse.xtext.ide.serializer.IChangeSerializer
@Accessors(PUBLIC_GETTER)
class ResourceRelocationContext {
val ChangeType changeType
val ChangeType changeType
val List<ResourceRelocationChange> changes
val RefactoringIssueAcceptor issueAcceptor
val IChangeSerializer changeSerializer
val ResourceSet resourceSet
enum ChangeType {
COPY, MOVE, RENAME
COPY,
MOVE,
RENAME
}
/**
* Loads and watches the respective resource, applies the relocation change and
* calls the given <code>modification</code> with the renamed/moved/copied resource.
*
* @param change the change to execute
* @param modification the side-effect the rename/move/copy operation should have.
*/
def void addModification(ResourceRelocationChange change, IChangeSerializer.IModification<Resource> modification) {
changeSerializer.addModification(loadAndWatchResource(change), modification)
}
/**
* Loads and watches the respective resource and applies the relocation change.
* Clients may usually rather call {@link #addModification()} to register their
* side-effects.
*
* @param change the change to execute
*/
def Resource loadAndWatchResource(ResourceRelocationChange change) {
val resource = switch changeType {
case MOVE,
case RENAME: {
val original = resourceSet.getResource(change.fromURI, true)
changeSerializer.addModification(original)[original.URI = change.toURI]
original
}
case COPY: {
val copy = resourceSet.createResource(change.fromURI)
copy.load(resourceSet.URIConverter.createInputStream(change.fromURI), null)
copy.URI = change.toURI
copy
}
}
return resource
}
}

View file

@ -1,51 +0,0 @@
/*******************************************************************************
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.ide.refactoring
import java.util.List
import org.apache.log4j.Logger
import static org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity.*
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
class ResourceRelocationStrategyExecutor {
static val LOG = Logger.getLogger(ResourceRelocationStrategyExecutor)
def executeParticipants(List<? extends IResourceRelocationStrategy> strategies, ResourceRelocationContext context) {
if(context.changeType === ResourceRelocationContext.ChangeType.COPY)
context.changeSerializer.updateRelatedFiles = false
strategies.forEach [
try {
loadAndWatchResources(context)
} catch (Throwable t) {
context.issueAcceptor.add(ERROR, 'Error loading resources', t)
LOG.error(t)
}
]
strategies.forEach [
try {
applyChange(context)
} catch (Throwable t) {
context.issueAcceptor.add(ERROR, 'Error applying resource changes', t)
LOG.error(t)
}
]
strategies.forEach [
try {
applySideEffects(context)
} catch (Throwable t) {
context.issueAcceptor.add(ERROR, 'Error applying side effects', t)
LOG.error(t)
}
]
}
}

View file

@ -7,6 +7,7 @@
*******************************************************************************/
package org.eclipse.xtext.ide.serializer;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.formatting2.IFormatter2;
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder;
@ -20,10 +21,11 @@ import com.google.inject.ImplementedBy;
/**
* Converts changes from EMF models to the smallest possible text changes.
*
* The ChangeSerializer records changes that are being made to EMF models between the calls of
* {@link #beginRecordChanges(Resource)} and {@link #endRecordChanges(IAcceptor)}. The result of the recording are
* {@link ITextDocumentChange}s which the describe the changes as {@link ITextReplacement}s. The TextReplacements may
* then be applied to an editor's contents or to a persisted file.
* The ChangeSerializer records changes in modifications that are added using
* {@link #addModification(T,IModification<T>)}. All corresponding
* {@link ITextDocumentChange}s can be retrieved in the end by calling
* {@link #applyModifications(IAcceptor)}. They contain {@link ITextReplacement}
* which may then be applied to an editor's contents or to a persisted file.
*
* In contrast to {@link ISerializer}, the ChangeSerializer aims to produce the smallest text changes possible, can
* handle changes that span multiple files, and is able to update cross references in related files.
@ -39,10 +41,10 @@ import com.google.inject.ImplementedBy;
@ImplementedBy(ChangeSerializer.class)
public interface IChangeSerializer {
void beginRecordChanges(Resource resource);
void endRecordChanges(IAcceptor<IEmfResourceChange> changeAcceptor);
<T extends Notifier> void addModification(T context, IModification<T> modification);
void applyModifications(IAcceptor<IEmfResourceChange> acceptor);
ITextRegionDiffBuilder getModifyableDocument(Resource resource);
boolean isUpdateCrossReferences();
@ -52,4 +54,8 @@ public interface IChangeSerializer {
void setUpdateCrossReferences(boolean value);
void setUpdateRelatedFiles(boolean value);
interface IModification<T extends Notifier> {
void modify(T context);
}
}

View file

@ -12,7 +12,9 @@ import static java.util.stream.Collectors.*;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.IGrammarAccess;
@ -26,8 +28,10 @@ import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.IAcceptor;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.inject.Inject;
/**
@ -49,8 +53,33 @@ public class ChangeSerializer implements IChangeSerializer {
private Map<Resource, RecordingResourceUpdater> updaters = Maps.newLinkedHashMap();
private Multimap<Notifier, IModification<? extends Notifier>> modifications = LinkedHashMultimap.create();
@Override
public void beginRecordChanges(Resource resource) {
public <T extends Notifier> void addModification(T context, IModification<T> modification) {
modifications.put(context, modification);
}
@Override
public void applyModifications(IAcceptor<IEmfResourceChange> changeAcceptor) {
for (Notifier context: modifications.keySet()) {
if (context instanceof EObject)
beginRecordChanges(((EObject)context).eResource());
else if (context instanceof Resource)
beginRecordChanges((Resource) context);
else if (context instanceof ResourceSet)
((ResourceSet) context).getResources().forEach(this::beginRecordChanges);
}
for (Map.Entry<Notifier, IModification<? extends Notifier>> entry: modifications.entries())
apply(entry.getKey(), entry.getValue());
endRecordChanges(changeAcceptor);
}
protected <T extends Notifier> void apply(Notifier context, IModification<T> modification) {
modification.modify((T) context);
}
protected void beginRecordChanges(Resource resource) {
RecordingResourceUpdater updater = updaters.get(resource);
if (updater != null) {
return;
@ -89,8 +118,7 @@ public class ChangeSerializer implements IChangeSerializer {
}
}
@Override
public void endRecordChanges(IAcceptor<IEmfResourceChange> changeAcceptor) {
protected void endRecordChanges(IAcceptor<IEmfResourceChange> changeAcceptor) {
if (updaters.isEmpty()) {
return;
}

View file

@ -79,7 +79,7 @@ public class ReferenceUpdater implements IReferenceUpdater {
protected boolean needsUpdating(Deltas deltas, EObject source, EObject target) {
Delta targetDelta = deltas.findContainingDelta(target);
if (targetDelta.getObject() == target)
if (targetDelta != null && targetDelta.getObject() == target)
return true;
Delta sourceDelta = deltas.findContainingDelta(source);
return !Objects.equal(sourceDelta, targetDelta);

View file

@ -57,11 +57,9 @@ class RenameService implements IRenameService {
val change = new RenameChange(renameParams.newName, EcoreUtil.getURI(element))
val changeSerializer = changeSerializerProvider.get
val context = new RenameContext(#[change], resourceSet, changeSerializer, issueAcceptor)
renameStrategy.loadAndWatchResources(context)
renameStrategy.applyRename(context)
renameStrategy.applySideEffects(context)
val changeConverter = converterFactory.create(workspaceManager, workspaceEdit)
changeSerializer.endRecordChanges(changeConverter)
changeSerializer.applyModifications(changeConverter)
} else {
issueAcceptor.add(FATAL, 'Loaded resource is not an XtextResource', resource.URI)
}

View file

@ -49,10 +49,7 @@ public interface IRenameNameValidator {
final ValueConverterException e = (ValueConverterException)_t;
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("Illegal name: ");
String _message = null;
if (e!=null) {
_message=e.getMessage();
}
String _message = e.getMessage();
_builder_1.append(_message);
issues.add(RefactoringIssueAcceptor.Severity.FATAL, _builder_1.toString());
} else {

View file

@ -14,23 +14,29 @@ import java.util.function.Consumer;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
import org.eclipse.xtext.ide.refactoring.RenameChange;
import org.eclipse.xtext.ide.refactoring.RenameContext;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
/**
* Called to rename an element in the {@link IChangeSerializer} based refactoring.
*
* Clients may extend the {@link DefaultImpl} to customize the behavior or implement
* this interface directly.
*
* Changes are usually performed in the The {@link RenameContext}
*
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@ImplementedBy(IRenameStrategy.Impl.class)
@ImplementedBy(IRenameStrategy.DefaultImpl.class)
@SuppressWarnings("all")
public interface IRenameStrategy {
public static class Impl implements IRenameStrategy {
public static class DefaultImpl implements IRenameStrategy {
@Inject
private IResourceServiceProvider resourceServiceProvider;
@ -38,29 +44,16 @@ public interface IRenameStrategy {
return this.resourceServiceProvider.canHandle(change.getTargetURI());
}
@Override
public void loadAndWatchResources(final RenameContext context) {
final Function1<RenameChange, Boolean> _function = (RenameChange it) -> {
return Boolean.valueOf(this.canHandle(it));
};
final Consumer<RenameChange> _function_1 = (RenameChange change) -> {
final Resource targetResource = context.getResourceSet().getResource(change.getTargetURI().trimFragment(), true);
EcoreUtil.resolveAll(targetResource);
context.getChangeSerializer().beginRecordChanges(targetResource);
};
IterableExtensions.filter(context.getChanges(), _function).forEach(_function_1);
}
@Override
public void applyRename(final RenameContext context) {
final Function1<RenameChange, Boolean> _function = (RenameChange it) -> {
return Boolean.valueOf(this.canHandle(it));
};
final Consumer<RenameChange> _function_1 = (RenameChange change) -> {
final EObject target = context.getResourceSet().getEObject(change.getTargetURI(), false);
if ((target instanceof EObject)) {
this.doRename(target, change, context);
}
final IChangeSerializer.IModification<EObject> _function_2 = (EObject it) -> {
this.doRename(it, change, context);
};
context.addModification(change, _function_2);
};
IterableExtensions.filter(context.getChanges(), _function).forEach(_function_1);
}
@ -88,17 +81,9 @@ public interface IRenameStrategy {
public String getCurrentName(final EObject element) {
return element.eGet(this.getNameEAttribute(element)).toString();
}
@Override
public void applySideEffects(final RenameContext context) {
}
}
public abstract void loadAndWatchResources(final RenameContext context);
public abstract void applyRename(final RenameContext context);
public abstract void applySideEffects(final RenameContext context);
public abstract String getCurrentName(final EObject element);
}

View file

@ -7,15 +7,7 @@
*/
package org.eclipse.xtext.ide.refactoring;
import com.google.inject.Inject;
import java.util.function.Consumer;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.ide.refactoring.ResourceRelocationChange;
import org.eclipse.xtext.ide.refactoring.ResourceRelocationContext;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
/**
* Allows a language to execute side-effects when the URI of a resource changes.
@ -25,8 +17,9 @@ import org.eclipse.xtext.xbase.lib.IterableExtensions;
* would make sense is Java, where the package name and the name of the first
* public top-level class must match the resource's path.
*
* Clients should not directly implement this interface but extend the
* {@link IResourceRelocationStrategy.AbstractImpl}.
* Clients usually call {@link ResourceRelocationContext.addModification()} to
* register their side effects. This way it is ensured that the resource is
* properly loaded and watched for changes.
*
* In Eclipse, {@link IResourceRelocationStrategy} are registered to an extension
* point.
@ -36,70 +29,5 @@ import org.eclipse.xtext.xbase.lib.IterableExtensions;
*/
@SuppressWarnings("all")
public interface IResourceRelocationStrategy {
/**
* Clients should extend this class to register side-effects on resource relocation changes.
*/
public static abstract class AbstractImpl implements IResourceRelocationStrategy {
@Inject
private IResourceServiceProvider resourceServiceProvider;
public boolean canHandle(final ResourceRelocationChange change) {
return this.resourceServiceProvider.canHandle(change.getFromURI());
}
@Override
public void loadAndWatchResources(final ResourceRelocationContext context) {
final Function1<ResourceRelocationChange, Boolean> _function = (ResourceRelocationChange it) -> {
return Boolean.valueOf((it.isFile() && this.canHandle(it)));
};
final Consumer<ResourceRelocationChange> _function_1 = (ResourceRelocationChange change) -> {
try {
ResourceRelocationContext.ChangeType _changeType = context.getChangeType();
boolean _tripleEquals = (_changeType == ResourceRelocationContext.ChangeType.COPY);
if (_tripleEquals) {
final Resource fromResource = context.getResourceSet().createResource(change.getFromURI());
fromResource.load(context.getResourceSet().getURIConverter().createInputStream(change.getFromURI()), null);
fromResource.setURI(change.getToURI());
context.getChangeSerializer().beginRecordChanges(fromResource);
} else {
final Resource fromResource_1 = context.getResourceSet().getResource(change.getFromURI(), true);
context.getChangeSerializer().beginRecordChanges(fromResource_1);
}
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
};
IterableExtensions.<ResourceRelocationChange>filter(context.getChanges(), _function).forEach(_function_1);
}
@Override
public void applyChange(final ResourceRelocationContext context) {
final Function1<ResourceRelocationChange, Boolean> _function = (ResourceRelocationChange it) -> {
return Boolean.valueOf((it.isFile() && this.canHandle(it)));
};
final Consumer<ResourceRelocationChange> _function_1 = (ResourceRelocationChange change) -> {
ResourceRelocationContext.ChangeType _changeType = context.getChangeType();
if (_changeType != null) {
switch (_changeType) {
case MOVE:
case RENAME:
final Resource fromResource = context.getResourceSet().getResource(change.getFromURI(), false);
fromResource.setURI(change.getToURI());
break;
case COPY:
break;
default:
break;
}
}
};
IterableExtensions.<ResourceRelocationChange>filter(context.getChanges(), _function).forEach(_function_1);
}
}
public abstract void loadAndWatchResources(final ResourceRelocationContext context);
public abstract void applyChange(final ResourceRelocationContext context);
public abstract void applySideEffects(final ResourceRelocationContext context);
}

View file

@ -8,7 +8,9 @@
package org.eclipse.xtext.ide.refactoring;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
@ -33,6 +35,16 @@ public class RenameContext {
private final RefactoringIssueAcceptor issues;
public void addModification(final RenameChange change, final IChangeSerializer.IModification<EObject> modification) {
final EObject target = this.resourceSet.getEObject(change.getTargetURI(), true);
if ((target instanceof EObject)) {
EcoreUtil.resolveAll(target.eResource());
this.changeSerializer.<EObject>addModification(target, modification);
} else {
this.issues.add(RefactoringIssueAcceptor.Severity.ERROR, "Element cannot be found", change.getTargetURI());
}
}
public RenameContext(final List<? extends RenameChange> changes, final ResourceSet resourceSet, final IChangeSerializer changeSerializer, final RefactoringIssueAcceptor issues) {
super();
this.changes = changes;

View file

@ -8,6 +8,7 @@
package org.eclipse.xtext.ide.refactoring;
import java.util.List;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors;
@ -15,6 +16,7 @@ import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
import org.eclipse.xtext.ide.refactoring.ResourceRelocationChange;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Pure;
/**
@ -43,6 +45,64 @@ public class ResourceRelocationContext {
private final ResourceSet resourceSet;
/**
* Loads and watches the respective resource, applies the relocation change and
* calls the given <code>modification</code> with the renamed/moved/copied resource.
*
* @param change the change to execute
* @param modification the side-effect the rename/move/copy operation should have.
*/
public void addModification(final ResourceRelocationChange change, final IChangeSerializer.IModification<Resource> modification) {
this.changeSerializer.<Resource>addModification(this.loadAndWatchResource(change), modification);
}
/**
* Loads and watches the respective resource and applies the relocation change.
* Clients may usually rather call {@link #addModification()} to register their
* side-effects.
*
* @param change the change to execute
*/
public Resource loadAndWatchResource(final ResourceRelocationChange change) {
try {
Resource _switchResult = null;
final ResourceRelocationContext.ChangeType changeType = this.changeType;
if (changeType != null) {
switch (changeType) {
case MOVE:
case RENAME:
Resource _xblockexpression = null;
{
final Resource original = this.resourceSet.getResource(change.getFromURI(), true);
final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
original.setURI(change.getToURI());
};
this.changeSerializer.<Resource>addModification(original, _function);
_xblockexpression = original;
}
_switchResult = _xblockexpression;
break;
case COPY:
Resource _xblockexpression_1 = null;
{
final Resource copy = this.resourceSet.createResource(change.getFromURI());
copy.load(this.resourceSet.getURIConverter().createInputStream(change.getFromURI()), null);
copy.setURI(change.getToURI());
_xblockexpression_1 = copy;
}
_switchResult = _xblockexpression_1;
break;
default:
break;
}
}
final Resource resource = _switchResult;
return resource;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
public ResourceRelocationContext(final ResourceRelocationContext.ChangeType changeType, final List<ResourceRelocationChange> changes, final RefactoringIssueAcceptor issueAcceptor, final IChangeSerializer changeSerializer, final ResourceSet resourceSet) {
super();
this.changeType = changeType;

View file

@ -1,77 +0,0 @@
/**
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.ide.refactoring;
import java.util.List;
import java.util.function.Consumer;
import org.apache.log4j.Logger;
import org.eclipse.xtext.ide.refactoring.IResourceRelocationStrategy;
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
import org.eclipse.xtext.ide.refactoring.ResourceRelocationContext;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.xbase.lib.Exceptions;
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@SuppressWarnings("all")
public class ResourceRelocationStrategyExecutor {
private final static Logger LOG = Logger.getLogger(ResourceRelocationStrategyExecutor.class);
public void executeParticipants(final List<? extends IResourceRelocationStrategy> strategies, final ResourceRelocationContext context) {
ResourceRelocationContext.ChangeType _changeType = context.getChangeType();
boolean _tripleEquals = (_changeType == ResourceRelocationContext.ChangeType.COPY);
if (_tripleEquals) {
IChangeSerializer _changeSerializer = context.getChangeSerializer();
_changeSerializer.setUpdateRelatedFiles(false);
}
final Consumer<IResourceRelocationStrategy> _function = (IResourceRelocationStrategy it) -> {
try {
it.loadAndWatchResources(context);
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable t = (Throwable)_t;
context.getIssueAcceptor().add(RefactoringIssueAcceptor.Severity.ERROR, "Error loading resources", t);
ResourceRelocationStrategyExecutor.LOG.error(t);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
};
strategies.forEach(_function);
final Consumer<IResourceRelocationStrategy> _function_1 = (IResourceRelocationStrategy it) -> {
try {
it.applyChange(context);
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable t = (Throwable)_t;
context.getIssueAcceptor().add(RefactoringIssueAcceptor.Severity.ERROR, "Error applying resource changes", t);
ResourceRelocationStrategyExecutor.LOG.error(t);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
};
strategies.forEach(_function_1);
final Consumer<IResourceRelocationStrategy> _function_2 = (IResourceRelocationStrategy it) -> {
try {
it.applySideEffects(context);
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable t = (Throwable)_t;
context.getIssueAcceptor().add(RefactoringIssueAcceptor.Severity.ERROR, "Error applying side effects", t);
ResourceRelocationStrategyExecutor.LOG.error(t);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
};
strategies.forEach(_function_2);
}
}

View file

@ -92,11 +92,9 @@ public class RenameService implements IRenameService {
final RenameChange change = new RenameChange(_newName, _uRI);
final IChangeSerializer changeSerializer = this.changeSerializerProvider.get();
final RenameContext context = new RenameContext(Collections.<RenameChange>unmodifiableList(CollectionLiterals.<RenameChange>newArrayList(change)), resourceSet, changeSerializer, issueAcceptor);
this.renameStrategy.loadAndWatchResources(context);
this.renameStrategy.applyRename(context);
this.renameStrategy.applySideEffects(context);
final ChangeConverter changeConverter = this.converterFactory.create(workspaceManager, workspaceEdit);
changeSerializer.endRecordChanges(changeConverter);
changeSerializer.applyModifications(changeConverter);
} else {
issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, "Loaded resource is not an XtextResource", resource.getURI());
}

View file

@ -1,19 +1,29 @@
package org.eclipse.xtext.testlanguages.fileAware.ide.refactoring
import com.google.inject.Inject
import org.eclipse.xtext.ide.refactoring.IResourceRelocationStrategy
import org.eclipse.xtext.ide.refactoring.ResourceRelocationChange
import org.eclipse.xtext.ide.refactoring.ResourceRelocationContext
import org.eclipse.xtext.resource.IResourceServiceProvider
import org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration
class FileAwareTestLanguageResourceRelocationStrategy extends IResourceRelocationStrategy.AbstractImpl {
class FileAwareTestLanguageResourceRelocationStrategy implements IResourceRelocationStrategy {
override applySideEffects(ResourceRelocationContext context) {
context.changes.filter[ isFile && canHandle ].forEach[ change |
val resource = context.resourceSet.getResource(change.toURI, false)
val rootElement = resource.contents.head
if (rootElement instanceof PackageDeclaration) {
val newPackage = change.toURI.trimSegments(1).segmentsList.drop(2).join('.')
rootElement.name = newPackage
}
@Inject IResourceServiceProvider resourceServiceProvider
def boolean canHandle(ResourceRelocationChange change) {
resourceServiceProvider.canHandle(change.fromURI)
}
override applyChange(ResourceRelocationContext context) {
context.changes.filter[ canHandle ].forEach [ change |
context.addModification(change) [ resource |
val rootElement = resource.contents.head
if (rootElement instanceof PackageDeclaration) {
val newPackage = change.toURI.trimSegments(1).segmentsList.drop(2).join('.')
rootElement.name = newPackage
}
]
]
}
}

View file

@ -1,29 +1,41 @@
package org.eclipse.xtext.testlanguages.fileAware.ide.refactoring;
import com.google.inject.Inject;
import java.util.function.Consumer;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.ide.refactoring.IResourceRelocationStrategy;
import org.eclipse.xtext.ide.refactoring.ResourceRelocationChange;
import org.eclipse.xtext.ide.refactoring.ResourceRelocationContext;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
@SuppressWarnings("all")
public class FileAwareTestLanguageResourceRelocationStrategy extends IResourceRelocationStrategy.AbstractImpl {
public class FileAwareTestLanguageResourceRelocationStrategy implements IResourceRelocationStrategy {
@Inject
private IResourceServiceProvider resourceServiceProvider;
public boolean canHandle(final ResourceRelocationChange change) {
return this.resourceServiceProvider.canHandle(change.getFromURI());
}
@Override
public void applySideEffects(final ResourceRelocationContext context) {
public void applyChange(final ResourceRelocationContext context) {
final Function1<ResourceRelocationChange, Boolean> _function = (ResourceRelocationChange it) -> {
return Boolean.valueOf((it.isFile() && this.canHandle(it)));
return Boolean.valueOf(this.canHandle(it));
};
final Consumer<ResourceRelocationChange> _function_1 = (ResourceRelocationChange change) -> {
final Resource resource = context.getResourceSet().getResource(change.getToURI(), false);
final EObject rootElement = IterableExtensions.<EObject>head(resource.getContents());
if ((rootElement instanceof PackageDeclaration)) {
final String newPackage = IterableExtensions.join(IterableExtensions.<String>drop(change.getToURI().trimSegments(1).segmentsList(), 2), ".");
((PackageDeclaration)rootElement).setName(newPackage);
}
final IChangeSerializer.IModification<Resource> _function_2 = (Resource resource) -> {
final EObject rootElement = IterableExtensions.<EObject>head(resource.getContents());
if ((rootElement instanceof PackageDeclaration)) {
final String newPackage = IterableExtensions.join(IterableExtensions.<String>drop(change.getToURI().trimSegments(1).segmentsList(), 2), ".");
((PackageDeclaration)rootElement).setName(newPackage);
}
};
context.addModification(change, _function_2);
};
IterableExtensions.<ResourceRelocationChange>filter(context.getChanges(), _function).forEach(_function_1);
}