mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
[refactoring] changes based on internal discussions
- use one strategy call for all changes - have a single change type per context - use changeSerializer.updateRelatedFiles = false for copy refactorings - load resource, resolve it, change the URI and then startRecording for copy
This commit is contained in:
parent
c4d33d4186
commit
bee8529bf5
10 changed files with 178 additions and 250 deletions
|
@ -7,11 +7,9 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import com.google.inject.Inject
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider
|
||||
import org.eclipse.emf.ecore.resource.Resource
|
||||
import org.eclipse.xtext.EcoreUtil2
|
||||
|
||||
/**
|
||||
* Allows a language to execute side-effects when the URI of a resource changes.
|
||||
|
@ -32,13 +30,11 @@ import org.eclipse.emf.ecore.resource.Resource
|
|||
*/
|
||||
interface IResourceRelocationStrategy {
|
||||
|
||||
def boolean canHandle(ResourceRelocationChange change)
|
||||
|
||||
def Resource loadAndWatchResource(ResourceRelocationChange change, ResourceRelocationContext context)
|
||||
def void loadAndWatchResources(ResourceRelocationContext context)
|
||||
|
||||
def void applyChange(ResourceRelocationChange change, Resource resource, ResourceRelocationContext context)
|
||||
def void applyChange(ResourceRelocationContext context)
|
||||
|
||||
def void applySideEffects(ResourceRelocationChange change, Resource resource, ResourceRelocationContext context)
|
||||
def void applySideEffects(ResourceRelocationContext context)
|
||||
|
||||
/**
|
||||
* Clients should extend this class to register side-effects on resource relocation changes.
|
||||
|
@ -47,37 +43,35 @@ interface IResourceRelocationStrategy {
|
|||
|
||||
@Inject IResourceServiceProvider resourceServiceProvider
|
||||
|
||||
override boolean canHandle(ResourceRelocationChange change) {
|
||||
def boolean canHandle(ResourceRelocationChange change) {
|
||||
resourceServiceProvider.canHandle(change.fromURI)
|
||||
}
|
||||
|
||||
override Resource loadAndWatchResource(ResourceRelocationChange change, ResourceRelocationContext context) {
|
||||
val fromResource = context.resourceSet.getResource(change.fromURI, true)
|
||||
if (change.type === ResourceRelocationChange.Type.COPY) {
|
||||
val copy = context.resourceSet.createResource(change.toURI)
|
||||
return copy
|
||||
} else {
|
||||
context.changeSerializer.beginRecordChanges(fromResource)
|
||||
return fromResource
|
||||
}
|
||||
override void loadAndWatchResources(ResourceRelocationContext context) {
|
||||
context.changes.filter[ isFile && canHandle ].forEach [ change |
|
||||
val fromResource = context.resourceSet.getResource(change.fromURI, true)
|
||||
if (context.changeType === ResourceRelocationContext.ChangeType.COPY) {
|
||||
val copy = context.resourceSet.getResource(change.fromURI, true)
|
||||
EcoreUtil2.resolveAll(copy)
|
||||
copy.URI = change.toURI
|
||||
context.changeSerializer.beginRecordChanges(copy)
|
||||
} else {
|
||||
context.changeSerializer.beginRecordChanges(fromResource)
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
override void applyChange(ResourceRelocationChange change, Resource resource, ResourceRelocationContext context) {
|
||||
val fromResource = context.resourceSet.getResource(change.fromURI, false)
|
||||
switch change.type {
|
||||
case COPY: {
|
||||
val buffer = new ByteArrayOutputStream
|
||||
fromResource.save(buffer, null)
|
||||
val copy = context.resourceSet.getResource(change.toURI, false)
|
||||
copy.load(new ByteArrayInputStream(buffer.toByteArray), null)
|
||||
context.changeSerializer.beginRecordChanges(copy)
|
||||
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: {}
|
||||
}
|
||||
case MOVE,
|
||||
case RENAME: {
|
||||
context.changeSerializer.beginRecordChanges(fromResource)
|
||||
fromResource.URI = change.toURI
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,10 +20,5 @@ import org.eclipse.xtend.lib.annotations.Data
|
|||
class ResourceRelocationChange {
|
||||
URI fromURI
|
||||
URI toURI
|
||||
Type type
|
||||
boolean isFile
|
||||
|
||||
enum Type {
|
||||
COPY, MOVE, RENAME
|
||||
}
|
||||
}
|
|
@ -20,9 +20,15 @@ import org.eclipse.xtext.ide.serializer.IChangeSerializer
|
|||
@Accessors(PUBLIC_GETTER)
|
||||
class ResourceRelocationContext {
|
||||
|
||||
val ChangeType changeType
|
||||
val List<ResourceRelocationChange> changes
|
||||
val RefactoringIssueAcceptor issueAcceptor
|
||||
|
||||
val IChangeSerializer changeSerializer
|
||||
val ResourceSet resourceSet
|
||||
|
||||
|
||||
enum ChangeType {
|
||||
COPY, MOVE, RENAME
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.refactoring
|
||||
|
||||
import com.google.common.collect.LinkedHashMultimap
|
||||
import java.util.List
|
||||
import org.apache.log4j.Logger
|
||||
|
||||
|
@ -22,46 +21,31 @@ class ResourceRelocationStrategyExecutor {
|
|||
static val LOG = Logger.getLogger(ResourceRelocationStrategyExecutor)
|
||||
|
||||
def executeParticipants(List<? extends IResourceRelocationStrategy> strategies, ResourceRelocationContext context) {
|
||||
val change2strategies = LinkedHashMultimap.create
|
||||
for (change: context.changes) {
|
||||
for (strategy: strategies) {
|
||||
if (strategy.canHandle(change))
|
||||
change2strategies.put(change, strategy)
|
||||
}
|
||||
}
|
||||
val change2resource = newHashMap
|
||||
for (change : change2strategies.keySet) {
|
||||
val primaryStrategy = change2strategies.get(change).head
|
||||
if(context.changeType === ResourceRelocationContext.ChangeType.COPY)
|
||||
context.changeSerializer.updateRelatedFiles = false
|
||||
strategies.forEach [
|
||||
try {
|
||||
val resource = primaryStrategy.loadAndWatchResource(change, context)
|
||||
change2resource.put(change, resource)
|
||||
loadAndWatchResources(context)
|
||||
} catch (Throwable t) {
|
||||
context.issueAcceptor.add(ERROR, 'Error loading resource ' + change?.fromURI?.toString, t)
|
||||
context.issueAcceptor.add(ERROR, 'Error loading resources', t)
|
||||
LOG.error(t)
|
||||
}
|
||||
}
|
||||
for (change : change2strategies.keySet) {
|
||||
var changeApplied = false
|
||||
val resource = change2resource.get(change)
|
||||
for (strategy: change2strategies.get(change)) {
|
||||
if (!changeApplied) {
|
||||
try {
|
||||
strategy.applyChange(change, resource, context)
|
||||
changeApplied = true
|
||||
} catch (Throwable t) {
|
||||
context.issueAcceptor.add(ERROR, 'Error applying relocation change to ' + change?.fromURI?.toString, t)
|
||||
LOG.error(t)
|
||||
}
|
||||
}
|
||||
if (changeApplied) {
|
||||
try {
|
||||
strategy.applySideEffects(change, resource, context)
|
||||
} catch (Throwable t) {
|
||||
context.issueAcceptor.add(ERROR, 'Error applying side effect to ' + change?.fromURI?.toString, 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)
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,13 +8,14 @@
|
|||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.function.Consumer;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.EcoreUtil2;
|
||||
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.
|
||||
|
@ -42,61 +43,59 @@ public interface IResourceRelocationStrategy {
|
|||
@Inject
|
||||
private IResourceServiceProvider resourceServiceProvider;
|
||||
|
||||
@Override
|
||||
public boolean canHandle(final ResourceRelocationChange change) {
|
||||
return this.resourceServiceProvider.canHandle(change.getFromURI());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource loadAndWatchResource(final ResourceRelocationChange change, final ResourceRelocationContext context) {
|
||||
final Resource fromResource = context.getResourceSet().getResource(change.getFromURI(), true);
|
||||
ResourceRelocationChange.Type _type = change.getType();
|
||||
boolean _tripleEquals = (_type == ResourceRelocationChange.Type.COPY);
|
||||
if (_tripleEquals) {
|
||||
final Resource copy = context.getResourceSet().createResource(change.getToURI());
|
||||
return copy;
|
||||
} else {
|
||||
context.getChangeSerializer().beginRecordChanges(fromResource);
|
||||
return fromResource;
|
||||
}
|
||||
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) -> {
|
||||
final Resource fromResource = context.getResourceSet().getResource(change.getFromURI(), true);
|
||||
ResourceRelocationContext.ChangeType _changeType = context.getChangeType();
|
||||
boolean _tripleEquals = (_changeType == ResourceRelocationContext.ChangeType.COPY);
|
||||
if (_tripleEquals) {
|
||||
final Resource copy = context.getResourceSet().getResource(change.getFromURI(), true);
|
||||
EcoreUtil2.resolveAll(copy);
|
||||
copy.setURI(change.getToURI());
|
||||
context.getChangeSerializer().beginRecordChanges(copy);
|
||||
} else {
|
||||
context.getChangeSerializer().beginRecordChanges(fromResource);
|
||||
}
|
||||
};
|
||||
IterableExtensions.<ResourceRelocationChange>filter(context.getChanges(), _function).forEach(_function_1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyChange(final ResourceRelocationChange change, final Resource resource, final ResourceRelocationContext context) {
|
||||
try {
|
||||
final Resource fromResource = context.getResourceSet().getResource(change.getFromURI(), false);
|
||||
ResourceRelocationChange.Type _type = change.getType();
|
||||
if (_type != null) {
|
||||
switch (_type) {
|
||||
case COPY:
|
||||
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
fromResource.save(buffer, null);
|
||||
final Resource copy = context.getResourceSet().getResource(change.getToURI(), false);
|
||||
byte[] _byteArray = buffer.toByteArray();
|
||||
ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
|
||||
copy.load(_byteArrayInputStream, null);
|
||||
context.getChangeSerializer().beginRecordChanges(copy);
|
||||
break;
|
||||
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:
|
||||
context.getChangeSerializer().beginRecordChanges(fromResource);
|
||||
final Resource fromResource = context.getResourceSet().getResource(change.getFromURI(), false);
|
||||
fromResource.setURI(change.getToURI());
|
||||
break;
|
||||
case COPY:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
};
|
||||
IterableExtensions.<ResourceRelocationChange>filter(context.getChanges(), _function).forEach(_function_1);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract boolean canHandle(final ResourceRelocationChange change);
|
||||
public abstract void loadAndWatchResources(final ResourceRelocationContext context);
|
||||
|
||||
public abstract Resource loadAndWatchResource(final ResourceRelocationChange change, final ResourceRelocationContext context);
|
||||
public abstract void applyChange(final ResourceRelocationContext context);
|
||||
|
||||
public abstract void applyChange(final ResourceRelocationChange change, final Resource resource, final ResourceRelocationContext context);
|
||||
|
||||
public abstract void applySideEffects(final ResourceRelocationChange change, final Resource resource, final ResourceRelocationContext context);
|
||||
public abstract void applySideEffects(final ResourceRelocationContext context);
|
||||
}
|
||||
|
|
|
@ -21,27 +21,16 @@ import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
|
|||
@Data
|
||||
@SuppressWarnings("all")
|
||||
public class ResourceRelocationChange {
|
||||
public enum Type {
|
||||
COPY,
|
||||
|
||||
MOVE,
|
||||
|
||||
RENAME;
|
||||
}
|
||||
|
||||
private final URI fromURI;
|
||||
|
||||
private final URI toURI;
|
||||
|
||||
private final ResourceRelocationChange.Type type;
|
||||
|
||||
private final boolean isFile;
|
||||
|
||||
public ResourceRelocationChange(final URI fromURI, final URI toURI, final ResourceRelocationChange.Type type, final boolean isFile) {
|
||||
public ResourceRelocationChange(final URI fromURI, final URI toURI, final boolean isFile) {
|
||||
super();
|
||||
this.fromURI = fromURI;
|
||||
this.toURI = toURI;
|
||||
this.type = type;
|
||||
this.isFile = isFile;
|
||||
}
|
||||
|
||||
|
@ -52,7 +41,6 @@ public class ResourceRelocationChange {
|
|||
int result = 1;
|
||||
result = prime * result + ((this.fromURI== null) ? 0 : this.fromURI.hashCode());
|
||||
result = prime * result + ((this.toURI== null) ? 0 : this.toURI.hashCode());
|
||||
result = prime * result + ((this.type== null) ? 0 : this.type.hashCode());
|
||||
result = prime * result + (this.isFile ? 1231 : 1237);
|
||||
return result;
|
||||
}
|
||||
|
@ -77,11 +65,6 @@ public class ResourceRelocationChange {
|
|||
return false;
|
||||
} else if (!this.toURI.equals(other.toURI))
|
||||
return false;
|
||||
if (this.type == null) {
|
||||
if (other.type != null)
|
||||
return false;
|
||||
} else if (!this.type.equals(other.type))
|
||||
return false;
|
||||
if (other.isFile != this.isFile)
|
||||
return false;
|
||||
return true;
|
||||
|
@ -93,7 +76,6 @@ public class ResourceRelocationChange {
|
|||
ToStringBuilder b = new ToStringBuilder(this);
|
||||
b.add("fromURI", this.fromURI);
|
||||
b.add("toURI", this.toURI);
|
||||
b.add("type", this.type);
|
||||
b.add("isFile", this.isFile);
|
||||
return b.toString();
|
||||
}
|
||||
|
@ -108,11 +90,6 @@ public class ResourceRelocationChange {
|
|||
return this.toURI;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public ResourceRelocationChange.Type getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public boolean isFile() {
|
||||
return this.isFile;
|
||||
|
|
|
@ -24,6 +24,16 @@ import org.eclipse.xtext.xbase.lib.Pure;
|
|||
@Accessors(AccessorType.PUBLIC_GETTER)
|
||||
@SuppressWarnings("all")
|
||||
public class ResourceRelocationContext {
|
||||
public enum ChangeType {
|
||||
COPY,
|
||||
|
||||
MOVE,
|
||||
|
||||
RENAME;
|
||||
}
|
||||
|
||||
private final ResourceRelocationContext.ChangeType changeType;
|
||||
|
||||
private final List<ResourceRelocationChange> changes;
|
||||
|
||||
private final RefactoringIssueAcceptor issueAcceptor;
|
||||
|
@ -32,14 +42,20 @@ public class ResourceRelocationContext {
|
|||
|
||||
private final ResourceSet resourceSet;
|
||||
|
||||
public ResourceRelocationContext(final List<ResourceRelocationChange> changes, final RefactoringIssueAcceptor issueAcceptor, final IChangeSerializer changeSerializer, final ResourceSet resourceSet) {
|
||||
public ResourceRelocationContext(final ResourceRelocationContext.ChangeType changeType, final List<ResourceRelocationChange> changes, final RefactoringIssueAcceptor issueAcceptor, final IChangeSerializer changeSerializer, final ResourceSet resourceSet) {
|
||||
super();
|
||||
this.changeType = changeType;
|
||||
this.changes = changes;
|
||||
this.issueAcceptor = issueAcceptor;
|
||||
this.changeSerializer = changeSerializer;
|
||||
this.resourceSet = resourceSet;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public ResourceRelocationContext.ChangeType getChangeType() {
|
||||
return this.changeType;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public List<ResourceRelocationChange> getChanges() {
|
||||
return this.changes;
|
||||
|
|
|
@ -7,20 +7,14 @@
|
|||
*/
|
||||
package org.eclipse.xtext.ide.refactoring;
|
||||
|
||||
import com.google.common.collect.LinkedHashMultimap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.ide.refactoring.IResourceRelocationStrategy;
|
||||
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
|
||||
import org.eclipse.xtext.ide.refactoring.ResourceRelocationChange;
|
||||
import org.eclipse.xtext.ide.refactoring.ResourceRelocationContext;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
|
||||
/**
|
||||
* @author koehnlein - Initial contribution and API
|
||||
|
@ -31,100 +25,53 @@ public class ResourceRelocationStrategyExecutor {
|
|||
private final static Logger LOG = Logger.getLogger(ResourceRelocationStrategyExecutor.class);
|
||||
|
||||
public void executeParticipants(final List<? extends IResourceRelocationStrategy> strategies, final ResourceRelocationContext context) {
|
||||
final LinkedHashMultimap<ResourceRelocationChange, IResourceRelocationStrategy> change2strategies = LinkedHashMultimap.<ResourceRelocationChange, IResourceRelocationStrategy>create();
|
||||
List<ResourceRelocationChange> _changes = context.getChanges();
|
||||
for (final ResourceRelocationChange change : _changes) {
|
||||
for (final IResourceRelocationStrategy strategy : strategies) {
|
||||
boolean _canHandle = strategy.canHandle(change);
|
||||
if (_canHandle) {
|
||||
change2strategies.put(change, strategy);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
final HashMap<ResourceRelocationChange, Resource> change2resource = CollectionLiterals.<ResourceRelocationChange, Resource>newHashMap();
|
||||
Set<ResourceRelocationChange> _keySet = change2strategies.keySet();
|
||||
for (final ResourceRelocationChange change_1 : _keySet) {
|
||||
{
|
||||
final IResourceRelocationStrategy primaryStrategy = IterableExtensions.<IResourceRelocationStrategy>head(change2strategies.get(change_1));
|
||||
try {
|
||||
final Resource resource = primaryStrategy.loadAndWatchResource(change_1, context);
|
||||
change2resource.put(change_1, resource);
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof Throwable) {
|
||||
final Throwable t = (Throwable)_t;
|
||||
URI _fromURI = null;
|
||||
if (change_1!=null) {
|
||||
_fromURI=change_1.getFromURI();
|
||||
}
|
||||
String _string = null;
|
||||
if (_fromURI!=null) {
|
||||
_string=_fromURI.toString();
|
||||
}
|
||||
String _plus = ("Error loading resource " + _string);
|
||||
context.getIssueAcceptor().add(RefactoringIssueAcceptor.Severity.ERROR, _plus, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Set<ResourceRelocationChange> _keySet_1 = change2strategies.keySet();
|
||||
for (final ResourceRelocationChange change_2 : _keySet_1) {
|
||||
{
|
||||
boolean changeApplied = false;
|
||||
final Resource resource = change2resource.get(change_2);
|
||||
Set<IResourceRelocationStrategy> _get = change2strategies.get(change_2);
|
||||
for (final IResourceRelocationStrategy strategy_1 : _get) {
|
||||
{
|
||||
if ((!changeApplied)) {
|
||||
try {
|
||||
strategy_1.applyChange(change_2, resource, context);
|
||||
changeApplied = true;
|
||||
} catch (final Throwable _t) {
|
||||
if (_t instanceof Throwable) {
|
||||
final Throwable t = (Throwable)_t;
|
||||
URI _fromURI = null;
|
||||
if (change_2!=null) {
|
||||
_fromURI=change_2.getFromURI();
|
||||
}
|
||||
String _string = null;
|
||||
if (_fromURI!=null) {
|
||||
_string=_fromURI.toString();
|
||||
}
|
||||
String _plus = ("Error applying relocation change to " + _string);
|
||||
context.getIssueAcceptor().add(RefactoringIssueAcceptor.Severity.ERROR, _plus, t);
|
||||
ResourceRelocationStrategyExecutor.LOG.error(t);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changeApplied) {
|
||||
try {
|
||||
strategy_1.applySideEffects(change_2, resource, context);
|
||||
} catch (final Throwable _t_1) {
|
||||
if (_t_1 instanceof Throwable) {
|
||||
final Throwable t_1 = (Throwable)_t_1;
|
||||
URI _fromURI_1 = null;
|
||||
if (change_2!=null) {
|
||||
_fromURI_1=change_2.getFromURI();
|
||||
}
|
||||
String _string_1 = null;
|
||||
if (_fromURI_1!=null) {
|
||||
_string_1=_fromURI_1.toString();
|
||||
}
|
||||
String _plus_1 = ("Error applying side effect to " + _string_1);
|
||||
context.getIssueAcceptor().add(RefactoringIssueAcceptor.Severity.ERROR, _plus_1, t_1);
|
||||
ResourceRelocationStrategyExecutor.LOG.error(t_1);
|
||||
} else {
|
||||
throw Exceptions.sneakyThrow(_t_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
package org.eclipse.xtext.testlanguages.fileAware.ide.refactoring
|
||||
|
||||
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.testlanguages.fileAware.fileAware.PackageDeclaration
|
||||
import org.eclipse.emf.ecore.resource.Resource
|
||||
|
||||
class FileAwareTestLanguageResourceRelocationStrategy extends IResourceRelocationStrategy.AbstractImpl {
|
||||
|
||||
override applySideEffects(ResourceRelocationChange change, Resource resource, ResourceRelocationContext context) {
|
||||
val rootElement = resource.contents.head
|
||||
if (rootElement instanceof PackageDeclaration) {
|
||||
val newPackage = change.toURI.trimSegments(1).segmentsList.drop(2).join('.')
|
||||
rootElement.name = newPackage
|
||||
}
|
||||
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
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,30 @@
|
|||
package org.eclipse.xtext.testlanguages.fileAware.ide.refactoring;
|
||||
|
||||
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.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 {
|
||||
@Override
|
||||
public void applySideEffects(final ResourceRelocationChange change, final Resource resource, final ResourceRelocationContext context) {
|
||||
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);
|
||||
}
|
||||
public void applySideEffects(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) -> {
|
||||
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);
|
||||
}
|
||||
};
|
||||
IterableExtensions.<ResourceRelocationChange>filter(context.getChanges(), _function).forEach(_function_1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue