use of org.eclipse.xtext.resource.IReferenceDescription in project xtext-eclipse by eclipse.
the class DefaultLinkedPositionGroupCalculator method getLinkedPositionGroup.
@Override
public Provider<LinkedPositionGroup> getLinkedPositionGroup(IRenameElementContext renameElementContext, IProgressMonitor monitor) {
final SubMonitor progress = SubMonitor.convert(monitor, 100);
final XtextEditor editor = (XtextEditor) renameElementContext.getTriggeringEditor();
IProject project = projectUtil.getProject(renameElementContext.getContextResourceURI());
if (project == null)
throw new IllegalStateException("Could not determine project for context resource " + renameElementContext.getContextResourceURI());
RefactoringResourceSetProvider resourceSetProvider = new CachingResourceSetProvider(DefaultLinkedPositionGroupCalculator.this.resourceSetProvider);
ResourceSet resourceSet = resourceSetProvider.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();
}
IRenameStrategy.Provider strategyProvider = globalServiceProvider.findService(targetElement, IRenameStrategy.Provider.class);
IRenameStrategy renameStrategy = null;
try {
renameStrategy = strategyProvider.get(targetElement, renameElementContext);
} catch (NoSuchStrategyException exc) {
// handle in next line
}
if (renameStrategy == null)
throw new IllegalArgumentException("Cannot find a rename strategy for " + notNull(renameElementContext.getTargetElementURI()));
String newName = renameStrategy.getOriginalName();
IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(renameElementContext.getTargetElementURI());
IDependentElementsCalculator dependentElementsCalculator = resourceServiceProvider.get(IDependentElementsCalculator.class);
Iterable<URI> dependentElementURIs = dependentElementsCalculator.getDependentElementURIs(targetElement, progress.newChild(10));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
LocalResourceRefactoringUpdateAcceptor updateAcceptor = updateAcceptorProvider.get();
updateAcceptor.setLocalResourceURI(renameElementContext.getContextResourceURI());
renameStrategy.createDeclarationUpdates(newName, resourceSet, updateAcceptor);
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
Map<URI, URI> original2newEObjectURI = renamedElementTracker.renameAndTrack(concat(Collections.singleton(renameElementContext.getTargetElementURI()), dependentElementURIs), newName, resourceSet, renameStrategy, progress.newChild(10));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
ElementRenameArguments elementRenameArguments = new ElementRenameArguments(renameElementContext.getTargetElementURI(), newName, renameStrategy, original2newEObjectURI, resourceSetProvider);
final List<IReferenceDescription> referenceDescriptions = newArrayList();
IReferenceFinder.Acceptor referenceAcceptor = new IReferenceFinder.Acceptor() {
@Override
public void accept(IReferenceDescription referenceDescription) {
referenceDescriptions.add(referenceDescription);
}
@Override
public void accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
referenceDescriptions.add(new DefaultReferenceDescription(EcoreUtil2.getFragmentPathURI(source), targetURI, eReference, index, null));
}
};
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
referenceFinder.findReferences(targetURIConverter.fromIterable(elementRenameArguments.getRenamedElementURIs()), resourceSet.getResource(renameElementContext.getContextResourceURI(), true), referenceAcceptor, progress.newChild(10));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
referenceUpdater.createReferenceUpdates(elementRenameArguments, referenceDescriptions, updateAcceptor, progress.newChild(60));
final List<ReplaceEdit> textEdits = updateAcceptor.getTextEdits();
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
final IRenameStrategy renameStrategy2 = renameStrategy;
return new Provider<LinkedPositionGroup>() {
@Override
public LinkedPositionGroup get() {
LinkedPositionGroup linkedGroup = createLinkedGroupFromReplaceEdits(textEdits, editor, renameStrategy2.getOriginalName(), progress.newChild(10));
return linkedGroup;
}
};
}
use of org.eclipse.xtext.resource.IReferenceDescription in project xtext-eclipse by eclipse.
the class ReferenceDescriptionSorter method sortByResource.
public Multimap<URI, IReferenceDescription> sortByResource(Iterable<IReferenceDescription> referenceDescriptions) {
Multimap<URI, IReferenceDescription> referencesByResourceURI = HashMultimap.create();
for (IReferenceDescription referenceDescription : referenceDescriptions) {
URI sourceResourceUri = referenceDescription.getSourceEObjectUri().trimFragment();
referencesByResourceURI.put(sourceResourceUri, referenceDescription);
}
return referencesByResourceURI;
}
use of org.eclipse.xtext.resource.IReferenceDescription in project xtext-eclipse by eclipse.
the class ReferenceDescriptionSorter method sortByProject.
public Multimap<IProject, IReferenceDescription> sortByProject(Iterable<IReferenceDescription> referenceDescriptions) {
Multimap<IProject, IReferenceDescription> referencesByProject = HashMultimap.create();
for (IReferenceDescription referenceDescription : referenceDescriptions) {
URI sourceResourceUri = referenceDescription.getSourceEObjectUri().trimFragment();
IProject project = projectUtil.getProject(sourceResourceUri);
if (project != null) {
referencesByProject.put(project, referenceDescription);
}
}
return referencesByProject;
}
use of org.eclipse.xtext.resource.IReferenceDescription in project xtext-eclipse by eclipse.
the class DefaultReferenceUpdater method createReferenceUpdates.
@Override
protected void createReferenceUpdates(ElementRenameArguments elementRenameArguments, Multimap<URI, IReferenceDescription> resource2references, ResourceSet resourceSet, IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor) {
SubMonitor progress = SubMonitor.convert(monitor, "Creating reference updates", resource2references.keySet().size());
for (URI referringResourceURI : resource2references.keySet()) {
if (progress.isCanceled())
return;
Resource referringResource = resourceSet.getResource(referringResourceURI, false);
if (!(referringResource instanceof XtextResource)) {
updateAcceptor.getRefactoringStatus().add(ERROR, "Resource {0} is not an XtextResource.", referringResource.getURI(), resourceSet);
} else {
Iterable<IReferenceDescription> referenceDescriptions = resource2references.get(referringResourceURI);
processReferringResource(referringResource, referenceDescriptions, elementRenameArguments, updateAcceptor, progress);
}
progress.worked(1);
}
}
use of org.eclipse.xtext.resource.IReferenceDescription in project xtext-eclipse by eclipse.
the class EmfResourceReferenceUpdater method createReferenceUpdates.
@Override
protected void createReferenceUpdates(ElementRenameArguments elementRenameArguments, Multimap<URI, IReferenceDescription> resource2references, ResourceSet resourceSet, IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor) {
for (URI referringResourceURI : resource2references.keySet()) {
try {
if (monitor.isCanceled())
throw new OperationCanceledException();
Resource referringResource = resourceSet.getResource(referringResourceURI, false);
EObject refactoredElement = resourceSet.getEObject(elementRenameArguments.getNewElementURI(elementRenameArguments.getTargetElementURI()), true);
if (referringResource != refactoredElement.eResource()) {
if (refactoredElement instanceof EClassifier) {
for (IReferenceDescription reference : resource2references.get(referringResourceURI)) {
EObject source = referringResource.getEObject(reference.getSourceEObjectUri().fragment());
if (source == null) {
LOG.error("Couldn't find source element " + reference.getSourceEObjectUri() + " in " + referringResource.getURI());
} else {
EObject referringEReference = source.eContainer();
if (referringEReference != null && referringEReference instanceof EReference)
((EReference) referringEReference).setEType((EClassifier) refactoredElement);
}
}
}
changeUtil.addSaveAsUpdate(referringResource, updateAcceptor);
}
} catch (OperationCanceledException e) {
throw e;
} catch (Exception exc) {
throw new WrappedException(exc);
}
}
}
Aggregations