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);
}
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);
}
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);
}
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));
};
}
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();
}
Aggregations