Search in sources :

Example 1 with TypeNameRequestor

use of org.eclipse.jdt.core.search.TypeNameRequestor in project xtext-eclipse by eclipse.

the class JdtBasedSimpleTypeScope method internalGetAllElements.

@Override
protected Iterable<IEObjectDescription> internalGetAllElements() {
    IJavaProject javaProject = getTypeProvider().getJavaProject();
    if (javaProject == null)
        return Collections.emptyList();
    final List<IEObjectDescription> allScopedElements = Lists.newArrayListWithExpectedSize(25000);
    try {
        IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });
        for (Class<?> clazz : Primitives.ALL_PRIMITIVE_TYPES) {
            IEObjectDescription primitive = createScopedElement(clazz.getName());
            if (primitive != null)
                allScopedElements.add(primitive);
        }
        TypeNameRequestor nameMatchRequestor = new TypeNameRequestor() {

            @Override
            public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
                StringBuilder fqName = new StringBuilder(packageName.length + simpleTypeName.length + 1);
                if (packageName.length != 0) {
                    fqName.append(packageName);
                    fqName.append('.');
                }
                for (char[] enclosingType : enclosingTypeNames) {
                    fqName.append(enclosingType);
                    fqName.append('.');
                }
                fqName.append(simpleTypeName);
                String fullyQualifiedName = fqName.toString();
                InternalEObject proxy = createProxy(fullyQualifiedName);
                Map<String, String> userData = null;
                if (enclosingTypeNames.length == 0) {
                    userData = ImmutableMap.of("flags", String.valueOf(modifiers));
                } else {
                    userData = ImmutableMap.of("flags", String.valueOf(modifiers), "inner", "true");
                }
                IEObjectDescription eObjectDescription = EObjectDescription.create(getQualifiedNameConverter().toQualifiedName(fullyQualifiedName), proxy, userData);
                if (eObjectDescription != null)
                    allScopedElements.add(eObjectDescription);
            }
        };
        collectContents(searchScope, nameMatchRequestor);
    } catch (JavaModelException e) {
    // ignore
    }
    return allScopedElements;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) TypeNameRequestor(org.eclipse.jdt.core.search.TypeNameRequestor) InternalEObject(org.eclipse.emf.ecore.InternalEObject) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 2 with TypeNameRequestor

use of org.eclipse.jdt.core.search.TypeNameRequestor 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)

Aggregations

JavaModelException (org.eclipse.jdt.core.JavaModelException)2 TypeNameRequestor (org.eclipse.jdt.core.search.TypeNameRequestor)2 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)1 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 AliasedEObjectDescription (org.eclipse.xtext.resource.impl.AliasedEObjectDescription)1 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)1 XImportSection (org.eclipse.xtext.xtype.XImportSection)1