Search in sources :

Example 1 with TailTypeDecorator

use of com.intellij.codeInsight.lookup.TailTypeDecorator in project android by JetBrains.

the class DataBindingCompletionUtil method fillClassNames.

private static void fillClassNames(@NotNull CompletionResultSet resultSet, @NotNull String packagePrefix, @NotNull Module module) {
    final Project project = module.getProject();
    JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
    PsiPackage basePackage = javaPsiFacade.findPackage(packagePrefix);
    if (basePackage == null) {
        PsiClass aClass = javaPsiFacade.findClass(packagePrefix, module.getModuleWithDependenciesAndLibrariesScope(false));
        if (aClass != null) {
            PsiClass[] innerClasses = aClass.getInnerClasses();
            for (PsiClass innerClass : innerClasses) {
                resultSet.addElement(new JavaPsiClassReferenceElement(innerClass));
            }
        }
    // TODO: add completions for java.lang classes
    } else {
        GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false);
        PsiPackage[] subPackages = basePackage.getSubPackages(scope);
        for (PsiPackage pkg : subPackages) {
            // many res folders also show up as package suggestions - eg. drawable-hdpi, which is clearly not a package.
            if (pkg.getSubPackages(scope).length > 0 || pkg.getClasses(scope).length > 0) {
                // For some reason, we see some invalid packages here - eg. META-INF. Filter them out.
                String name = pkg.getName();
                boolean invalidPkg = false;
                // can only be null for default package, which this is not, as it's a subpackage.
                assert name != null;
                for (int i = 0; i < name.length(); i++) {
                    if (!Character.isJavaIdentifierPart(name.charAt(i))) {
                        invalidPkg = true;
                        break;
                    }
                }
                if (invalidPkg) {
                    // skip adding this package.
                    continue;
                }
                LookupElement element = new TailTypeDecorator<LookupElement>(LookupElementBuilder.createWithIcon(pkg)) {

                    @Nullable
                    @Override
                    protected TailType computeTailType(InsertionContext context) {
                        return TailType.DOT;
                    }

                    @Override
                    public void handleInsert(InsertionContext context) {
                        super.handleInsert(context);
                        AutoPopupController.getInstance(project).scheduleAutoPopup(context.getEditor());
                    }
                };
                resultSet.addElement(element);
            }
        }
        for (PsiClass psiClass : basePackage.getClasses(scope)) {
            resultSet.addElement(new JavaPsiClassReferenceElement(psiClass));
        }
    }
}
Also used : TailTypeDecorator(com.intellij.codeInsight.lookup.TailTypeDecorator) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 TailTypeDecorator (com.intellij.codeInsight.lookup.TailTypeDecorator)1 Project (com.intellij.openapi.project.Project)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1