private static boolean processAllClassesInGlobalScope(@NotNull final GlobalSearchScope scope, @NotNull final AllClassesSearch.SearchParameters parameters, @NotNull Processor<PsiClass> processor) { final Set<String> names = new THashSet<String>(10000); processClassNames(parameters.getProject(), scope, new Consumer<String>() { @Override public void consume(String s) { if (parameters.nameMatches(s)) { names.add(s); } } }); List<String> sorted = new ArrayList<String>(names); Collections.sort(sorted, String.CASE_INSENSITIVE_ORDER); return processClassesByNames(parameters.getProject(), scope, sorted, processor); }
@Override public boolean execute(@NotNull final AllClassesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiClass> consumer) { SearchScope scope = queryParameters.getScope(); if(scope instanceof GlobalSearchScope) { return processAllClassesInGlobalScope((GlobalSearchScope) scope, queryParameters, consumer); } PsiElement[] scopeRoots = ((LocalSearchScope) scope).getScope(); for(final PsiElement scopeRoot : scopeRoots) { if(!processScopeRootForAllClasses(scopeRoot, consumer)) { return false; } } return true; }
private static boolean processAllClassesInGlobalScope(@NotNull final GlobalSearchScope scope, @NotNull final AllClassesSearch.SearchParameters parameters, @NotNull Processor<PsiClass> processor) { final Set<String> names = new THashSet<String>(10000); processClassNames(parameters.getProject(), scope, new Consumer<String>() { @Override public void consume(String s) { if(parameters.nameMatches(s)) { names.add(s); } } }); List<String> sorted = new ArrayList<String>(names); Collections.sort(sorted, String.CASE_INSENSITIVE_ORDER); return processClassesByNames(parameters.getProject(), scope, sorted, processor); }
private void refreshSketchSelector(Module [] moduleSearchScope) { ApplicationManager.getApplication().runWriteAction(() -> { Query<PsiClass> classQuery = AllClassesSearch.search(ProcessingPluginUtil.INSTANCE.sketchesInModuleScope(moduleSearchScope), project); Collection<PsiClass> classesInModule = classQuery.findAll(); for (PsiClass classInModule : classesInModule) { if (SketchClassFilter.isSketchClass(classInModule)) { sketchSelector.addItem(new SketchSelectorComboItem(classInModule)); } } }); }
@Override public boolean execute(@NotNull final AllClassesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiClass> consumer) { SearchScope scope = queryParameters.getScope(); if (scope instanceof GlobalSearchScope) { return processAllClassesInGlobalScope((GlobalSearchScope)scope, queryParameters, consumer); } PsiElement[] scopeRoots = ((LocalSearchScope)scope).getScope(); for (final PsiElement scopeRoot : scopeRoots) { if (!processScopeRootForAllClasses(scopeRoot, consumer)) return false; } return true; }
@Nullable public static PsiClass[] getAllTestClasses(final TestClassFilter filter, boolean sync) { final PsiClass[][] holder = new PsiClass[1][]; final Runnable process = new Runnable() { public void run() { final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator(); final Collection<PsiClass> set = new LinkedHashSet<PsiClass>(); final PsiManager manager = PsiManager.getInstance(filter.getProject()); final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject()); final GlobalSearchScope scope = projectScope.intersectWith(filter.getScope()); ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { for (final PsiClass psiClass : AllClassesSearch.search(scope, manager.getProject())) { if (filter.isAccepted(psiClass)) { if (indicator != null) { indicator.setText2("Found test class " + psiClass.getQualifiedName()); } set.add(psiClass); } } } }); holder[0] = set.toArray(new PsiClass[set.size()]); } }; if (sync) { ProgressManager.getInstance().runProcessWithProgressSynchronously(process, "Searching For Tests...", true, filter.getProject()); } else { process.run(); } return holder[0]; }
@NotNull private List<PsiClass> extractDagger2ModulesPsiClasses(AnActionEvent e) { DaggerModuleClassFilter filter = new DaggerModuleClassFilter(); final Module module = LangDataKeys.MODULE.getData(e.getDataContext()); SearchScope scope = GlobalSearchScope.moduleScope(module); Query<PsiClass> search = AllClassesSearch.search(scope, e.getProject()); List<PsiClass> modulesClasses = new ArrayList<>(); for (PsiClass psiClass : search.findAll()) { if (filter.isAccepted(psiClass)) { modulesClasses.add(psiClass); } } return modulesClasses; }
@NotNull @Override public Object[] getVariants() { // all classes of current platform with same type closure are eligible final Project project = getElement().getProject(); final DefracFacet facet = DefracFacet.getInstance(getElement()); if(facet == null) { return NO_VARIANTS; } final GlobalSearchScope scope = facet.getMultiPlatformClassSearchScope(platform); final PsiJavaFile file = (PsiJavaFile)getElement().getContainingFile(); if(file == null) { return NO_VARIANTS; } final PsiClass enclosingClass = getParentOfType(getElement(), PsiClass.class, false); if(enclosingClass == null) { return NO_VARIANTS; } final Query<PsiClass> query = AllClassesSearch.search(scope, project); return variantsViaQuery(query, enclosingClass); }
@Nullable public static PsiClass[] getAllTestClasses(final TestClassFilter filter, boolean sync) { final PsiClass[][] holder = new PsiClass[1][]; final Runnable process = new Runnable() { public void run() { final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator(); final Collection<PsiClass> set = new HashSet<PsiClass>(); PsiManager manager = PsiManager.getInstance(filter.getProject()); GlobalSearchScope scope = filter.getScope(); GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject()); scope = projectScope.intersectWith(scope); for (final PsiClass psiClass : AllClassesSearch.search(scope, manager.getProject())) { ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { if (filter.isAccepted(psiClass)) { indicator.setText2("Found test class " + psiClass.getQualifiedName()); set.add(psiClass); } } }); } holder[0] = set.toArray(new PsiClass[set.size()]); } }; if (sync) { ProgressManager.getInstance().runProcessWithProgressSynchronously(process, "Searching For Tests...", true, filter.getProject()); } else { process.run(); } return holder[0]; }
@NotNull public static PsiElement[] buildEntryPoints(@NotNull Module module) { Query<PsiClass> search = AllClassesSearch.search(GlobalSearchScope.moduleScope(module), module.getProject()); final List<PsiElement> list = new ArrayList<PsiElement>(); search.forEach(psiClass -> { if(PsiMethodUtil.hasMainMethod(psiClass)) { list.add(new IkvmJavaClassAsDotNetTypeElement(psiClass)); } return true; }); return ContainerUtil.toArray(list, PsiElement.ARRAY_FACTORY); }
private static void processInheritors(@NotNull final Processor<PsiClass> consumer, @NotNull final PsiClass baseClass, @NotNull final SearchScope searchScope, @NotNull final ClassInheritorsSearch.SearchParameters parameters) { if (baseClass instanceof PsiAnonymousClass || isFinal(baseClass)) return; Project project = PsiUtilCore.getProjectInReadAction(baseClass); if (isJavaLangObject(baseClass)) { AllClassesSearch.search(searchScope, project, parameters.getNameCondition()).forEach(new Processor<PsiClass>() { @Override public boolean process(final PsiClass aClass) { ProgressManager.checkCanceled(); return isJavaLangObject(aClass) || consumer.process(aClass); } }); return; } final Ref<PsiClass> currentBase = Ref.create(null); final Stack<PsiAnchor> stack = new Stack<PsiAnchor>(); final Set<PsiAnchor> processed = ContainerUtil.newTroveSet(); final Processor<PsiClass> processor = new ReadActionProcessor<PsiClass>() { @Override public boolean processInReadAction(PsiClass candidate) { ProgressManager.checkCanceled(); if (parameters.isCheckInheritance() || parameters.isCheckDeep() && !(candidate instanceof PsiAnonymousClass)) { if (!candidate.isInheritor(currentBase.get(), false)) { return true; } } if (PsiSearchScopeUtil.isInScope(searchScope, candidate)) { if (candidate instanceof PsiAnonymousClass) { return consumer.process(candidate); } final String name = candidate.getName(); if (name != null && parameters.getNameCondition().value(name) && !consumer.process(candidate)) { return false; } } if (parameters.isCheckDeep() && !(candidate instanceof PsiAnonymousClass) && !isFinal(candidate)) { stack.push(PsiAnchor.create(candidate)); } return true; } }; ApplicationManager.getApplication().runReadAction(new Runnable() { @Override public void run() { stack.push(PsiAnchor.create(baseClass)); } }); final GlobalSearchScope projectScope = GlobalSearchScope.allScope(project); while (!stack.isEmpty()) { ProgressManager.checkCanceled(); final PsiAnchor anchor = stack.pop(); if (!processed.add(anchor)) continue; PsiClass psiClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() { @Override public PsiClass compute() { return (PsiClass)anchor.retrieve(); } }); if (psiClass == null) continue; currentBase.set(psiClass); if (!DirectClassInheritorsSearch.search(psiClass, projectScope, parameters.isIncludeAnonymous(), false).forEach(processor)) return; } }
@NotNull @Override public Object[] getVariants() { // all classes of current platform with same type closure are eligible final Project project = getElement().getProject(); final DefracFacet facet = DefracFacet.getInstance(getElement()); if(facet == null) { return NO_VARIANTS; } final GlobalSearchScope scope = facet.getMultiPlatformClassSearchScope(platform); final PsiJavaFile file = (PsiJavaFile)getElement().getContainingFile(); if(file == null) { return NO_VARIANTS; } final PsiClass enclosingClass = getParentOfType(getElement(), PsiClass.class, false); if(enclosingClass == null) { return NO_VARIANTS; } final PsiClassType[] extendsTypes = enclosingClass.getExtendsListTypes(); final PsiClassType[] implementsTypes = enclosingClass.getImplementsListTypes(); final PsiClassType[] baseTypes = ArrayUtil.mergeArrays(extendsTypes, implementsTypes); final Query<PsiClass> query; if(baseTypes.length == 0) { // There is no filter (no bases or interfaces the injection must implement) // so we offer the user everything we got. query = AllClassesSearch.search(scope, project); } else { PsiClass base = null; for(final PsiClassType baseType : baseTypes) { final PsiClass resolved = baseType.resolve(); if(resolved != null) { base = resolved; break; } } if(base == null) { return NO_VARIANTS; } query = ClassInheritorsSearch.search(base, scope, true, true); } return variantsViaQuery(query, enclosingClass, extendsTypes.length == 0 ? null : extendsTypes, implementsTypes.length == 0 ? null : implementsTypes); }