Search in sources :

Example 21 with IChangeSerializer

use of org.eclipse.xtext.ide.serializer.IChangeSerializer in project xtext-core by eclipse.

the class ChangeSerializerTest method testResourceURIChange.

@Test
public void testResourceURIChange() {
    InMemoryURIHandler fs = new InMemoryURIHandler();
    changeSerializerTestHelper.operator_add(fs, Pair.of("inmemory:/f.pstl", "#1 root { }"));
    ResourceSet rs = changeSerializerTestHelper.createResourceSet(fs);
    Node model = changeSerializerTestHelper.findFirstOfTypeInFile(rs, "inmemory:/f.pstl", Node.class);
    IChangeSerializer serializer = changeSerializerTestHelper.newChangeSerializer();
    serializer.addModification(model.eResource(), (Resource it) -> {
        model.eResource().setURI(URI.createURI("inmemory:/x.pstl"));
    });
    Collection<IEmfResourceChange> changes = changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
    String expectation = "----- renamed inmemory:/f.pstl to inmemory:/x.pstl (syntax: <offset|text>) -----\n" + "(no changes)\n" + "--------------------------------------------------------------------------------\n";
    changeSerializerTestHelper.operator_tripleEquals(changes, expectation);
}
Also used : IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) InMemoryURIHandler(org.eclipse.xtext.testing.util.InMemoryURIHandler) Node(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IEmfResourceChange(org.eclipse.xtext.ide.serializer.IEmfResourceChange) Test(org.junit.Test)

Example 22 with IChangeSerializer

use of org.eclipse.xtext.ide.serializer.IChangeSerializer in project xtext-core by eclipse.

the class ChangeSerializerTest method testRenameLocal.

@Test
public void testRenameLocal() {
    InMemoryURIHandler fs = new InMemoryURIHandler();
    changeSerializerTestHelper.operator_add(fs, Pair.of("inmemory:/file1.pstl", "#1 root { foo1; foo2 { ref foo1 } }"));
    ResourceSet rs = changeSerializerTestHelper.createResourceSet(fs);
    Node model = changeSerializerTestHelper.findFirstOfTypeInFile(rs, "inmemory:/file1.pstl", Node.class);
    IChangeSerializer serializer = changeSerializerTestHelper.newChangeSerializer();
    serializer.addModification(model.eResource(), (Resource it) -> {
        model.getChildren().get(0).setName("bazz4");
    });
    Collection<IEmfResourceChange> changes = changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer);
    String expectation = "----------------- inmemory:/file1.pstl (syntax: <offset|text>) -----------------\n" + "#1 root { <10:4|bazz4>; foo2 { ref <27:4|bazz4> } }\n" + "--------------------------------------------------------------------------------\n" + "10 4 \"foo1\" -> \"bazz4\"\n" + "27 4 \"foo1\" -> \"bazz4\"\n";
    changeSerializerTestHelper.operator_tripleEquals(changes, expectation);
}
Also used : IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) InMemoryURIHandler(org.eclipse.xtext.testing.util.InMemoryURIHandler) Node(org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IEmfResourceChange(org.eclipse.xtext.ide.serializer.IEmfResourceChange) Test(org.junit.Test)

Example 23 with IChangeSerializer

use of org.eclipse.xtext.ide.serializer.IChangeSerializer in project xtext-core by eclipse.

the class RenameService2 method applyModifications.

/**
 * <p>
 * Performs the actual renaming. Runs within a read transaction on the index and a live-scoped resource set.
 * </p>
 * <p>
 * Override this method to implement custom renaming behavior.
 * </p>
 *
 * @since 2.25
 */
protected void applyModifications(EObject element, WorkspaceEdit workspaceEdit, ServerRefactoringIssueAcceptor issueAcceptor, IRenameService2.Options options, ILanguageServerAccess.Context context) {
    IResourceServiceProvider services = serviceProviderRegistry.getResourceServiceProvider(element.eResource().getURI());
    IChangeSerializer changeSerializer = services.get(IChangeSerializer.class);
    RenameChange change = new RenameChange(options.getRenameParams().getNewName(), EcoreUtil.getURI(element));
    ResourceSet resourceSet = element.eResource().getResourceSet();
    RenameContext renameContext = new RenameContext(Lists.newArrayList(change), resourceSet, changeSerializer, issueAcceptor);
    IRenameStrategy2 renameStrategy = services.<IRenameStrategy2>get(IRenameStrategy2.class);
    renameStrategy.applyRename(renameContext);
    ChangeConverter2.Factory converterFactory = services.<ChangeConverter2.Factory>get(ChangeConverter2.Factory.class);
    ChangeConverter2 changeConverter = converterFactory.create(workspaceEdit, options.getLanguageServerAccess());
    changeSerializer.applyModifications(changeConverter);
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) IRenameStrategy2(org.eclipse.xtext.ide.refactoring.IRenameStrategy2) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 24 with IChangeSerializer

use of org.eclipse.xtext.ide.serializer.IChangeSerializer in project xtext-eclipse by eclipse.

the class DefaultLinkedPositionGroupCalculator2 method getLinkedPositionGroup.

@Override
public Provider<LinkedPositionGroup> getLinkedPositionGroup(IRenameElementContext renameElementContext, IProgressMonitor monitor) {
    SubMonitor progress = SubMonitor.convert(monitor, 100);
    XtextEditor editor = (XtextEditor) renameElementContext.getTriggeringEditor();
    IProject project = projectUtil.getProject(renameElementContext.getContextResourceURI());
    if (project == null) {
        throw new IllegalStateException("Could not determine the project for context resource " + renameElementContext.getContextResourceURI());
    }
    CachingResourceSetProvider cachingResourceSetProvider = new CachingResourceSetProvider(resourceSetProvider);
    ResourceSet resourceSet = cachingResourceSetProvider.get(project);
    EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
    if (targetElement == null) {
        throw new IllegalStateException("Target element could not be loaded");
    }
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    IRenameStrategy2 renameStrategy = globalServiceProvider.findService(targetElement, IRenameStrategy2.class);
    if (renameStrategy == null) {
        throw new IllegalArgumentException("Cannot find a rename strategy for " + renameElementContext.getTargetElementURI());
    }
    String oldName = simpleNameProvider.getSimpleName(targetElement);
    String newName = getNewDummyName(oldName);
    IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(renameElementContext.getTargetElementURI());
    LtkIssueAcceptor issues = resourceServiceProvider.get(LtkIssueAcceptor.class);
    IChangeSerializer changeSerializer = resourceServiceProvider.get(IChangeSerializer.class);
    changeSerializer.setUpdateRelatedFiles(false);
    RenameChange change = new RenameChange(newName, renameElementContext.getTargetElementURI());
    RenameContext renameContext = new RenameContext(Lists.newArrayList(change), resourceSet, changeSerializer, issues);
    renameStrategy.applyRename(renameContext);
    List<ReplaceEdit> replaceEdits = new ArrayList<>();
    changeSerializer.applyModifications(c -> {
        if (c instanceof ITextDocumentChange) {
            ITextDocumentChange textDocumentChange = (ITextDocumentChange) c;
            textDocumentChange.getReplacements().forEach(replacement -> {
                replaceEdits.add(new ReplaceEdit(replacement.getOffset(), replacement.getLength(), replacement.getReplacementText()));
            });
        }
    });
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    return () -> {
        return createLinkedGroupFromReplaceEdits(replaceEdits, editor, oldName, progress.newChild(10));
    };
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) ArrayList(java.util.ArrayList) ITextDocumentChange(org.eclipse.xtext.ide.serializer.ITextDocumentChange) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LtkIssueAcceptor(org.eclipse.xtext.ui.refactoring2.LtkIssueAcceptor) IProject(org.eclipse.core.resources.IProject) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) IRenameStrategy2(org.eclipse.xtext.ide.refactoring.IRenameStrategy2) EObject(org.eclipse.emf.ecore.EObject) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider)

Example 25 with IChangeSerializer

use of org.eclipse.xtext.ide.serializer.IChangeSerializer 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

IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)26 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)23 Resource (org.eclipse.emf.ecore.resource.Resource)19 IEmfResourceChange (org.eclipse.xtext.ide.serializer.IEmfResourceChange)19 Test (org.junit.Test)19 InMemoryURIHandler (org.eclipse.xtext.testing.util.InMemoryURIHandler)18 Node (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node)11 MandatoryValue (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.MandatoryValue)5 EObject (org.eclipse.emf.ecore.EObject)4 TwoChildLists (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.TwoChildLists)4 SubMonitor (org.eclipse.core.runtime.SubMonitor)3 RenameChange (org.eclipse.xtext.ide.refactoring.RenameChange)3 RenameContext (org.eclipse.xtext.ide.refactoring.RenameContext)3 IProject (org.eclipse.core.resources.IProject)2 URI (org.eclipse.emf.common.util.URI)2 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)2 IRenameStrategy2 (org.eclipse.xtext.ide.refactoring.IRenameStrategy2)2 ResourceRelocationContext (org.eclipse.xtext.ide.refactoring.ResourceRelocationContext)2 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2