private void init(Data result, IResourceAccess resourceAccess, TargetURIs targetURIs) { targetURIs.getTargetResourceURIs().forEach((resourceURI) -> { resourceAccess.readOnly(resourceURI, (resourceSet) -> { targetURIs.getEObjectURIs(resourceURI).forEach((objectURI) -> { try { EObject object = resourceSet.getEObject(objectURI, true); if (object != null) { result.add(object); } } catch (RuntimeException e) { // ignore } }); return null; }); }); }
@Override public void findReferences(Predicate<URI> targetURIs, Resource resource, Acceptor acceptor, IProgressMonitor monitor) { // make sure data is present keys.getData((TargetURIs) targetURIs, new SimpleResourceAccess(resource.getResourceSet())); EList<EObject> astContents; if (resource instanceof N4JSResource) { // In case of N4JSResource, we search only in the AST but NOT in TModule tree! Script script = (Script) ((N4JSResource) resource).getContents().get(0); astContents = new BasicEList<>(); astContents.add(script); } else { astContents = resource.getContents(); } for (EObject content : astContents) { findReferences(targetURIs, content, acceptor, monitor); } }
@Override protected EObject toValidInstanceOrNull(Resource resource, Predicate<URI> targetURIs, EObject value) { EObject result = value; if (result.eIsProxy()) { URI proxyURI = EcoreUtil.getURI(result); if (uriEncoder.isCrossLinkFragment(resource, proxyURI.fragment())) { INode node = uriEncoder.decode(resource, proxyURI.fragment()).getThird(); String string = linkingHelper.getCrossRefNodeAsString(node, true); if (((TargetURIs) targetURIs).getUserData(TargetURIKey.KEY).isMatchingConcreteSyntax(string)) { result = resolveInternalProxy(value, resource); } else { result = null; } } else { result = resolveInternalProxy(value, resource); } } return result; }
/** * 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; }
public List<? extends Location> getDefinitions(final XtextResource resource, final int offset, final IReferenceFinder.IResourceAccess resourceAccess, final CancelIndicator cancelIndicator) { final EObject element = this._eObjectAtOffsetHelper.resolveElementAt(resource, offset); if ((element == null)) { return CollectionLiterals.<Location>emptyList(); } final ArrayList<Location> locations = CollectionLiterals.<Location>newArrayList(); final TargetURIs targetURIs = this.collectTargetURIs(element); for (final URI targetURI : targetURIs) { { this.operationCanceledManager.checkCanceled(cancelIndicator); final Procedure1<EObject> _function = (EObject obj) -> { final Location location = this._documentExtensions.newLocation(obj); if ((location != null)) { locations.add(location); } }; this.doRead(resourceAccess, targetURI, _function); } } return locations; }
public List<? extends Location> getReferences(final XtextResource resource, final int offset, final IReferenceFinder.IResourceAccess resourceAccess, final IResourceDescriptions indexData, final CancelIndicator cancelIndicator) { final EObject element = this._eObjectAtOffsetHelper.resolveElementAt(resource, offset); if ((element == null)) { return CollectionLiterals.<Location>emptyList(); } final ArrayList<Location> locations = CollectionLiterals.<Location>newArrayList(); final TargetURIs targetURIs = this.collectTargetURIs(element); final IAcceptor<IReferenceDescription> _function = (IReferenceDescription reference) -> { final Procedure1<EObject> _function_1 = (EObject obj) -> { final Location location = this._documentExtensions.newLocation(obj, reference.getEReference(), reference.getIndexInList()); if ((location != null)) { locations.add(location); } }; this.doRead(resourceAccess, reference.getSourceEObjectUri(), _function_1); }; ReferenceAcceptor _referenceAcceptor = new ReferenceAcceptor(this.resourceServiceProviderRegistry, _function); CancelIndicatorProgressMonitor _cancelIndicatorProgressMonitor = new CancelIndicatorProgressMonitor(cancelIndicator); this.referenceFinder.findAllReferences(targetURIs, resourceAccess, indexData, _referenceAcceptor, _cancelIndicatorProgressMonitor); return locations; }
protected void findSourceDeclarations(final URI targetDeclarationURI, final IProgressMonitor monitor, final Procedure2<? super IEObjectDescription, ? super IReferenceDescription> acceptor) { final TargetURIs targetURIs = this.collectTargetURIs(targetDeclarationURI); IResourceServiceProvider.Registry _resourceServiceProviderRegistry = this.getResourceServiceProviderRegistry(); final IAcceptor<IReferenceDescription> _function = (IReferenceDescription reference) -> { boolean _filterReference = this.filterReference(reference); if (_filterReference) { IEObjectDescription _findSourceDeclaration = null; if (reference!=null) { _findSourceDeclaration=this.findSourceDeclaration(reference); } final IEObjectDescription sourceDeclaration = _findSourceDeclaration; acceptor.apply(sourceDeclaration, reference); } }; ReferenceAcceptor _referenceAcceptor = new ReferenceAcceptor(_resourceServiceProviderRegistry, _function); this.getReferenceFinder().findAllReferences(targetURIs, this.getResourceAccess(), this.getIndexData(), _referenceAcceptor, monitor); }
/** * Get or create a data cache in the user data space of the given target URIs. * * @return a new or existing data cache. */ public Data getData(TargetURIs targetURIs, IReferenceFinder.IResourceAccess resourceAccess) { Data result = targetURIs.getUserData(KEY); if (result != null) { return result; } return initData(targetURIs, resourceAccess); }
@Override protected void doAdd(EObject primaryTarget, TargetURIs targetURIsAddHere) { Resource resource = primaryTarget.eResource(); // If the target is not contained in a resource, we cannot do anything but return. if (resource == null) return; EcoreUtil.resolveAll(primaryTarget.eResource()); // Special handling for composed members and TStructMember if (primaryTarget instanceof TMember && ((TMember) primaryTarget).isComposed()) { // In case of composed member, add the constituent members instead. List<TMember> constituentMembers = ((TMember) primaryTarget).getConstituentMembers(); for (TMember constituentMember : constituentMembers) { super.doAdd(constituentMember, targetURIsAddHere); } } else { if (primaryTarget instanceof TStructMember) { TStructMember crossRefStructMember = ((TStructMember) primaryTarget).getDefinedMember(); if (crossRefStructMember != null) // If this TStructMember is an AST, also add the defined member located in the TModule super.doAdd(((TStructMember) primaryTarget).getDefinedMember(), targetURIsAddHere); } super.doAdd(primaryTarget, targetURIsAddHere); } inferredElements.collectInferredElements(primaryTarget, (object) -> { if (object != null) { super.doAdd(object, targetURIsAddHere); } }, projectUtils); }
@Override protected void doAdd(EObject object, TargetURIs targetURIs) { super.doAdd(object, targetURIs); Set<EObject> jvmElements = associations.getJvmElements(object); for(EObject jvmElement: jvmElements) { super.doAdd(jvmElement, targetURIs); } }
protected TargetURIs collectTargetURIs(final URI targetURI) { final TargetURIs targetURIs = this.getTargetURIProvider().get(); if ((targetURI == null)) { return targetURIs; } final IUnitOfWork<Object, EObject> _function = (EObject targetObject) -> { if ((targetObject != null)) { this.getTargetURICollector().add(targetObject, targetURIs); } return null; }; this.<Object>readOnly(targetURI, _function); return targetURIs; }
/** {@inheritDoc} */ @Override public void findAllReferences(final TargetURIs targetURIs, final IResourceAccess resourceAccess, final IResourceDescriptions indexData, final Acceptor acceptor, final IProgressMonitor monitor) { final SubMonitor subMonitor = SubMonitor.convert(monitor, 10); if (!Iterables.isEmpty(targetURIs)) { if (indexData instanceof IResourceDescriptions2) { findReferences(targetURIs, Sets.newHashSet(targetURIs.getTargetResourceURIs()), resourceAccess, indexData, acceptor, subMonitor.newChild(1)); findAllIndexedReferences(targetURIs, indexData, acceptor, subMonitor.newChild(9)); } else { super.findAllReferences(targetURIs, resourceAccess, indexData, acceptor, subMonitor); } } }
/** * Uses IResourceDescriptions2 to find all indexed references. * * @param targetURIs * the URIs to find references to, must not be {@code null} * @param indexData * index to use when finding references, must not be {@code null} * @param referenceAcceptor * the reference acceptor, must not be {@code null} * @param subMonitor * the progress monitor, can be {@code null} */ protected void findAllIndexedReferences(final TargetURIs targetURIs, final IResourceDescriptions indexData, final Acceptor referenceAcceptor, final SubMonitor subMonitor) { IResourceDescriptions2 idx = (IResourceDescriptions2) indexData; List<IReferenceDescription> refs = uniqueReferences(Lists.newArrayList(idx.findReferencesToObjects(targetURIs.asSet()))); final SubMonitor monitor = SubMonitor.convert(subMonitor, Messages.ReferenceQuery_monitor, refs.size()); for (IReferenceDescription desc : refs) { if (monitor.isCanceled()) { return; } referenceAcceptor.accept(desc); monitor.worked(1); } monitor.done(); }
private Data initData(TargetURIs targetURIs, IReferenceFinder.IResourceAccess resourceAccess) { Data result = new Data(qualifiedNameProvider); init(result, resourceAccess, targetURIs); targetURIs.putUserData(KEY, result); return result; }
@Override protected void findReferencesInDescription(TargetURIs targetURIs, IResourceDescription resourceDescription, IResourceAccess resourceAccess, Acceptor acceptor, IProgressMonitor monitor) { TargetURIKey.Data findReferencesData = keys.getData(targetURIs, resourceAccess); if (monitor.isCanceled()) throw new OperationCanceledException(); final Set<QualifiedName> typesOrModulesToFind = findReferencesData.getTypesOrModulesToFind(); if (typesOrModulesToFind.isEmpty()) { return; } Iterable<QualifiedName> importedNames = resourceDescription.getImportedNames(); // scenario 1: imported names from index // TODO check if imported names from editors are returned as a set? // maybe we need another check for instanceof SortedSet<?> if (importedNames instanceof List<?>) { List<QualifiedName> sorted = (List<QualifiedName>) importedNames; List<QualifiedName> searchMe = sorted; // Optimize search for (QualifiedName typeOrModuleToFind : typesOrModulesToFind) { int insertionIndex = Collections.binarySearch(searchMe, typeOrModuleToFind); if (insertionIndex >= 0) { resourceAccess.readOnly( resourceDescription.getURI(), (resourceSet) -> { findReferences(targetURIs, resourceSet.getResource(resourceDescription.getURI(), true), acceptor, monitor); return null; }); return; } else { int startFrom = -(insertionIndex + 1); if (startFrom >= sorted.size()) { return; } searchMe = sorted.subList(startFrom, sorted.size()); } } } else { for (QualifiedName importedName : importedNames) { if (typesOrModulesToFind.contains(importedName)) { resourceAccess.readOnly( resourceDescription.getURI(), (resourceSet) -> { findReferences(targetURIs, resourceSet.getResource(resourceDescription.getURI(), true), acceptor, monitor); return null; }); return; } } } }
@Override protected boolean doProcess(EReference reference, Predicate<URI> targetURISet) { return ((TargetURIs) targetURISet).getUserData(TargetURIKey.KEY) .isEReferenceTypeApplicable(reference.getEReferenceType()); }
private TargetURIs getTargetURIs(EObject primaryTarget) { TargetURIs result = targetURIsProvider.get(); uriCollector.add(primaryTarget, result); return result; }
@Override public Data getData(TargetURIs targetURIs, IResourceAccess resourceAccess) { return super.getData(targetURIs, new ForwardingResourceAccess(resourceAccess, resourceSetProvider)); }
protected TargetURIs collectTargetURIs(final EObject targetObject) { final TargetURIs targetURIs = this.targetURIProvider.get(); this.targetURICollector.add(targetObject, targetURIs); return targetURIs; }
@Pure protected Provider<TargetURIs> getTargetURIProvider() { return this.targetURIProvider; }
public void setTargetURIProvider(final Provider<TargetURIs> targetURIProvider) { this.targetURIProvider = targetURIProvider; }
/** * Returns with an iterable of URIs that points to all elements that are * referenced by the argument or vice-versa. * * @return an iterable of URIs that are referenced by the argument or the * other way around. */ protected Iterable<URI> getTargetURIs(final EObject primaryTarget) { final TargetURIs result = targetURIsProvider.get(); uriCollector.add(primaryTarget, result); return result; }