use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project n4js by eclipse.
the class ImportsAwareReferenceProposalCreator method getAliasedDescription.
/**
* 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);
// 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;
}
use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-xtend by eclipse.
the class NestedTypesScope method doGetSingleElement.
@Override
protected IEObjectDescription doGetSingleElement(QualifiedName name, String firstSegment, int dollarIndex) {
JvmDeclaredType declaredType = innermost;
while (declaredType != null) {
IEObjectDescription result = doGetSingleElement(declaredType, name, firstSegment, dollarIndex);
if (result != null) {
return result;
}
declaredType = EcoreUtil2.getContainerOfType(declaredType.eContainer(), JvmDeclaredType.class);
}
if (dollarIndex > 0 && name.getSegmentCount() == 1) {
QualifiedName splitted = QualifiedName.create(Strings.split(name.getFirstSegment(), '$'));
IEObjectDescription result = doGetSingleElement(splitted, splitted.getFirstSegment(), -1);
if (result != null) {
return new AliasedEObjectDescription(name, result);
}
}
return null;
}
use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-core by eclipse.
the class ImportScopeTest method testAllAliasedElements_04.
@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());
}
use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-core by eclipse.
the class ImportScopeTest method testAllAliasedElements_01.
@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());
}
use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-core by eclipse.
the class ImportScope method getAliasedElements.
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();
}
Aggregations