@Override protected List<IEObjectDescription> getLocalElementsByName(QualifiedName name) { XAbstractFeatureCall featureCall = getFeatureCall(); if (featureCall.isExplicitOperationCallOrBuilderSyntax()) return Collections.emptyList(); QualifiedName fqn = parentSegments.append(name); IScope typeScope = getSession().getScope(getFeatureCall(), TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, resolvedTypes); IEObjectDescription typeDescription = typeScope.getSingleElement(fqn); if (typeDescription != null) { EObject type = typeDescription.getEObjectOrProxy(); if (type instanceof JvmType) return Collections.<IEObjectDescription>singletonList(new TypeLiteralDescription( new AliasedEObjectDescription(name, typeDescription), isVisible((JvmType) type))); } return Collections.emptyList(); }
protected Iterable<IEObjectDescription> getAliasedElements(Iterable<IEObjectDescription> candidates) { Multimap<QualifiedName, IEObjectDescription> keyToDescription = LinkedHashMultimap.create(); Multimap<QualifiedName, ImportNormalizer> keyToNormalizer = HashMultimap.create(); for (IEObjectDescription imported : candidates) { QualifiedName fullyQualifiedName = imported.getName(); for (ImportNormalizer normalizer : normalizers) { QualifiedName alias = normalizer.deresolve(fullyQualifiedName); if (alias != null) { QualifiedName key = alias; if (isIgnoreCase()) { key = key.toLowerCase(); } keyToDescription.put(key, new AliasedEObjectDescription(alias, imported)); keyToNormalizer.put(key, normalizer); } } } for (QualifiedName name : keyToNormalizer.keySet()) { if (keyToNormalizer.get(name).size() > 1) keyToDescription.removeAll(name); } return keyToDescription.values(); }
@Override protected Iterable<IEObjectDescription> getAliasedElements(Iterable<IEObjectDescription> candidates) { Multimap<QualifiedName, IEObjectDescription> keyToDescription = LinkedHashMultimap.create(); Multimap<QualifiedName, ImportNormalizer> keyToNormalizer = HashMultimap.create(); for (IEObjectDescription imported : candidates) { QualifiedName fullyQualifiedName = imported.getName(); for (ImportNormalizer normalizer : normalizers) { QualifiedName alias = normalizer.deresolve(fullyQualifiedName); if (alias != null) { QualifiedName key = alias; if (isIgnoreCase()) { key = key.toLowerCase(); } keyToDescription.put(key, new AliasedEObjectDescription(alias, imported)); keyToNormalizer.put(key, normalizer); } } } return keyToDescription.values(); }
@Override protected Iterable<IEObjectDescription> getLocalElementsByName(QualifiedName name) { List<IEObjectDescription> result = newArrayList(); ISelectable importFrom = getImportFrom(); for (ImportNormalizer normalizer : normalizers) { final QualifiedName resolvedName = normalizer.resolve(name); if (resolvedName != null) { Iterable<IEObjectDescription> resolvedElements = importFrom.getExportedObjects(type, resolvedName, isIgnoreCase()); for (IEObjectDescription resolvedElement : resolvedElements) { QualifiedName alias = normalizer.deresolve(resolvedElement.getName()); if (alias == null) throw new IllegalStateException("Couldn't deresolve " + resolvedElement.getName() + " with import " + normalizer); final AliasedEObjectDescription aliasedEObjectDescription = new AliasedEObjectDescription(alias, resolvedElement); result.add(aliasedEObjectDescription); } } } return result; }
private Collection<IEObjectDescription> getDescriptionFor(EObject obj) { List<IEObjectDescription> descriptions = Lists.newArrayList(); QualifiedName qualifiedName = qualifiedNameProvider.getFullyQualifiedName(obj); if (qualifiedName == null) { return Collections.emptyList(); } IEObjectDescription description = new EObjectDescription(qualifiedName, obj, null); descriptions.add(description); Optional<QualifiedName> alternativeName = alternativeQualifiedNameProvider .getAlternativeFullyQualifiedName(obj); if (alternativeName.isPresent()) { IEObjectDescription alternativeDescription = new AliasedEObjectDescription(alternativeName.get(), description); descriptions.add(alternativeDescription); } return descriptions; }
/** * Helper method to be used instead of instanceof-checks, as it also covers {@link AliasedEObjectDescription}s. * * @return true if given description is an (aliased) IEObjectDescriptionWithError */ public static boolean isErrorDescription(final IEObjectDescription eObjectDescription) { if (eObjectDescription instanceof IEObjectDescriptionWithError) { return true; } if (eObjectDescription instanceof AliasedEObjectDescription) { return isErrorDescription(((AliasedEObjectDescription) eObjectDescription).getAliasedEObjectDescription()); } return false; }
/** * Helper method to be used casting to IEObjectDescriptionWithError as it also covers * {@link AliasedEObjectDescription}s. * * @return the casted (or delegated) IEObjectDescriptionWithError, or null if description does not contain an error. */ public static IEObjectDescriptionWithError getDescriptionWithError(final IEObjectDescription eObjectDescription) { if (eObjectDescription instanceof IEObjectDescriptionWithError) { return (IEObjectDescriptionWithError) eObjectDescription; } if (eObjectDescription instanceof AliasedEObjectDescription) { return getDescriptionWithError(((AliasedEObjectDescription) eObjectDescription) .getAliasedEObjectDescription()); } return null; }
/** * Creates proposal taking semantics of the N4JS imports into account. * * @param candidate * the original input for which we create proposal * @param reference * the reference * @param context * the context * @return candidate proposal adjusted to the N4JS imports */ private IEObjectDescription getAliasedDescription(IEObjectDescription candidate, EReference reference, ContentAssistContext context) { // Content assist at a location where only simple names are allowed: // We found a qualified name and we'd need an import to be allowed to use // that name. Consider only the simple name of the element from the index // and make sure that the import is inserted as soon as the proposal is applied QualifiedName inputQN = candidate.getName(); int inputNameSegmentCount = inputQN.getSegmentCount(); if (reference == N4JSPackage.Literals.IDENTIFIER_REF__ID && inputNameSegmentCount > 1) return new AliasedEObjectDescription(QualifiedName.create(inputQN.getLastSegment()), candidate); // filter out non-importable things: // globally provided things should never be imported: if (inputNameSegmentCount == 2 && N4TSQualifiedNameProvider.GLOBAL_NAMESPACE_SEGMENT .equals(inputQN.getFirstSegment())) return new AliasedEObjectDescription(QualifiedName.create(inputQN.getLastSegment()), candidate); // special handling for default imports: if (inputQN.getLastSegment().equals(N4JSLanguageConstants.EXPORT_DEFAULT_NAME)) { EObject element = candidate.getEObjectOrProxy(); if (element instanceof TExportableElement) { TExportableElement exported = (TExportableElement) element; if (N4JSLanguageConstants.EXPORT_DEFAULT_NAME.equals(exported.getExportedName())) { return new AliasedEObjectDescription(inputQN, candidate); } } // not accessed via namespace QualifiedName nameNoDefault = inputQN.skipLast(1); QualifiedName moduleName = nameNoDefault.getSegmentCount() > 1 ? QualifiedName.create(nameNoDefault.getLastSegment()) : nameNoDefault; return new AliasedEObjectDescription(moduleName, candidate); } // no special handling, return original input return candidate; }
@Override protected Iterable<IEObjectDescription> getLocalElementsByName(QualifiedName name) { List<IEObjectDescription> result = newArrayList(); QualifiedName resolvedQualifiedName = null; ISelectable importFrom = getImportFrom(); for (ImportNormalizer normalizer : normalizers) { final QualifiedName resolvedName = normalizer.resolve(name); if (resolvedName != null) { Iterable<IEObjectDescription> resolvedElements = importFrom.getExportedObjects(type, resolvedName, isIgnoreCase()); for (IEObjectDescription resolvedElement : resolvedElements) { if (resolvedQualifiedName == null) resolvedQualifiedName = resolvedName; else if (!resolvedQualifiedName.equals(resolvedName)) { if (result.get(0).getEObjectOrProxy() != resolvedElement.getEObjectOrProxy()) { return emptyList(); } } QualifiedName alias = normalizer.deresolve(resolvedElement.getName()); if (alias == null) throw new IllegalStateException("Couldn't deresolve " + resolvedElement.getName() + " with import " + normalizer); final AliasedEObjectDescription aliasedEObjectDescription = new AliasedEObjectDescription(alias, resolvedElement); result.add(aliasedEObjectDescription); } } } return result; }
@Test public void testAllAliasedElements_00() throws Exception { final IEObjectDescription desc1 = new EObjectDescription(QualifiedName.create("com","foo"), EcorePackage.Literals.EANNOTATION, null); final IEObjectDescription desc2 = new EObjectDescription(QualifiedName.create("com","foo"), EcorePackage.Literals.EATTRIBUTE, null); final ArrayList<IEObjectDescription> newArrayList = newArrayList(desc1,desc2); ImportNormalizer n1 = new ImportNormalizer(QualifiedName.create("com"), true, true); ImportNormalizer n2 = new ImportNormalizer(QualifiedName.create("de"), true, true); TestableImportScope scope = new TestableImportScope(newArrayList(n1,n2), IScope.NULLSCOPE, null, EcorePackage.Literals.EOBJECT, true); Iterable<IEObjectDescription> elements = scope.getAliasedElements(newArrayList); assertEquals(2,size(elements)); Iterator<IEObjectDescription> iterator = elements.iterator(); assertSame(desc1,((AliasedEObjectDescription)iterator.next()).getAliasedEObjectDescription()); assertSame(desc2,((AliasedEObjectDescription)iterator.next()).getAliasedEObjectDescription()); }
@Test public void testAllAliasedElements_01() throws Exception { final IEObjectDescription desc1 = new EObjectDescription(QualifiedName.create("com","foo"), EcorePackage.Literals.EANNOTATION, null); final IEObjectDescription desc2 = new EObjectDescription(QualifiedName.create("com","foo"), EcorePackage.Literals.EATTRIBUTE, null); final ArrayList<IEObjectDescription> newArrayList = newArrayList(desc1,desc2); ImportNormalizer n1 = new ImportNormalizer(QualifiedName.create("COM"), true, true); ImportNormalizer n2 = new ImportNormalizer(QualifiedName.create("DE"), true, true); TestableImportScope scope = new TestableImportScope(newArrayList(n1,n2), IScope.NULLSCOPE, null, EcorePackage.Literals.EOBJECT, true); Iterable<IEObjectDescription> elements = scope.getAliasedElements(newArrayList); assertEquals(2,size(elements)); Iterator<IEObjectDescription> iterator = elements.iterator(); assertSame(desc1,((AliasedEObjectDescription)iterator.next()).getAliasedEObjectDescription()); assertSame(desc2,((AliasedEObjectDescription)iterator.next()).getAliasedEObjectDescription()); }
@Test public void testAllAliasedElements_04() throws Exception { final IEObjectDescription desc1 = new EObjectDescription(QualifiedName.create("com","foo"), EcorePackage.Literals.EANNOTATION, null); final IEObjectDescription desc2 = new EObjectDescription(QualifiedName.create("com","bar"), EcorePackage.Literals.EANNOTATION, null); final IEObjectDescription desc3 = new EObjectDescription(QualifiedName.create("de","foo"), EcorePackage.Literals.EATTRIBUTE, null); final ArrayList<IEObjectDescription> newArrayList = newArrayList(desc1,desc2,desc3); ImportNormalizer n1 = new ImportNormalizer(QualifiedName.create("com"), true, true); ImportNormalizer n2 = new ImportNormalizer(QualifiedName.create("de"), true, true); TestableImportScope scope = new TestableImportScope(newArrayList(n1,n2), IScope.NULLSCOPE, null, EcorePackage.Literals.EOBJECT, true); Iterable<IEObjectDescription> elements = scope.getAliasedElements(newArrayList); assertEquals(1,size(elements)); assertSame(desc2,((AliasedEObjectDescription)elements.iterator().next()).getAliasedEObjectDescription()); }
@Test public void testAllAliasedElements_05() throws Exception { final IEObjectDescription desc1 = new EObjectDescription(QualifiedName.create("com","foo"), EcorePackage.Literals.EANNOTATION, null); final IEObjectDescription desc2 = new EObjectDescription(QualifiedName.create("com","bar"), EcorePackage.Literals.EANNOTATION, null); final IEObjectDescription desc3 = new EObjectDescription(QualifiedName.create("de","foo"), EcorePackage.Literals.EATTRIBUTE, null); final ArrayList<IEObjectDescription> newArrayList = newArrayList(desc1,desc2,desc3); ImportNormalizer n1 = new ImportNormalizer(QualifiedName.create("COM"), true, true); ImportNormalizer n2 = new ImportNormalizer(QualifiedName.create("DE"), true, true); TestableImportScope scope = new TestableImportScope(newArrayList(n1,n2), IScope.NULLSCOPE, null, EcorePackage.Literals.EOBJECT, true); Iterable<IEObjectDescription> elements = scope.getAliasedElements(newArrayList); assertEquals(1,size(elements)); assertSame(desc2,((AliasedEObjectDescription)elements.iterator().next()).getAliasedEObjectDescription()); }
private static IEObjectDescription createAliasedDescription(IEObjectDescription description) { if (!(description instanceof AliasedEObjectDescription) && description.getEObjectOrProxy() instanceof AliasedElement) { AliasedElement element = (AliasedElement) description.getEObjectOrProxy(); String alias = element.getAlias(); if (!Strings.isNullOrEmpty(alias)) { QualifiedName aliasedName = description.getQualifiedName().skipLast(1).append(alias); return new AliasedEObjectDescription(aliasedName, description); } } return description; }
public void completeResourceByName_Name(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { final boolean bRemoveUnnecessaryPrefixes = preferencesAccess.getPreferenceStore().getBoolean("prefixesOnlyAsNeeded"); Predicate<IEObjectDescription> filter = new Predicate<IEObjectDescription>() { @Override public boolean apply(IEObjectDescription input) { boolean result = bRemoveUnnecessaryPrefixes ^ !(input instanceof AliasedEObjectDescription); return result; } }; lookupCrossReference(model, SadlPackage.Literals.RESOURCE_BY_NAME__NAME, acceptor, filter, getProposalFactory(null, context)); }