Search in sources :

Example 56 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class ConfigurationUtil method findAllTestClasses.

// return true if there is JUnit4 test
public static boolean findAllTestClasses(final TestClassFilter testClassFilter, @Nullable final Module module, final Set<PsiClass> found) {
    final PsiManager manager = testClassFilter.getPsiManager();
    final Project project = manager.getProject();
    GlobalSearchScope projectScopeWithoutLibraries = GlobalSearchScope.projectScope(project);
    final GlobalSearchScope scope = projectScopeWithoutLibraries.intersectWith(testClassFilter.getScope());
    final PsiClass base = testClassFilter.getBase();
    if (base != null) {
        ClassInheritorsSearch.search(base, scope, true, true, false).forEach(new ReadActionProcessor<PsiClass>() {

            @Override
            public boolean processInReadAction(PsiClass aClass) {
                if (testClassFilter.isAccepted(aClass))
                    found.add(aClass);
                return true;
            }
        });
    }
    // classes having suite() method
    final PsiMethod[] suiteMethods = ApplicationManager.getApplication().runReadAction(new Computable<PsiMethod[]>() {

        public PsiMethod[] compute() {
            return PsiShortNamesCache.getInstance(project).getMethodsByName(JUnitUtil.SUITE_METHOD_NAME, scope);
        }
    });
    for (final PsiMethod method : suiteMethods) {
        ApplicationManager.getApplication().runReadAction(() -> {
            final PsiClass containingClass = method.getContainingClass();
            if (containingClass == null)
                return;
            if (containingClass instanceof PsiAnonymousClass)
                return;
            if (containingClass.hasModifierProperty(PsiModifier.ABSTRACT))
                return;
            if (containingClass.getContainingClass() != null && !containingClass.hasModifierProperty(PsiModifier.STATIC))
                return;
            if (JUnitUtil.isSuiteMethod(method) && testClassFilter.isAccepted(containingClass)) {
                found.add(containingClass);
            }
        });
    }
    Set<PsiClass> processed = ContainerUtil.newHashSet();
    boolean hasJunit4 = addAnnotatedMethodsAnSubclasses(scope, testClassFilter, module, found, processed, JUnitUtil.TEST_ANNOTATION, manager.getProject());
    hasJunit4 |= addAnnotatedMethodsAnSubclasses(scope, testClassFilter, module, found, processed, JUnitUtil.RUN_WITH, manager.getProject());
    return hasJunit4;
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 57 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class DuplicatePropertyInspection method checkFile.

private void checkFile(final PsiFile file, final InspectionManager manager, GlobalInspectionContextBase context, final RefManager refManager, final ProblemDescriptionsProcessor processor) {
    if (!(file instanceof PropertiesFile))
        return;
    if (!context.isToCheckFile(file, this) || SuppressionUtil.inspectionResultSuppressed(file, this))
        return;
    final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(file.getProject());
    final PropertiesFile propertiesFile = (PropertiesFile) file;
    final List<IProperty> properties = propertiesFile.getProperties();
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    if (module == null)
        return;
    final GlobalSearchScope scope = CURRENT_FILE ? GlobalSearchScope.fileScope(file) : MODULE_WITH_DEPENDENCIES ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.projectScope(file.getProject());
    final Map<String, Set<PsiFile>> processedValueToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
    final Map<String, Set<PsiFile>> processedKeyToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
    final ProgressIndicator original = ProgressManager.getInstance().getProgressIndicator();
    final ProgressIndicator progress = ProgressWrapper.wrap(original);
    ProgressManager.getInstance().runProcess(() -> {
        if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(properties, progress, false, property -> {
            if (original != null) {
                if (original.isCanceled())
                    return false;
                original.setText2(PropertiesBundle.message("searching.for.property.key.progress.text", property.getUnescapedKey()));
            }
            processTextUsages(processedValueToFiles, property.getValue(), processedKeyToFiles, searchHelper, scope);
            processTextUsages(processedKeyToFiles, property.getUnescapedKey(), processedValueToFiles, searchHelper, scope);
            return true;
        }))
            throw new ProcessCanceledException();
        List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
        Map<String, Set<String>> keyToDifferentValues = new HashMap<>();
        if (CHECK_DUPLICATE_KEYS || CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
            prepareDuplicateKeysByFile(processedKeyToFiles, manager, keyToDifferentValues, problemDescriptors, file, original);
        }
        if (CHECK_DUPLICATE_VALUES)
            prepareDuplicateValuesByFile(processedValueToFiles, manager, problemDescriptors, file, original);
        if (CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
            processDuplicateKeysWithDifferentValues(keyToDifferentValues, processedKeyToFiles, problemDescriptors, manager, file, original);
        }
        if (!problemDescriptors.isEmpty()) {
            processor.addProblemElement(refManager.getReference(file), problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]));
        }
    }, progress);
}
Also used : java.util(java.util) ActionListener(java.awt.event.ActionListener) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) JobLauncher(com.intellij.concurrency.JobLauncher) VirtualFile(com.intellij.openapi.vfs.VirtualFile) URL(java.net.URL) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) LowLevelSearchUtil(com.intellij.psi.impl.search.LowLevelSearchUtil) StringSearcher(com.intellij.util.text.StringSearcher) RefManager(com.intellij.codeInspection.reference.RefManager) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Comparing(com.intellij.openapi.util.Comparing) ProgressWrapper(com.intellij.openapi.progress.util.ProgressWrapper) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ProgressManager(com.intellij.openapi.progress.ProgressManager) Property(com.intellij.lang.properties.psi.Property) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) PropertiesBundle(com.intellij.lang.properties.PropertiesBundle) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) MalformedURLException(java.net.MalformedURLException) StringUtil(com.intellij.openapi.util.text.StringUtil) Processors(com.intellij.util.Processors) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) com.intellij.codeInspection(com.intellij.codeInspection) ActionEvent(java.awt.event.ActionEvent) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IProperty(com.intellij.lang.properties.IProperty) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase) NotNull(org.jetbrains.annotations.NotNull) CharArrayUtil(com.intellij.util.text.CharArrayUtil) javax.swing(javax.swing) THashSet(gnu.trove.THashSet) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) IProperty(com.intellij.lang.properties.IProperty) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Module(com.intellij.openapi.module.Module) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 58 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class CreateClassToBindFix method createBoundFields.

private void createBoundFields(final PsiClass formClass) throws IncorrectOperationException {
    final Module module = myEditor.getRootContainer().getModule();
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
    final PsiManager psiManager = PsiManager.getInstance(myEditor.getProject());
    final Ref<IncorrectOperationException> exception = new Ref<>();
    FormEditingUtil.iterate(myEditor.getRootContainer(), new FormEditingUtil.ComponentVisitor() {

        public boolean visit(final IComponent component) {
            if (component.getBinding() != null) {
                final PsiClass fieldClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope);
                if (fieldClass != null) {
                    PsiType fieldType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(fieldClass);
                    try {
                        PsiField field = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createField(component.getBinding(), fieldType);
                        formClass.add(field);
                    } catch (IncorrectOperationException e) {
                        exception.set(e);
                        return false;
                    }
                }
            }
            return true;
        }
    });
    if (!exception.isNull()) {
        throw exception.get();
    }
}
Also used : Ref(com.intellij.openapi.util.Ref) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IComponent(com.intellij.uiDesigner.lw.IComponent) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) FormEditingUtil(com.intellij.uiDesigner.FormEditingUtil)

Example 59 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class PyJavaImportCandidateProvider method addImportCandidates.

@Override
public void addImportCandidates(PsiReference reference, String name, AutoImportQuickFix quickFix) {
    final PsiElement element = reference.getElement();
    final Project project = element.getProject();
    Module module = ModuleUtil.findModuleForPsiElement(element);
    GlobalSearchScope scope = module == null ? ProjectScope.getAllScope(project) : module.getModuleWithDependenciesAndLibrariesScope(false);
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
    final PsiClass[] classesByName = cache.getClassesByName(name, scope);
    for (PsiClass psiClass : classesByName) {
        final String qualifiedName = psiClass.getQualifiedName();
        if (qualifiedName == null) {
            continue;
        }
        final QualifiedName packageQName = QualifiedName.fromDottedString(qualifiedName).removeLastComponent();
        quickFix.addImport(psiClass, psiClass.getContainingFile(), packageQName);
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) QualifiedName(com.intellij.psi.util.QualifiedName) PsiClass(com.intellij.psi.PsiClass) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 60 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class PyJavaPackageType method getCompletionVariants.

@Override
public Object[] getCompletionVariants(String completionPrefix, PsiElement location, ProcessingContext context) {
    List<Object> variants = new ArrayList<>();
    final GlobalSearchScope scope = getScope(location.getProject());
    final PsiClass[] classes = myPackage.getClasses(scope);
    for (PsiClass psiClass : classes) {
        variants.add(LookupElementBuilder.create(psiClass).withIcon(psiClass.getIcon(0)));
    }
    final PsiPackage[] subPackages = myPackage.getSubPackages(scope);
    for (PsiPackage subPackage : subPackages) {
        variants.add(LookupElementBuilder.create(subPackage).withIcon(subPackage.getIcon(0)));
    }
    return ArrayUtil.toObjectArray(variants);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)485 Project (com.intellij.openapi.project.Project)145 NotNull (org.jetbrains.annotations.NotNull)134 VirtualFile (com.intellij.openapi.vfs.VirtualFile)106 Module (com.intellij.openapi.module.Module)101 Nullable (org.jetbrains.annotations.Nullable)78 PsiClass (com.intellij.psi.PsiClass)53 ArrayList (java.util.ArrayList)37 PsiFile (com.intellij.psi.PsiFile)32 PsiElement (com.intellij.psi.PsiElement)31 THashSet (gnu.trove.THashSet)22 SearchScope (com.intellij.psi.search.SearchScope)19 PsiDirectory (com.intellij.psi.PsiDirectory)18 ContainerUtil (com.intellij.util.containers.ContainerUtil)18 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)17 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)17 com.intellij.psi (com.intellij.psi)17 List (java.util.List)17 StringUtil (com.intellij.openapi.util.text.StringUtil)15 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)15