Search in sources :

Example 1 with ChangeConverter

use of org.eclipse.xtext.ui.refactoring2.ChangeConverter in project xtext-eclipse by eclipse.

the class ResourceRelocationProcessor method createChange.

public Change createChange(String name, ResourceRelocationContext.ChangeType type, IProgressMonitor pm) throws CoreException, OperationCanceledException {
    if (uriChanges.isEmpty()) {
        return null;
    }
    SubMonitor subMonitor = SubMonitor.convert(pm);
    // Declaring the task and its effort in 'SubMonitor.convert(...)' doesn't yield the expected UI updates
    // so let's do it separately; the total effort of '5' is chosen for weighting the subsequent efforts
    subMonitor.beginTask("Preparing the refactoring...", 5);
    IChangeSerializer changeSerializer = changeSerializerProvider.get();
    ResourceSet resourceSet = resourceSetProvider.get(project);
    ResourceRelocationContext context = new ResourceRelocationContext(type, uriChanges, issues, changeSerializer, resourceSet);
    boolean persistedIndexUsageRequested = isPersistedIndexUsageRequested(context);
    // TODO check preconditions like all editors being saved if 'persistedIndexUsageRequested' == true
    initializeResourceSet(persistedIndexUsageRequested, context);
    executeParticipants(context, subMonitor.split(1));
    ChangeConverter changeConverter = // 
    changeConverterFactory.create(// 
    name, (it) -> {
        return (!(it instanceof MoveResourceChange || it instanceof RenameResourceChange) || !excludedResources.contains(it.getModifiedElement()));
    }, issues);
    // remaining effort is assigned to 'changeSerializer's work
    SubMonitor modificationApplicationMonitor = subMonitor.split(4);
    changeSerializer.setProgressMonitor(modificationApplicationMonitor);
    changeSerializer.applyModifications(changeConverter);
    modificationApplicationMonitor.done();
    return changeConverter.getChange();
}
Also used : IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) ResourceRelocationContext(org.eclipse.xtext.ide.refactoring.ResourceRelocationContext) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) MoveResourceChange(org.eclipse.ltk.core.refactoring.resource.MoveResourceChange) SubMonitor(org.eclipse.core.runtime.SubMonitor) ChangeConverter(org.eclipse.xtext.ui.refactoring2.ChangeConverter) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 2 with ChangeConverter

use of org.eclipse.xtext.ui.refactoring2.ChangeConverter in project xtext-eclipse by eclipse.

the class RenameElementProcessor2 method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException, OperationCanceledException {
    RenameChange renameChange = new RenameChange(newName, renameElementContext.getTargetElementURI());
    RenameContext renameContext = new RenameContext(Arrays.asList(renameChange), resourceSet, changeSerializer, status);
    renameStrategy.applyRename(renameContext);
    String name = "Rename " + originalName + " to " + newName;
    ChangeConverter changeConverter = changeConverterFactory.create(name, null, status);
    changeSerializer.applyModifications(changeConverter);
    change = changeConverter.getChange();
    return status.getRefactoringStatus();
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) ChangeConverter(org.eclipse.xtext.ui.refactoring2.ChangeConverter)

Example 3 with ChangeConverter

use of org.eclipse.xtext.ui.refactoring2.ChangeConverter in project xtext-eclipse by eclipse.

the class BatchModification method applyInWorkspace.

protected void applyInWorkspace(Iterable<IBatchableModification> modifications, IProgressMonitor monitor) throws CoreException {
    IProject proj = getResolvedProject();
    if (proj == null) {
        LOG.error("No project configured.");
        return;
    }
    SubMonitor progress = SubMonitor.convert(monitor, 100);
    ResourceSet resourceSet = resourceSetProvider.get(proj);
    liveScopeResourceSetInitializer.initialize(resourceSet);
    List<ResolvedModification> resolved = Lists.newArrayList();
    for (IBatchableModification modification : modifications) {
        EObject obj = resourceSet.getEObject(modification.getEObjectURI(), true);
        if (obj == null || obj.eIsProxy()) {
            LOG.error("Invalid EObject URI " + modification.getEObjectURI());
            continue;
        }
        resolved.add(new ResolvedModification(obj, modification));
    }
    progress.split(5);
    IChangeSerializer serializer = serializerProvider.get();
    SubMonitor subProgress = SubMonitor.convert(progress.split(80), resolved.size());
    for (ResolvedModification mod : resolved) {
        serializer.setProgressMonitor(subProgress.split(1));
        mod.modification.apply(mod.object, serializer);
        subProgress.split(1);
    }
    boolean first = true;
    for (ResolvedModification mod : resolved) {
        if (first) {
            serializer.setUpdateCrossReferences(mod.modification.isUpdateCrossReferences());
            serializer.setUpdateRelatedFiles(mod.modification.isUpdateRelatedFiles());
        } else {
            if (serializer.isUpdateCrossReferences() != mod.modification.isUpdateCrossReferences()) {
                LOG.error("two modifications can't be batched.");
                return;
            }
            if (serializer.isUpdateRelatedFiles() != mod.modification.isUpdateRelatedFiles()) {
                LOG.error("two modifications can't be batched.");
                return;
            }
        }
    }
    // 15 ticks yet available
    progress.split(1);
    ChangeConverter converter = changeConverterFactory.create("Resolving Issues", null, issueAcceptor);
    serializer.applyModifications(converter);
    progress.split(1);
    Change change = converter.getChange();
    if (change != null) {
        change.initializeValidationData(progress.split(3));
        new PerformChangeOperation(change).run(progress.split(10));
    }
    progress.done();
}
Also used : PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) EObject(org.eclipse.emf.ecore.EObject) SubMonitor(org.eclipse.core.runtime.SubMonitor) ChangeConverter(org.eclipse.xtext.ui.refactoring2.ChangeConverter) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Change(org.eclipse.ltk.core.refactoring.Change) IProject(org.eclipse.core.resources.IProject)

Aggregations

ChangeConverter (org.eclipse.xtext.ui.refactoring2.ChangeConverter)3 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)2 IProject (org.eclipse.core.resources.IProject)1 EObject (org.eclipse.emf.ecore.EObject)1 Change (org.eclipse.ltk.core.refactoring.Change)1 PerformChangeOperation (org.eclipse.ltk.core.refactoring.PerformChangeOperation)1 MoveResourceChange (org.eclipse.ltk.core.refactoring.resource.MoveResourceChange)1 RenameResourceChange (org.eclipse.ltk.core.refactoring.resource.RenameResourceChange)1 RenameChange (org.eclipse.xtext.ide.refactoring.RenameChange)1 RenameContext (org.eclipse.xtext.ide.refactoring.RenameContext)1 ResourceRelocationContext (org.eclipse.xtext.ide.refactoring.ResourceRelocationContext)1