Search in sources :

Example 6 with DefaultReferenceDescription

use of org.eclipse.xtext.resource.impl.DefaultReferenceDescription in project xtext-eclipse by eclipse.

the class DelegatingReferenceFinderTest method testLocalAndExternalRefs.

@Test
public void testLocalAndExternalRefs() throws Exception {
    Resource refResource = loadResource("ref.refactoringtestlanguage", "D { ref A }");
    EObject elementD = refResource.getContents().get(0).eContents().get(0);
    acceptor.expect(new DefaultReferenceDescription(elementB, elementA, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementB)));
    findRefs(elementA, resource, localResourceAccess);
    acceptor.assertFinished();
    acceptor.expect(new DefaultReferenceDescription(elementB, elementA, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementB)));
    acceptor.expect(new DefaultReferenceDescription(elementD, elementA, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementD)));
    findAllRefs(elementA, localResourceAccess);
    acceptor.assertFinished();
    acceptor.expect(new DefaultReferenceDescription(elementD, elementA, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementD)));
    findRefs(elementA, refResource, localResourceAccess);
    acceptor.assertFinished();
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) DefaultReferenceDescription(org.eclipse.xtext.resource.impl.DefaultReferenceDescription) Test(org.junit.Test)

Example 7 with DefaultReferenceDescription

use of org.eclipse.xtext.resource.impl.DefaultReferenceDescription 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;
        }
    };
}
Also used : IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) ElementRenameArguments(org.eclipse.xtext.ui.refactoring.ElementRenameArguments) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) URI(org.eclipse.emf.common.util.URI) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) EObject(org.eclipse.emf.ecore.EObject) IDependentElementsCalculator(org.eclipse.xtext.ui.refactoring.IDependentElementsCalculator) DefaultReferenceDescription(org.eclipse.xtext.resource.impl.DefaultReferenceDescription) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider) NoSuchStrategyException(org.eclipse.xtext.ui.refactoring.IRenameStrategy.Provider.NoSuchStrategyException) EReference(org.eclipse.emf.ecore.EReference) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) IRefactoringUpdateAcceptor(org.eclipse.xtext.ui.refactoring.IRefactoringUpdateAcceptor) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) SubMonitor(org.eclipse.core.runtime.SubMonitor) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IProject(org.eclipse.core.resources.IProject) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider) IGlobalServiceProvider(org.eclipse.xtext.resource.IGlobalServiceProvider) RefactoringResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.RefactoringResourceSetProvider) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) Provider(com.google.inject.Provider) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) IReferenceFinder(org.eclipse.xtext.findReferences.IReferenceFinder) RefactoringResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.RefactoringResourceSetProvider) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 8 with DefaultReferenceDescription

use of org.eclipse.xtext.resource.impl.DefaultReferenceDescription in project xtext-eclipse by eclipse.

the class DelegatingReferenceFinderTest method testLocalRefs.

@Test
public void testLocalRefs() throws Exception {
    acceptor.expect(new DefaultReferenceDescription(elementB, elementA, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementB)));
    findAllRefs(elementA, localResourceAccess);
    acceptor.assertFinished();
    acceptor.expect(new DefaultReferenceDescription(elementB, elementA, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementB)));
    findRefs(elementA, resource, localResourceAccess);
    acceptor.assertFinished();
}
Also used : DefaultReferenceDescription(org.eclipse.xtext.resource.impl.DefaultReferenceDescription) Test(org.junit.Test)

Example 9 with DefaultReferenceDescription

use of org.eclipse.xtext.resource.impl.DefaultReferenceDescription in project xtext-eclipse by eclipse.

the class DelegatingReferenceFinderTest method testExternalRefs.

@Test
public void testExternalRefs() throws Exception {
    Resource refResource = loadResource("ref.refactoringtestlanguage", "D { ref C }");
    EObject elementD = refResource.getContents().get(0).eContents().get(0);
    findRefs(elementC, resource, localResourceAccess);
    acceptor.assertFinished();
    acceptor.expect(new DefaultReferenceDescription(elementD, elementC, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementD)));
    findAllRefs(elementC, localResourceAccess);
    acceptor.assertFinished();
    acceptor.expect(new DefaultReferenceDescription(elementD, elementC, RefactoringPackage.Literals.ELEMENT__REFERENCED, 0, EcoreUtil2.getPlatformResourceOrNormalizedURI(elementD)));
    findRefs(elementC, refResource, localResourceAccess);
    acceptor.assertFinished();
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) DefaultReferenceDescription(org.eclipse.xtext.resource.impl.DefaultReferenceDescription) Test(org.junit.Test)

Example 10 with DefaultReferenceDescription

use of org.eclipse.xtext.resource.impl.DefaultReferenceDescription in project n4js by eclipse.

the class HeadlessReferenceFinder method findReferencesTo.

/**
 * Find all references to the given target with its resource set as working environment.
 *
 * @param target
 *            the object to look for.
 * @param monitor
 *            the progress monitor.
 * @return the list of reference descriptions.
 */
public List<IReferenceDescription> findReferencesTo(EObject target, IProgressMonitor monitor) {
    final TargetURIs targetURIs = getTargetURIs(target);
    final ResourceSet resourceSet = target.eResource().getResourceSet();
    final List<IReferenceDescription> result = Lists.newArrayList();
    IReferenceFinder.IResourceAccess resourceAccess = new SimpleResourceAccess(resourceSet);
    IReferenceFinder.Acceptor acceptor = new IReferenceFinder.Acceptor() {

        @Override
        public void accept(IReferenceDescription description) {
            result.add(description);
        }

        @Override
        public void accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
            accept(new DefaultReferenceDescription(sourceURI, targetURI, eReference, index, null));
        }
    };
    referenceFinder.findAllReferences(targetURIs, resourceAccess, resourceDescriptionsProvider.getResourceDescriptions(resourceSet), acceptor, monitor);
    return result;
}
Also used : TargetURIs(org.eclipse.xtext.findReferences.TargetURIs) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) SimpleResourceAccess(org.eclipse.n4js.ts.findReferences.SimpleResourceAccess) IReferenceFinder(org.eclipse.xtext.findReferences.IReferenceFinder) EObject(org.eclipse.emf.ecore.EObject) DefaultReferenceDescription(org.eclipse.xtext.resource.impl.DefaultReferenceDescription) EReference(org.eclipse.emf.ecore.EReference) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription)

Aggregations

DefaultReferenceDescription (org.eclipse.xtext.resource.impl.DefaultReferenceDescription)10 EObject (org.eclipse.emf.ecore.EObject)8 Test (org.junit.Test)8 Resource (org.eclipse.emf.ecore.resource.Resource)6 URI (org.eclipse.emf.common.util.URI)2 EReference (org.eclipse.emf.ecore.EReference)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 IReferenceFinder (org.eclipse.xtext.findReferences.IReferenceFinder)2 IReferenceDescription (org.eclipse.xtext.resource.IReferenceDescription)2 Provider (com.google.inject.Provider)1 IProject (org.eclipse.core.resources.IProject)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)1 SimpleResourceAccess (org.eclipse.n4js.ts.findReferences.SimpleResourceAccess)1 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1 TargetURIs (org.eclipse.xtext.findReferences.TargetURIs)1 IGlobalServiceProvider (org.eclipse.xtext.resource.IGlobalServiceProvider)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)1