@NotNull @Override public PsiClass[] findClassByShortName( @NotNull String name, @NotNull final GlobalSearchScope scope) { PsiClass[] allClasses = getCachedClassesByName(name); if (allClasses.length == 0) return allClasses; if (allClasses.length == 1) { return PsiSearchScopeUtil.isInScope(scope, allClasses[0]) ? allClasses : PsiClass.EMPTY_ARRAY; } PsiClass[] array = ContainerUtil.findAllAsArray( allClasses, new Condition<PsiClass>() { @Override public boolean value(PsiClass aClass) { return PsiSearchScopeUtil.isInScope(scope, aClass); } }); Arrays.sort( array, new Comparator<PsiClass>() { @Override public int compare(PsiClass o1, PsiClass o2) { VirtualFile file1 = o1.getContainingFile().getVirtualFile(); VirtualFile file2 = o2.getContainingFile().getVirtualFile(); if (file1 == null) return file2 == null ? 0 : -1; if (file2 == null) return 1; return scope.compare(file2, file1); } }); return array; }
private static List<LanguageDefinition> collectLanguageDefinitions(final ConvertContext context) { final Project project = context.getProject(); final Collection<PsiClass> allLanguages = CachedValuesManager.getManager(project) .getCachedValue( project, () -> { final PsiClass languageClass = JavaPsiFacade.getInstance(project) .findClass(Language.class.getName(), GlobalSearchScope.allScope(project)); if (languageClass == null) { return Result.create( Collections.emptyList(), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); } final GlobalSearchScope projectProductionScope = GlobalSearchScopesCore.projectProductionScope(project); GlobalSearchScope allScope = projectProductionScope.union(ProjectScope.getLibrariesScope(project)); final Collection<PsiClass> allInheritors = ClassInheritorsSearch.search(languageClass, allScope, true).findAll(); return Result.create( allInheritors, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); }); if (allLanguages.isEmpty()) { return Collections.emptyList(); } final List<LanguageDefinition> libraryDefinitions = collectLibraryLanguages(context, allLanguages); final GlobalSearchScope projectProductionScope = GlobalSearchScopesCore.projectProductionScope(project); final Collection<PsiClass> projectLanguages = ContainerUtil.filter( allLanguages, aClass -> PsiSearchScopeUtil.isInScope(projectProductionScope, aClass)); final List<LanguageDefinition> projectDefinitions = collectProjectLanguages(projectLanguages, libraryDefinitions); final List<LanguageDefinition> all = ContainerUtil.newArrayList(libraryDefinitions); all.addAll(projectDefinitions); return all; }