Search in sources :

Example 76 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class ResourceDescriptionsUtil method findReferencesToResources.

/**
 * Find all {@link IResourceDescription}s of all resources containing cross-references to any of the objects.
 *
 * @param descriptions
 *          {@link IResourceDescriptions} to find the references in.
 * @param targetResources
 *          Target objects.
 * @param matchPolicy
 *          match policy
 * @return An {@link Iterable} of all {@link IResourceDescription}s that reference any of the objects.
 */
@SuppressWarnings("PMD.NPathComplexity")
public static Iterable<IResourceDescription> findReferencesToResources(final IResourceDescriptions descriptions, final Set<IResourceDescription> targetResources, final ReferenceMatchPolicy matchPolicy) {
    if (targetResources.isEmpty()) {
        return ImmutableSet.of();
    }
    final boolean matchNames = matchPolicy.includes(ReferenceMatchPolicy.IMPORTED_NAMES) || matchPolicy.includes(ReferenceMatchPolicy.UNRESOLVED_IMPORTED_NAMES);
    final Set<URI> targetUris = Sets.newHashSetWithExpectedSize(targetResources.size());
    final Set<QualifiedName> exportedNames = Sets.newHashSet();
    for (IResourceDescription res : targetResources) {
        targetUris.add(res.getURI());
        if (matchNames) {
            for (IEObjectDescription obj : res.getExportedObjects()) {
                if (matchPolicy.includes(ReferenceMatchPolicy.IMPORTED_NAMES)) {
                    exportedNames.add(obj.getName().toLowerCase());
                }
                if (matchPolicy.includes(ReferenceMatchPolicy.UNRESOLVED_IMPORTED_NAMES)) {
                    exportedNames.add(QualifiedNames.toUnresolvedName(obj.getName().toLowerCase()));
                }
            }
        }
    }
    return Iterables.filter(descriptions.getAllResourceDescriptions(), input -> {
        if (matchNames) {
            for (QualifiedName name : input.getImportedNames()) {
                if (exportedNames.contains(name.toLowerCase())) {
                    // NOPMD
                    return true;
                }
            }
        }
        if (matchPolicy.includes(ReferenceMatchPolicy.REFERENCES)) {
            for (IReferenceDescription ref : input.getReferenceDescriptions()) {
                if (targetUris.contains(ref.getTargetEObjectUri().trimFragment())) {
                    return true;
                }
            }
        }
        return false;
    });
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription)

Example 77 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class AbstractRecursiveScope method getSingleElement.

/**
 * {@inheritDoc}
 */
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
    // NOPMD NPathComplexity
    if (name == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Null name in getContentByName");
    }
    try {
        if (DEBUG && !invocationTrace.add(name)) {
            throw new IllegalStateException(Messages.bind(CYCLIC_DEPENDENCY_MESSAGE, String.valueOf(name)));
        }
        // NOPMD UseLocaleWithCaseConversions
        final QualifiedName lookupName = isIgnoreCase() ? name.toLowerCase() : name;
        IEObjectDescription result = null;
        result = nameCache.get(lookupName);
        if (result != null) {
            // NOPMD CompareObjectsWithEquals
            return result == NULL_DESCRIPTION ? null : result;
        }
        if (contentsIterator == null) {
            contentsIterator = getAllLocalElements().iterator();
        }
        while (result == null && contentsIterator.hasNext()) {
            result = contentsIterator.next();
            QualifiedName thisName = result.getName();
            if (isIgnoreCase()) {
                // NOPMD UseLocaleWithCaseConversions
                thisName = thisName.toLowerCase();
            }
            if (!isIgnoreCase() || nameCache.get(thisName) == null) {
                nameCache.put(thisName, result);
            }
            if (!lookupName.equals(thisName)) {
                // NOPMD NullAssignment
                result = null;
            }
        }
        if (result == null) {
            result = getParent().getSingleElement(name);
        // Possibly cache this result, too. For the time being, let the outer scope use its own cache, otherwise we'll duplicate
        // a lot.
        }
        if (result == null) {
            nameCache.put(lookupName, NULL_DESCRIPTION);
        // } else {
        // ScopeTrace.addTrace(result, getId());
        }
        return result;
    } catch (ConcurrentModificationException e) {
        // cache invalidated: retry
        reset();
        // Remove name from invocation trace, otherwise the intended retry recursive call will fail
        if (DEBUG) {
            invocationTrace.remove(name);
        }
        return getSingleElement(name);
    } finally {
        if (DEBUG) {
            invocationTrace.remove(name);
        }
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 78 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class AbstractRecursiveScope method getSingleElement.

/**
 * {@inheritDoc}
 */
@Override
public synchronized IEObjectDescription getSingleElement(final EObject object) {
    try {
        if (DEBUG && !invocationTrace.add(object)) {
            throw new IllegalStateException(Messages.bind(CYCLIC_DEPENDENCY_MESSAGE, String.valueOf(object)));
        }
        final URI key = EcoreUtil.getURI(object);
        IEObjectDescription result = objectCache.get(key);
        if (result == NULL_DESCRIPTION) {
            // NOPMD CompareObjectsWithEquals
            return null;
        } else if (result == null) {
            result = super.getSingleElement(object);
            if (result == null) {
                objectCache.put(key, NULL_DESCRIPTION);
            } else {
                objectCache.put(key, result);
            }
        }
        return result;
    } finally {
        if (DEBUG) {
            invocationTrace.remove(object);
        }
    }
}
Also used : URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 79 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class ContainerBasedScope method getSingleElement.

// Using QualifiedName#toLowerCase() not String#toLowerCase()
@SuppressWarnings({ "PMD.UseLocaleWithCaseConversions", "PMD.NPathComplexity" })
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
    if (nameFunctions != null && nameFunctions.contains(NameFunctions.exportNameFunction())) {
        final boolean ignoreCase = isIgnoreCase();
        final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
        final IEObjectDescription cachedResult = contentByNameCache.get(lookupName);
        if (cachedResult != null) {
            if (cachedResult != NULL_DESCRIPTION) {
                return cachedResult;
            }
        // Otherwise check for aliasing and if yes revert to normal behavior or delegate or parent otherwise
        } else {
            QualifiedName namePattern = criteria.getNamePattern();
            if (namePattern != null && ignoreCase) {
                namePattern = namePattern.toLowerCase();
            }
            if (namePattern == null || namePattern.equals(lookupName) || (namePattern instanceof QualifiedNamePattern && ((QualifiedNamePattern) namePattern).matches(lookupName))) {
                final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(lookupName).ignoreCase(ignoreCase);
                final Iterable<IEObjectDescription> queryResult = copy.execute(container);
                IEObjectDescription description = Iterables.getFirst(queryResult, null);
                if (description != null) {
                    contentByNameCache.put(lookupName, description);
                    return description;
                }
                contentByNameCache.put(lookupName, NULL_DESCRIPTION);
            }
        }
        // in case of aliasing revert to normal behavior
        return nameFunctions.size() > 1 ? super.getSingleElement(name) : getParent().getSingleElement(name);
    } else {
        return super.getSingleElement(name);
    }
}
Also used : QualifiedNamePattern(com.avaloq.tools.ddk.xtext.naming.QualifiedNamePattern) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 80 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.

the class ContainerBasedScope method getLocalElementsByName.

// Using QualifiedName#toLowerCase() not String#toLowerCase()
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
@Override
protected Iterable<IEObjectDescription> getLocalElementsByName(final QualifiedName name) {
    if (nameFunctions != null && nameFunctions.size() == 1 && nameFunctions.contains(NameFunctions.exportNameFunction())) {
        final boolean ignoreCase = isIgnoreCase();
        final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
        QualifiedName namePattern = criteria.getNamePattern();
        if (namePattern != null && ignoreCase) {
            namePattern = namePattern.toLowerCase();
        }
        if (namePattern == null || namePattern.equals(lookupName) || (namePattern instanceof QualifiedNamePattern && ((QualifiedNamePattern) namePattern).matches(lookupName))) {
            final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(lookupName).ignoreCase(ignoreCase);
            return copy.execute(container);
        }
        return Collections.<IEObjectDescription>emptyList();
    } else {
        return super.getLocalElementsByName(name);
    }
}
Also used : QualifiedNamePattern(com.avaloq.tools.ddk.xtext.naming.QualifiedNamePattern) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)324 Test (org.junit.Test)95 EObject (org.eclipse.emf.ecore.EObject)82 QualifiedName (org.eclipse.xtext.naming.QualifiedName)79 IScope (org.eclipse.xtext.scoping.IScope)56 URI (org.eclipse.emf.common.util.URI)41 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)33 EClass (org.eclipse.emf.ecore.EClass)30 Resource (org.eclipse.emf.ecore.resource.Resource)28 EObjectDescription (org.eclipse.xtext.resource.EObjectDescription)22 AliasedEObjectDescription (org.eclipse.xtext.resource.impl.AliasedEObjectDescription)22 ArrayList (java.util.ArrayList)19 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)14 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 StyledString (org.eclipse.jface.viewers.StyledString)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 StringInputStream (org.eclipse.xtext.util.StringInputStream)11 EReference (org.eclipse.emf.ecore.EReference)10 List (java.util.List)9