Search in sources :

Example 1 with ResourceRelocationContext

use of org.eclipse.xtext.ide.refactoring.ResourceRelocationContext in project xtext-core by eclipse.

the class FileAwareTestLanguageResourceRelocationStrategy method applyChange.

@Override
public void applyChange(ResourceRelocationContext context) {
    filter(context.getChanges(), c -> canHandle(c)).forEach(change -> {
        context.addModification(change, resource -> {
            EObject rootElement = head(resource.getContents());
            if (rootElement instanceof PackageDeclaration) {
                List<String> segments = change.getToURI().trimSegments(1).segmentsList();
                String newPackage = Joiner.on(".").join(drop(segments, 2));
                ((PackageDeclaration) rootElement).setName(newPackage);
            }
        });
    });
}
Also used : PackageDeclaration(org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration) List(java.util.List) ResourceRelocationChange(org.eclipse.xtext.ide.refactoring.ResourceRelocationChange) IterableExtensions.head(org.eclipse.xtext.xbase.lib.IterableExtensions.head) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) Inject(com.google.inject.Inject) IterableExtensions.drop(org.eclipse.xtext.xbase.lib.IterableExtensions.drop) EObject(org.eclipse.emf.ecore.EObject) IterableExtensions.filter(org.eclipse.xtext.xbase.lib.IterableExtensions.filter) IResourceRelocationStrategy(org.eclipse.xtext.ide.refactoring.IResourceRelocationStrategy) ResourceRelocationContext(org.eclipse.xtext.ide.refactoring.ResourceRelocationContext) Joiner(com.google.common.base.Joiner) EObject(org.eclipse.emf.ecore.EObject) PackageDeclaration(org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration)

Example 2 with ResourceRelocationContext

use of org.eclipse.xtext.ide.refactoring.ResourceRelocationContext in project xtext-eclipse by eclipse.

the class ResourceRelocationProcessor method executeParticipants.

protected void executeParticipants(final ResourceRelocationContext context) {
    final List<? extends IResourceRelocationStrategy> strategies = this.strategyRegistry.getStrategies();
    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.applyChange(context);
        } catch (final Throwable _t) {
            if (_t instanceof Throwable) {
                final Throwable t = (Throwable) _t;
                this.issues.add(RefactoringIssueAcceptor.Severity.ERROR, "Error applying resource changes", t);
                ResourceRelocationProcessor.LOG.error(t.getMessage(), t);
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    };
    strategies.forEach(_function);
}
Also used : IResourceRelocationStrategy(org.eclipse.xtext.ide.refactoring.IResourceRelocationStrategy) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) ResourceRelocationContext(org.eclipse.xtext.ide.refactoring.ResourceRelocationContext)

Example 3 with ResourceRelocationContext

use of org.eclipse.xtext.ide.refactoring.ResourceRelocationContext 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)

Aggregations

ResourceRelocationContext (org.eclipse.xtext.ide.refactoring.ResourceRelocationContext)3 IResourceRelocationStrategy (org.eclipse.xtext.ide.refactoring.IResourceRelocationStrategy)2 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)2 Joiner (com.google.common.base.Joiner)1 Inject (com.google.inject.Inject)1 List (java.util.List)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 EObject (org.eclipse.emf.ecore.EObject)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 MoveResourceChange (org.eclipse.ltk.core.refactoring.resource.MoveResourceChange)1 RenameResourceChange (org.eclipse.ltk.core.refactoring.resource.RenameResourceChange)1 ResourceRelocationChange (org.eclipse.xtext.ide.refactoring.ResourceRelocationChange)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1 PackageDeclaration (org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration)1 ChangeConverter (org.eclipse.xtext.ui.refactoring2.ChangeConverter)1 IterableExtensions.drop (org.eclipse.xtext.xbase.lib.IterableExtensions.drop)1 IterableExtensions.filter (org.eclipse.xtext.xbase.lib.IterableExtensions.filter)1 IterableExtensions.head (org.eclipse.xtext.xbase.lib.IterableExtensions.head)1