Search in sources :

Example 1 with PsiSearchHelper

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

the class DuplicateStringLiteralInspection method getCandidateFiles.

@NotNull
private Set<PsiFile> getCandidateFiles(String stringToFind, Project project) {
    final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
    final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
    final List<String> words = StringUtil.getWordsInStringLongestFirst(stringToFind);
    if (words.isEmpty())
        return Collections.emptySet();
    Set<PsiFile> resultFiles = null;
    for (String word : words) {
        if (word.length() < MIN_STRING_LENGTH) {
            continue;
        }
        ProgressManager.checkCanceled();
        final Set<PsiFile> files = new THashSet<>();
        Processor<PsiFile> processor = Processors.cancelableCollectProcessor(files);
        searchHelper.processAllFilesWithWordInLiterals(word, scope, processor);
        if (resultFiles == null) {
            resultFiles = files;
        } else {
            resultFiles.retainAll(files);
        }
        if (resultFiles.isEmpty())
            return Collections.emptySet();
    }
    return resultFiles != null ? resultFiles : Collections.emptySet();
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiSearchHelper

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

the class JavaFxImplicitUsageProvider method isFxmlUsage.

private static boolean isFxmlUsage(PsiMember member, GlobalSearchScope scope) {
    final String name = member.getName();
    if (name == null)
        return false;
    final Project project = member.getProject();
    final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
    final PsiSearchHelper.SearchCostResult searchCost = RefResolveService.getInstance(project).isUpToDate() ? PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES : searchHelper.isCheapEnoughToSearch(name, scope, null, null);
    if (searchCost == PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES) {
        final Query<PsiReference> query = ReferencesSearch.search(member, scope);
        return query.findFirst() != null;
    }
    return false;
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) Project(com.intellij.openapi.project.Project)

Example 3 with PsiSearchHelper

use of com.intellij.psi.search.PsiSearchHelper 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 4 with PsiSearchHelper

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

the class TestLocationDataRule method collectRelativeLocations.

@NotNull
protected static List<Location> collectRelativeLocations(Project project, VirtualFile file) {
    if (DumbService.isDumb(project))
        return Collections.emptyList();
    final List<Location> locations = new ArrayList<>();
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    if (fileIndex.isInContent(file) && !fileIndex.isInSource(file) && !fileIndex.isInLibraryClasses(file)) {
        final VirtualFile parent = file.getParent();
        final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
        if (contentRoot != null && parent != null) {
            final String relativePath = VfsUtilCore.getRelativePath(parent, contentRoot, '/');
            if (relativePath != null) {
                final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
                final List<String> words = StringUtil.getWordsIn(relativePath);
                // put longer strings first
                Collections.sort(words, (o1, o2) -> o2.length() - o1.length());
                final GlobalSearchScope testScope = GlobalSearchScopesCore.projectTestScope(project);
                Set<PsiFile> resultFiles = null;
                for (String word : words) {
                    if (word.length() < 5) {
                        continue;
                    }
                    final Set<PsiFile> files = new THashSet<>();
                    searchHelper.processAllFilesWithWordInLiterals(word, testScope, new CommonProcessors.CollectProcessor<>(files));
                    if (resultFiles == null) {
                        resultFiles = files;
                    } else {
                        resultFiles.retainAll(files);
                    }
                    if (resultFiles.isEmpty())
                        break;
                }
                if (resultFiles != null) {
                    for (Iterator<PsiFile> iterator = resultFiles.iterator(); iterator.hasNext(); ) {
                        if (!VfsUtilCore.isAncestor(contentRoot, iterator.next().getVirtualFile(), true)) {
                            iterator.remove();
                        }
                    }
                    final String fileName = file.getName();
                    final String nameWithoutExtension = file.getNameWithoutExtension();
                    for (PsiFile resultFile : resultFiles) {
                        if (resultFile instanceof PsiClassOwner) {
                            final PsiClass[] classes = ((PsiClassOwner) resultFile).getClasses();
                            if (classes.length > 0) {
                                ContainerUtil.addIfNotNull(locations, getLocation(project, fileName, nameWithoutExtension, classes[0]));
                            }
                        }
                    }
                }
            }
        }
    }
    return locations;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashSet(gnu.trove.THashSet) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CommonProcessors(com.intellij.util.CommonProcessors) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) PsiLocation(com.intellij.execution.PsiLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiSearchHelper

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

the class SystemBuilder method build.

public ReductionSystem build(final Set<PsiElement> victims) {
    final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(myManager.getProject());
    ReductionSystem system = new ReductionSystem(myProject, victims, myTypes, myTypeVariableFactory, mySettings);
    for (final PsiElement element : victims) {
        if (element instanceof PsiParameter && ((PsiParameter) element).getDeclarationScope() instanceof PsiMethod) {
            if (!verifyMethod(element, victims, helper)) {
                continue;
            }
        } else if (element instanceof PsiMethod) {
            if (!verifyMethod(element, victims, helper)) {
                continue;
            }
        }
    }
    for (final PsiElement element : victims) {
        PsiType definedType;
        if (element instanceof PsiParameter && ((PsiParameter) element).getDeclarationScope() instanceof PsiMethod) {
            final PsiParameter p = myParameters.get(element);
            if (p != null) {
                setType(element, definedType = defineType(p));
            } else {
                continue;
            }
        } else if (element instanceof PsiMethod) {
            final PsiMethod m = myMethods.get(element);
            if (m != null) {
                system.addSubtypeConstraint(defineType(element), definedType = defineType(m));
            } else {
                continue;
            }
        } else {
            definedType = defineType(element);
        }
        addBoundConstraints(system, definedType, element);
    }
    for (final PsiElement element : victims) {
        if (element instanceof PsiParameter) {
            final PsiElement scope = ((PsiParameter) element).getDeclarationScope();
            if (scope instanceof PsiMethod) {
                final PsiParameter p = myParameters.get(element);
                if (p == null)
                    continue;
            } else /*else if (scope instanceof PsiForeachStatement) {
          addForEachConstraint(system, (PsiForeachStatement)scope);
        }*/
            if (element instanceof PsiMethod) {
                final PsiMethod m = myMethods.get(element);
                if (m == null)
                    continue;
            }
        } else if (element instanceof PsiMethod) {
            final PsiMethod m = myMethods.get(element);
            if (m == null)
                continue;
        }
        addUsage(system, element);
        if (!(element instanceof PsiExpression)) {
            for (PsiReference ref : ReferencesSearch.search(element, getScope(helper, element), true)) {
                final PsiElement elt = ref.getElement();
                if (elt != null) {
                    addUsage(system, elt);
                }
            }
        }
    }
    return system;
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper)

Aggregations

PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)16 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 SearchScope (com.intellij.psi.search.SearchScope)5 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 THashSet (gnu.trove.THashSet)3 AnalysisScope (com.intellij.analysis.AnalysisScope)2 com.intellij.codeInspection (com.intellij.codeInspection)2 GlobalInspectionContextBase (com.intellij.codeInspection.ex.GlobalInspectionContextBase)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 ToolExtensionPoints (com.intellij.ToolExtensionPoints)1 GroupNames (com.intellij.codeInsight.daemon.GroupNames)1 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)1 HighlightUtilBase (com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase)1 EntryPointsManager (com.intellij.codeInspection.ex.EntryPointsManager)1 JobDescriptor (com.intellij.codeInspection.ex.JobDescriptor)1 com.intellij.codeInspection.reference (com.intellij.codeInspection.reference)1