[refactoring] change serializer based resource move refactorings

This commit is contained in:
Jan Koehnlein 2017-08-17 18:43:27 +02:00 committed by Moritz Eysholdt
parent 4ec4a1bdb4
commit 007cb435fc
9 changed files with 269 additions and 220 deletions

View file

@ -0,0 +1,86 @@
/*******************************************************************************
* 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 com.google.inject.Inject
import java.util.List
import org.eclipse.emf.common.util.URI
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.Data
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.ide.serializer.IChangeSerializer
import org.eclipse.xtext.resource.IResourceServiceProvider
import java.util.Map
import static org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor.Severity.*
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@FinalFieldsConstructor
class MoveResourceContext {
@Accessors(PUBLIC_GETTER) val List<ResourceURIChange> fileChanges
@Accessors(PUBLIC_GETTER) val List<ResourceURIChange> folderChanges
@Accessors(PUBLIC_GETTER) val RefactoringIssueAcceptor issueAcceptor
val IChangeSerializer changeSerializer
val ResourceSet resourceSet
val IResourceServiceProvider.Registry resourceServiceProviderRegistry
val Map<Resource, ResourceModification> modifications = newHashMap
def addModification(URI uri, ResourceModification modification) {
try {
val resource = resourceSet.getResource(uri, true)
changeSerializer.beginRecordChanges(resource)
modifications.put(resource, modification)
} catch (Throwable t) {
issueAcceptor.add(ERROR, 'Error loading resource ' + uri?.toString, t)
}
}
def executeModifications() {
modifications.entrySet.forEach [
try {
value.modify(key)
} catch (Throwable t) {
issueAcceptor.add(ERROR, 'Error executing modification on resource ' + key?.URI?.toString, t)
}
]
}
def isXtextResource(URI uri) {
resourceServiceProviderRegistry.getResourceServiceProvider(uri) !== null
}
static class Factory {
@Inject IResourceServiceProvider.Registry resourceServiceProviderRegistry
def MoveResourceContext create(List<ResourceURIChange> fileChanges, List<ResourceURIChange> folderChanges,
RefactoringIssueAcceptor issues, IChangeSerializer changeSerializer, ResourceSet resourceSet) {
new MoveResourceContext(fileChanges, folderChanges, issues, changeSerializer, resourceSet,
resourceServiceProviderRegistry)
}
}
}
/**
* URIs can also refer to folders and non-Xtext resources.
*
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
class ResourceURIChange {
URI oldURI
URI newURI
}

View file

@ -1,50 +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.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtend.lib.annotations.Data
/**
* The arguments passed to a {@link XtextMoveResourceStrategy}.
*
* In the resourceSet the refactoring is already applied, i.e. all
* moved resources already have the new URI.
*
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
class XtextMoveArguments {
ResourceSet resourceSet
List<ResourceURIChange> changes
}
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
class XtextMoveFolderArguments extends XtextMoveArguments {
List<ResourceURIChange> folderChanges
}
/**
* URIs can also refer to folders and non-Xtext resources.
*
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
class ResourceURIChange {
URI oldURI
URI newURI
}

View file

@ -0,0 +1,18 @@
/*******************************************************************************
* 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.ecore.resource.Resource
/**
* @author koehnlein - Initial contribution and API
*/
interface ResourceModification {
def void modify(Resource resource)
}

View file

@ -13,7 +13,5 @@ package org.eclipse.xtext.ide.refactoring
*/
interface XtextMoveResourceStrategy {
def void applyMove(XtextMoveArguments arguments, RefactoringIssueAcceptor issues)
def void applyMove(MoveResourceContext context)
}

View file

@ -0,0 +1,144 @@
/**
* 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 com.google.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.eclipse.emf.common.util.URI;
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;
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
import org.eclipse.xtext.ide.refactoring.ResourceModification;
import org.eclipse.xtext.ide.refactoring.ResourceURIChange;
import org.eclipse.xtext.ide.serializer.IChangeSerializer;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@FinalFieldsConstructor
@SuppressWarnings("all")
public class MoveResourceContext {
public static class Factory {
@Inject
private IResourceServiceProvider.Registry resourceServiceProviderRegistry;
public MoveResourceContext create(final List<ResourceURIChange> fileChanges, final List<ResourceURIChange> folderChanges, final RefactoringIssueAcceptor issues, final IChangeSerializer changeSerializer, final ResourceSet resourceSet) {
return new MoveResourceContext(fileChanges, folderChanges, issues, changeSerializer, resourceSet,
this.resourceServiceProviderRegistry);
}
}
@Accessors(AccessorType.PUBLIC_GETTER)
private final List<ResourceURIChange> fileChanges;
@Accessors(AccessorType.PUBLIC_GETTER)
private final List<ResourceURIChange> folderChanges;
@Accessors(AccessorType.PUBLIC_GETTER)
private final RefactoringIssueAcceptor issueAcceptor;
private final IChangeSerializer changeSerializer;
private final ResourceSet resourceSet;
private final IResourceServiceProvider.Registry resourceServiceProviderRegistry;
private final Map<Resource, ResourceModification> modifications = CollectionLiterals.<Resource, ResourceModification>newHashMap();
public ResourceModification addModification(final URI uri, final ResourceModification modification) {
ResourceModification _xtrycatchfinallyexpression = null;
try {
ResourceModification _xblockexpression = null;
{
final Resource resource = this.resourceSet.getResource(uri, true);
this.changeSerializer.beginRecordChanges(resource);
_xblockexpression = this.modifications.put(resource, modification);
}
_xtrycatchfinallyexpression = _xblockexpression;
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable t = (Throwable)_t;
String _string = null;
if (uri!=null) {
_string=uri.toString();
}
String _plus = ("Error loading resource " + _string);
this.issueAcceptor.add(RefactoringIssueAcceptor.Severity.ERROR, _plus, t);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return _xtrycatchfinallyexpression;
}
public void executeModifications() {
final Consumer<Map.Entry<Resource, ResourceModification>> _function = (Map.Entry<Resource, ResourceModification> it) -> {
try {
it.getValue().modify(it.getKey());
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable t = (Throwable)_t;
Resource _key = it.getKey();
URI _uRI = null;
if (_key!=null) {
_uRI=_key.getURI();
}
String _string = null;
if (_uRI!=null) {
_string=_uRI.toString();
}
String _plus = ("Error executing modification on resource " + _string);
this.issueAcceptor.add(RefactoringIssueAcceptor.Severity.ERROR, _plus, t);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
};
this.modifications.entrySet().forEach(_function);
}
public boolean isXtextResource(final URI uri) {
IResourceServiceProvider _resourceServiceProvider = this.resourceServiceProviderRegistry.getResourceServiceProvider(uri);
return (_resourceServiceProvider != null);
}
public MoveResourceContext(final List<ResourceURIChange> fileChanges, final List<ResourceURIChange> folderChanges, final RefactoringIssueAcceptor issueAcceptor, final IChangeSerializer changeSerializer, final ResourceSet resourceSet, final IResourceServiceProvider.Registry resourceServiceProviderRegistry) {
super();
this.fileChanges = fileChanges;
this.folderChanges = folderChanges;
this.issueAcceptor = issueAcceptor;
this.changeSerializer = changeSerializer;
this.resourceSet = resourceSet;
this.resourceServiceProviderRegistry = resourceServiceProviderRegistry;
}
@Pure
public List<ResourceURIChange> getFileChanges() {
return this.fileChanges;
}
@Pure
public List<ResourceURIChange> getFolderChanges() {
return this.folderChanges;
}
@Pure
public RefactoringIssueAcceptor getIssueAcceptor() {
return this.issueAcceptor;
}
}

View file

@ -0,0 +1,18 @@
/**
* 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.ecore.resource.Resource;
/**
* @author koehnlein - Initial contribution and API
*/
@SuppressWarnings("all")
public interface ResourceModification {
public abstract void modify(final Resource resource);
}

View file

@ -1,90 +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.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend.lib.annotations.Data;
import org.eclipse.xtext.ide.refactoring.ResourceURIChange;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
/**
* The arguments passed to a {@link XtextMoveResourceStrategy}.
*
* In the resourceSet the refactoring is already applied, i.e. all
* moved resources already have the new URI.
*
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
@SuppressWarnings("all")
public class XtextMoveArguments {
private final ResourceSet resourceSet;
private final List<ResourceURIChange> changes;
public XtextMoveArguments(final ResourceSet resourceSet, final List<ResourceURIChange> changes) {
super();
this.resourceSet = resourceSet;
this.changes = changes;
}
@Override
@Pure
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.resourceSet== null) ? 0 : this.resourceSet.hashCode());
result = prime * result + ((this.changes== null) ? 0 : this.changes.hashCode());
return result;
}
@Override
@Pure
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
XtextMoveArguments other = (XtextMoveArguments) obj;
if (this.resourceSet == null) {
if (other.resourceSet != null)
return false;
} else if (!this.resourceSet.equals(other.resourceSet))
return false;
if (this.changes == null) {
if (other.changes != null)
return false;
} else if (!this.changes.equals(other.changes))
return false;
return true;
}
@Override
@Pure
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("resourceSet", this.resourceSet);
b.add("changes", this.changes);
return b.toString();
}
@Pure
public ResourceSet getResourceSet() {
return this.resourceSet;
}
@Pure
public List<ResourceURIChange> getChanges() {
return this.changes;
}
}

View file

@ -1,74 +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.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend.lib.annotations.Data;
import org.eclipse.xtext.ide.refactoring.ResourceURIChange;
import org.eclipse.xtext.ide.refactoring.XtextMoveArguments;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
/**
* @author koehnlein - Initial contribution and API
* @since 2.13
*/
@Data
@SuppressWarnings("all")
public class XtextMoveFolderArguments extends XtextMoveArguments {
private final List<ResourceURIChange> folderChanges;
public XtextMoveFolderArguments(final ResourceSet resourceSet, final List<ResourceURIChange> changes, final List<ResourceURIChange> folderChanges) {
super(resourceSet, changes);
this.folderChanges = folderChanges;
}
@Override
@Pure
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((this.folderChanges== null) ? 0 : this.folderChanges.hashCode());
return result;
}
@Override
@Pure
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
if (!super.equals(obj))
return false;
XtextMoveFolderArguments other = (XtextMoveFolderArguments) obj;
if (this.folderChanges == null) {
if (other.folderChanges != null)
return false;
} else if (!this.folderChanges.equals(other.folderChanges))
return false;
return true;
}
@Override
@Pure
public String toString() {
String result = new ToStringBuilder(this)
.addAllFields()
.toString();
return result;
}
@Pure
public List<ResourceURIChange> getFolderChanges() {
return this.folderChanges;
}
}

View file

@ -7,8 +7,7 @@
*/
package org.eclipse.xtext.ide.refactoring;
import org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor;
import org.eclipse.xtext.ide.refactoring.XtextMoveArguments;
import org.eclipse.xtext.ide.refactoring.MoveResourceContext;
/**
* @author koehnlein - Initial contribution and API
@ -16,5 +15,5 @@ import org.eclipse.xtext.ide.refactoring.XtextMoveArguments;
*/
@SuppressWarnings("all")
public interface XtextMoveResourceStrategy {
public abstract void applyMove(final XtextMoveArguments arguments, final RefactoringIssueAcceptor issues);
public abstract void applyMove(final MoveResourceContext context);
}