org.eclipse.xtext.resource.impl.AliasedEObjectDescription Java Examples

The following examples show how to use org.eclipse.xtext.resource.impl.AliasedEObjectDescription. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeLiteralScope.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@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();
}
 
Example #2
Source File: ImportScope.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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();
}
 
Example #3
Source File: IEObjectDescriptionWithError.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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 <T extends IEObjectDescription> T unwrap(IEObjectDescription eObjectDescription, Class<T> type) {
	if (type.isInstance(eObjectDescription)) {
		return type.cast(eObjectDescription);
	}
	if (eObjectDescription instanceof AliasedEObjectDescription) {
		return unwrap(((AliasedEObjectDescription) eObjectDescription)
				.getAliasedEObjectDescription(), type);
	}
	if (eObjectDescription instanceof ForwardingEObjectDescription) {
		return unwrap(((ForwardingEObjectDescription) eObjectDescription).delegate(), type);
	}
	return null;
}
 
Example #4
Source File: ImportsAwareReferenceProposalCreator.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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 (inputNameSegmentCount > 1
			&& Arrays.contains(referencesSupportingImportedElements, reference))
		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)) {
		if (TExportableElement.class.isAssignableFrom(candidate.getEClass().getInstanceClass())) {
			if (N4JSResourceDescriptionStrategy.getExportDefault(candidate)) {
				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;
}
 
Example #5
Source File: ReferenceResolutionFinder.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private CandidateAccessType getAccessType() {
	if (isScopedCandidateEqual) {
		if (candidateViaScopeShortName instanceof PlainAccessOfAliasedImportDescription) {
			return CandidateAccessType.alias;
		}
		if (candidateViaScopeShortName instanceof PlainAccessOfNamespacedImportDescription) {
			return CandidateAccessType.namespace;
		}
	}
	if (candidate instanceof AliasedEObjectDescription) {
		return CandidateAccessType.alias;
	}
	return CandidateAccessType.direct;
}
 
Example #6
Source File: ReferenceResolutionFinder.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private String getAliasName() {
	if (accessType == CandidateAccessType.alias) {
		if (candidate instanceof AliasedEObjectDescription) {
			return candidate.getName().toString();
		}
		return ((PlainAccessOfAliasedImportDescription) candidateViaScopeShortName).getAlias();
	}
	return null;
}
 
Example #7
Source File: NonResolvingImportScope.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected Iterable<IEObjectDescription> getLocalElementsByName(QualifiedName name) {
	List<IEObjectDescription> result = newArrayList();
	QualifiedName resolvedQualifiedName = null;
	ISelectable importFrom = getImportFrom();
	for (ImportNormalizer normalizer : myNormalizers) {
		final QualifiedName resolvedName = normalizer.resolve(name);
		if (resolvedName != null) {
			Iterable<IEObjectDescription> resolvedElements = importFrom.getExportedObjects(myType, resolvedName,
					isIgnoreCase());
			for (IEObjectDescription resolvedElement : resolvedElements) {
				if (resolvedQualifiedName == null)
					resolvedQualifiedName = resolvedName;
				else if (!resolvedQualifiedName.equals(resolvedName)) {
					// change is here
					if (result.get(0).getEObjectURI().equals(resolvedElement.getEObjectURI())) {
						return emptyList();
					}
					// change is above
				}
				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;
}
 
Example #8
Source File: ImportScope.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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;
}
 
Example #9
Source File: ImportScopeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #10
Source File: ImportScopeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #11
Source File: ImportScopeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #12
Source File: ImportScopeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}