Search in sources :

Example 1 with ImportNormalizer

use of org.eclipse.xtext.scoping.impl.ImportNormalizer in project xtext-xtend by eclipse.

the class TypeScopeWithWildcardImports method doGetElements.

@Override
protected void doGetElements(JvmType type, List<IEObjectDescription> result) {
    if (!(type instanceof JvmDeclaredType)) {
        return;
    }
    JvmDeclaredType declaredType = (JvmDeclaredType) type;
    String packageName = declaredType.getPackageName();
    if (!Strings.isEmpty(packageName)) {
        QualifiedName qualifiedPackageName = QualifiedName.create(Strings.split(packageName, '.'));
        QualifiedName withDot = null;
        String withDollar = null;
        for (int i = 0; i < imports.length; i++) {
            ImportNormalizer[] chunk = imports[i];
            for (int j = 0; j < chunk.length; j++) {
                ImportNormalizer normalizer = chunk[j];
                QualifiedName namespacePrefix = normalizer.getImportedNamespacePrefix();
                if (namespacePrefix.equals(qualifiedPackageName)) {
                    if (withDot == null) {
                        withDot = QualifiedName.create(Strings.split(type.getQualifiedName('.'), '.'));
                        withDollar = type.eContainer() instanceof JvmType ? type.getQualifiedName('$').substring(packageName.length() + 1) : null;
                    }
                    result.add(EObjectDescription.create(withDot.skipFirst(qualifiedPackageName.getSegmentCount()), type));
                    if (withDollar != null) {
                        result.add(EObjectDescription.create(withDollar, type));
                    }
                }
            }
        }
    }
    if (parent != null) {
        parent.doGetElements(type, result);
    } else {
        Iterables.addAll(result, typeScope.getElements(type));
    }
}
Also used : ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 2 with ImportNormalizer

use of org.eclipse.xtext.scoping.impl.ImportNormalizer in project xtext-xtend by eclipse.

the class XtendImportedNamespaceScopeProvider method getImportScope.

private AbstractScope getImportScope(XImportSection importSection, AbstractScope parent, RecordingTypeScope typeScope) {
    if (importSection == null)
        return parent;
    List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
    if (importDeclarations.isEmpty()) {
        return parent;
    }
    List<ImportNormalizer> wildcardImports = null;
    List<JvmType> concreteImports = null;
    List<QualifiedName> importedNames = null;
    boolean hasLegacyImport = false;
    for (XImportDeclaration importDeclaration : importDeclarations) {
        if (!importDeclaration.isStatic()) {
            if (importDeclaration.isWildcard()) {
                if (wildcardImports == null) {
                    wildcardImports = Lists.newArrayListWithCapacity(4);
                }
                String importedNamespace = importDeclaration.getImportedNamespace();
                importedNamespace = importedNamespace.substring(0, importedNamespace.length() - 2);
                QualifiedName qualifiedImportedNamespace = qualifiedNameConverter.toQualifiedName(importedNamespace);
                wildcardImports.add(AbstractNestedTypeAwareImportNormalizer.createNestedTypeAwareImportNormalizer(qualifiedImportedNamespace, true, false));
            } else {
                JvmDeclaredType importedType = importDeclaration.getImportedType();
                if (importedType != null && !importedType.eIsProxy()) {
                    if (concreteImports == null || importedNames == null) /* to make JDT happy */
                    {
                        concreteImports = Lists.newArrayListWithCapacity(10);
                        importedNames = Lists.newArrayListWithCapacity(10);
                    }
                    concreteImports.add(importedType);
                    if (importedType.eContainer() instanceof JvmDeclaredType) {
                        String importSyntax = getImportsConfiguration().getLegacyImportSyntax(importDeclaration);
                        if (importSyntax != null) {
                            hasLegacyImport = true;
                            importedNames.add(getQualifiedNameConverter().toQualifiedName(importSyntax));
                        } else
                            importedNames.add(null);
                    } else {
                        importedNames.add(null);
                    }
                }
            }
        }
    }
    return getImportScope(wildcardImports, concreteImports, hasLegacyImport ? importedNames : null, parent, typeScope);
}
Also used : AbstractNestedTypeAwareImportNormalizer(org.eclipse.xtext.xbase.scoping.AbstractNestedTypeAwareImportNormalizer) ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration)

Example 3 with ImportNormalizer

use of org.eclipse.xtext.scoping.impl.ImportNormalizer in project xtext-core by eclipse.

the class FileAwareTestLanguageImportScopeProvider method internalGetImportedNamespaceResolvers.

@Override
protected List<ImportNormalizer> internalGetImportedNamespaceResolvers(final EObject context, final boolean ignoreCase) {
    final List<ImportNormalizer> resolvers = super.internalGetImportedNamespaceResolvers(context, ignoreCase);
    if ((context instanceof PackageDeclaration)) {
        QualifiedName _qualifiedName = this.getQualifiedNameConverter().toQualifiedName(((PackageDeclaration) context).getName());
        ImportNormalizer _importNormalizer = new ImportNormalizer(_qualifiedName, true, false);
        resolvers.add(_importNormalizer);
        EList<Import> _imports = ((PackageDeclaration) context).getImports();
        for (final Import imp : _imports) {
            {
                final QualifiedName name = this.getImportedNamespace(imp);
                ImportNormalizer _importNormalizer_1 = new ImportNormalizer(name, false, false);
                resolvers.add(_importNormalizer_1);
            }
        }
    }
    return resolvers;
}
Also used : Import(org.eclipse.xtext.testlanguages.fileAware.fileAware.Import) ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) QualifiedName(org.eclipse.xtext.naming.QualifiedName) PackageDeclaration(org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration)

Example 4 with ImportNormalizer

use of org.eclipse.xtext.scoping.impl.ImportNormalizer in project xtext-xtend by eclipse.

the class TypeScopeWithWildcardImports method getSingleElement.

@Override
public IEObjectDescription getSingleElement(QualifiedName name) {
    for (int i = 0; i < imports.length; i++) {
        ImportNormalizer[] chunk = imports[i];
        IEObjectDescription result = null;
        for (int j = 0; j < chunk.length; j++) {
            ImportNormalizer normalizer = chunk[j];
            QualifiedName resolvedName = normalizer.resolve(name);
            if (resolvedName != null) {
                IEObjectDescription candidate = typeScope.getSingleElement(resolvedName, true);
                if (candidate != null) {
                    if (result == null) {
                        result = candidate;
                    } else {
                        return null;
                    }
                }
            }
        }
        if (result != null) {
            return result;
        }
    }
    return getSingleElementFromParent(name);
}
Also used : ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 5 with ImportNormalizer

use of org.eclipse.xtext.scoping.impl.ImportNormalizer in project xtext-core by eclipse.

the class QualifiedNameTest method testBug354473.

@Test
public void testBug354473() {
    DefaultImpl impl = new IQualifiedNameConverter.DefaultImpl();
    QualifiedName name = impl.toQualifiedName(".");
    ImportNormalizer normalizer = new ImportNormalizer(QualifiedName.create("Test"), true, false);
    assertNull(normalizer.resolve(name));
}
Also used : ImportNormalizer(org.eclipse.xtext.scoping.impl.ImportNormalizer) DefaultImpl(org.eclipse.xtext.naming.IQualifiedNameConverter.DefaultImpl) Test(org.junit.Test)

Aggregations

ImportNormalizer (org.eclipse.xtext.scoping.impl.ImportNormalizer)5 QualifiedName (org.eclipse.xtext.naming.QualifiedName)4 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 JvmType (org.eclipse.xtext.common.types.JvmType)2 DefaultImpl (org.eclipse.xtext.naming.IQualifiedNameConverter.DefaultImpl)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 Import (org.eclipse.xtext.testlanguages.fileAware.fileAware.Import)1 PackageDeclaration (org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration)1 AbstractNestedTypeAwareImportNormalizer (org.eclipse.xtext.xbase.scoping.AbstractNestedTypeAwareImportNormalizer)1 XImportDeclaration (org.eclipse.xtext.xtype.XImportDeclaration)1 Test (org.junit.Test)1