Search in sources :

Example 1 with AliasedEObjectDescription

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;
}
Also used : AliasedEObjectDescription(org.eclipse.xtext.resource.impl.AliasedEObjectDescription) TExportableElement(org.eclipse.n4js.ts.types.TExportableElement) QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObject(org.eclipse.emf.ecore.EObject)

Example 2 with AliasedEObjectDescription

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;
}
Also used : AliasedEObjectDescription(org.eclipse.xtext.resource.impl.AliasedEObjectDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 3 with AliasedEObjectDescription

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());
}
Also used : AliasedEObjectDescription(org.eclipse.xtext.resource.impl.AliasedEObjectDescription) EObjectDescription(org.eclipse.xtext.resource.EObjectDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 4 with AliasedEObjectDescription

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());
}
Also used : AliasedEObjectDescription(org.eclipse.xtext.resource.impl.AliasedEObjectDescription) EObjectDescription(org.eclipse.xtext.resource.EObjectDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 5 with AliasedEObjectDescription

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();
}
Also used : AliasedEObjectDescription(org.eclipse.xtext.resource.impl.AliasedEObjectDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

AliasedEObjectDescription (org.eclipse.xtext.resource.impl.AliasedEObjectDescription)9 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)8 QualifiedName (org.eclipse.xtext.naming.QualifiedName)5 EObjectDescription (org.eclipse.xtext.resource.EObjectDescription)4 Test (org.junit.Test)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 EObject (org.eclipse.emf.ecore.EObject)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)1 TypeNameRequestor (org.eclipse.jdt.core.search.TypeNameRequestor)1 TExportableElement (org.eclipse.n4js.ts.types.TExportableElement)1 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)1 ISelectable (org.eclipse.xtext.resource.ISelectable)1 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)1 XImportSection (org.eclipse.xtext.xtype.XImportSection)1