Search in sources :

Example 6 with AliasedEObjectDescription

use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-core by eclipse.

the class ImportScope method getLocalElementsByName.

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

Example 7 with AliasedEObjectDescription

use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-eclipse by eclipse.

the class JavaTypeQuickfixes method getImportedTypesScope.

protected IScope getImportedTypesScope(EObject model, final String misspelled, final IScope actualScope, IJavaSearchScope scope) {
    if (scope == null) {
        return IScope.NULLSCOPE;
    }
    try {
        final Set<String> visiblePackages = importsConfiguration.getImplicitlyImportedPackages((XtextResource) model.eResource());
        final Set<String> importedTypes = Sets.newHashSet();
        final Set<String> seen = Sets.newHashSet();
        XImportSection importSection = importsConfiguration.getImportSection((XtextResource) model.eResource());
        if (importSection != null) {
            parseImportSection(importSection, new IAcceptor<String>() {

                @Override
                public void accept(String t) {
                    visiblePackages.add(t);
                }
            }, new IAcceptor<String>() {

                @Override
                public void accept(String t) {
                    importedTypes.add(t);
                }
            });
        }
        SearchEngine searchEngine = new SearchEngine();
        final List<IEObjectDescription> validProposals = Lists.newArrayList();
        for (String importedType : importedTypes) {
            if (validProposals.size() <= 5 && seen.add(importedType)) {
                int dot = importedType.lastIndexOf('.');
                if (dot != -1) {
                    importedType = importedType.substring(dot + 1);
                }
                if (isSimilarTypeName(misspelled, importedType)) {
                    QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(importedType);
                    for (IEObjectDescription element : actualScope.getElements(qualifiedName)) {
                        validProposals.add(new AliasedEObjectDescription(qualifiedName, element));
                        break;
                    }
                }
            }
        }
        try {
            for (String visiblePackage : visiblePackages) {
                if (validProposals.size() <= 5) {
                    searchEngine.searchAllTypeNames(visiblePackage.toCharArray(), SearchPattern.R_EXACT_MATCH, null, SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, scope, new TypeNameRequestor() {

                        @Override
                        public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
                            StringBuilder typeNameBuilder = new StringBuilder(simpleTypeName.length);
                            for (char[] enclosingType : enclosingTypeNames) {
                                typeNameBuilder.append(enclosingType);
                                typeNameBuilder.append('.');
                            }
                            typeNameBuilder.append(simpleTypeName);
                            String typeName = typeNameBuilder.toString();
                            if (isSimilarTypeName(misspelled, typeName)) {
                                String fqNameAsString = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName);
                                if (seen.add(fqNameAsString)) {
                                    QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(typeName);
                                    for (IEObjectDescription element : actualScope.getElements(qualifiedName)) {
                                        validProposals.add(new AliasedEObjectDescription(qualifiedName, element));
                                        break;
                                    }
                                }
                            }
                        }
                    }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor() {

                        @Override
                        public boolean isCanceled() {
                            return validProposals.size() > 5;
                        }
                    });
                }
            }
        } catch (OperationCanceledException exc) {
        // enough proposals
        }
        return new SimpleScope(validProposals);
    } catch (JavaModelException jme) {
        return IScope.NULLSCOPE;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) AliasedEObjectDescription(org.eclipse.xtext.resource.impl.AliasedEObjectDescription) JavaModelException(org.eclipse.jdt.core.JavaModelException) XImportSection(org.eclipse.xtext.xtype.XImportSection) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) QualifiedName(org.eclipse.xtext.naming.QualifiedName) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameRequestor(org.eclipse.jdt.core.search.TypeNameRequestor)

Example 8 with AliasedEObjectDescription

use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-core by eclipse.

the class ImportScopeTest method testAllAliasedElements_05.

@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());
}
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 9 with AliasedEObjectDescription

use of org.eclipse.xtext.resource.impl.AliasedEObjectDescription in project xtext-core by eclipse.

the class ImportScopeTest method testAllAliasedElements_00.

@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());
}
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)

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