Search in sources :

Example 6 with FindUsagesProcessPresentation

use of com.intellij.usages.FindUsagesProcessPresentation in project intellij-community by JetBrains.

the class IconsReferencesContributor method execute.

@Override
public boolean execute(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
    final PsiElement file = queryParameters.getElementToSearch();
    if (file instanceof PsiBinaryFile) {
        final Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() {

            @Override
            public Module compute() {
                return ModuleUtilCore.findModuleForPsiElement(file);
            }
        });
        final VirtualFile image = ((PsiBinaryFile) file).getVirtualFile();
        if (isImage(image) && isIconsModule(module)) {
            final Project project = file.getProject();
            final FindModel model = new FindModel();
            final String path = getPathToImage(image, module);
            if (path == null)
                return true;
            model.setStringToFind(path);
            model.setCaseSensitive(true);
            model.setFindAll(true);
            model.setWholeWordsOnly(true);
            FindInProjectUtil.findUsages(model, project, usage -> {
                ApplicationManager.getApplication().runReadAction(() -> {
                    final PsiElement element = usage.getElement();
                    final ProperTextRange textRange = usage.getRangeInElement();
                    if (element != null && textRange != null) {
                        final PsiElement start = element.findElementAt(textRange.getStartOffset());
                        final PsiElement end = element.findElementAt(textRange.getEndOffset());
                        if (start != null && end != null) {
                            PsiElement value = PsiTreeUtil.findCommonParent(start, end);
                            if (value instanceof PsiJavaToken) {
                                value = value.getParent();
                            }
                            if (value != null) {
                                final PsiFileReference reference = FileReferenceUtil.findFileReference(value);
                                if (reference != null) {
                                    consumer.process(reference);
                                }
                            }
                        }
                    }
                });
                return true;
            }, new FindUsagesProcessPresentation(new UsageViewPresentation()));
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UsageViewPresentation(com.intellij.usages.UsageViewPresentation) FindModel(com.intellij.find.FindModel) FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) ProperTextRange(com.intellij.openapi.util.ProperTextRange) Project(com.intellij.openapi.project.Project) PsiFileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.PsiFileReference) Module(com.intellij.openapi.module.Module)

Example 7 with FindUsagesProcessPresentation

use of com.intellij.usages.FindUsagesProcessPresentation in project intellij-community by JetBrains.

the class FindInProjectTask method searchInFiles.

private void searchInFiles(@NotNull Collection<VirtualFile> virtualFiles, @NotNull FindUsagesProcessPresentation processPresentation, @NotNull final Processor<UsageInfo> consumer) {
    AtomicInteger occurrenceCount = new AtomicInteger();
    AtomicInteger processedFileCount = new AtomicInteger();
    Processor<VirtualFile> processor = virtualFile -> {
        if (!virtualFile.isValid())
            return true;
        long fileLength = UsageViewManagerImpl.getFileLength(virtualFile);
        if (fileLength == -1)
            return true;
        final boolean skipProjectFile = ProjectUtil.isProjectOrWorkspaceFile(virtualFile) && !myFindModel.isSearchInProjectFiles();
        if (skipProjectFile && !Registry.is("find.search.in.project.files"))
            return true;
        if (fileLength > SINGLE_FILE_SIZE_LIMIT) {
            myLargeFiles.add(virtualFile);
            return true;
        }
        myProgress.checkCanceled();
        if (myProgress.isRunning()) {
            double fraction = (double) processedFileCount.incrementAndGet() / virtualFiles.size();
            myProgress.setFraction(fraction);
        }
        String text = FindBundle.message("find.searching.for.string.in.file.progress", myFindModel.getStringToFind(), virtualFile.getPresentableUrl());
        myProgress.setText(text);
        myProgress.setText2(FindBundle.message("find.searching.for.string.in.file.occurrences.progress", occurrenceCount));
        Pair.NonNull<PsiFile, VirtualFile> pair = ReadAction.compute(() -> findFile(virtualFile));
        if (pair == null)
            return true;
        PsiFile psiFile = pair.first;
        VirtualFile sourceVirtualFile = pair.second;
        int countInFile = FindInProjectUtil.processUsagesInFile(psiFile, sourceVirtualFile, myFindModel, info -> skipProjectFile || consumer.process(info));
        if (countInFile > 0 && skipProjectFile) {
            processPresentation.projectFileUsagesFound(() -> {
                FindModel model = myFindModel.clone();
                model.setSearchInProjectFiles(true);
                FindInProjectManager.getInstance(myProject).startFindInProject(model);
            });
            return true;
        }
        occurrenceCount.addAndGet(countInFile);
        if (countInFile > 0) {
            if (myTotalFilesSize.addAndGet(fileLength) > FILES_SIZE_LIMIT && myWarningShown.compareAndSet(false, true)) {
                String message = FindBundle.message("find.excessive.total.size.prompt", UsageViewManagerImpl.presentableSize(myTotalFilesSize.longValue()), ApplicationNamesInfo.getInstance().getProductName());
                UsageLimitUtil.showAndCancelIfAborted(myProject, message, processPresentation.getUsageViewPresentation());
            }
        }
        return true;
    };
    PsiSearchHelperImpl.processFilesConcurrentlyDespiteWriteActions(myProject, new ArrayList<>(virtualFiles), myProgress, processor);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FindInProjectManager(com.intellij.find.findInProject.FindInProjectManager) FindBundle(com.intellij.find.FindBundle) ReadAction(com.intellij.openapi.application.ReadAction) TrigramBuilder(com.intellij.openapi.util.text.TrigramBuilder) PsiManager(com.intellij.psi.PsiManager) HashMultiset(com.google.common.collect.HashMultiset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FindModel(com.intellij.find.FindModel) ApplicationNamesInfo(com.intellij.openapi.application.ApplicationNamesInfo) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ProgressManager(com.intellij.openapi.progress.ProgressManager) DumbService(com.intellij.openapi.project.DumbService) FileTypeManager(com.intellij.openapi.fileTypes.FileTypeManager) Processors(com.intellij.util.Processors) ProjectUtil(com.intellij.openapi.project.ProjectUtil) UsageViewManagerImpl(com.intellij.usages.impl.UsageViewManagerImpl) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) com.intellij.psi.search(com.intellij.psi.search) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IdIndex(com.intellij.psi.impl.cache.impl.id.IdIndex) Registry(com.intellij.openapi.util.registry.Registry) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) Multiset(com.google.common.collect.Multiset) ModuleManager(com.intellij.openapi.module.ModuleManager) PsiSearchHelperImpl(com.intellij.psi.impl.search.PsiSearchHelperImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UsageInfo(com.intellij.usageView.UsageInfo) ContainerUtil(com.intellij.util.containers.ContainerUtil) com.intellij.openapi.roots(com.intellij.openapi.roots) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) FileBasedIndexImpl(com.intellij.util.indexing.FileBasedIndexImpl) CacheManager(com.intellij.psi.impl.cache.CacheManager) FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) VirtualFileFilter(com.intellij.openapi.vfs.VirtualFileFilter) FileType(com.intellij.openapi.fileTypes.FileType) AtomicLong(java.util.concurrent.atomic.AtomicLong) UsageLimitUtil(com.intellij.usages.UsageLimitUtil) Pair(com.intellij.openapi.util.Pair) TrigramIndex(com.intellij.find.ngrams.TrigramIndex) Condition(com.intellij.openapi.util.Condition) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FindModel(com.intellij.find.FindModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PsiFile(com.intellij.psi.PsiFile)

Example 8 with FindUsagesProcessPresentation

use of com.intellij.usages.FindUsagesProcessPresentation in project intellij-community by JetBrains.

the class FindInProjectUtil method setupProcessPresentation.

@NotNull
public static FindUsagesProcessPresentation setupProcessPresentation(@NotNull final Project project, final boolean showPanelIfOnlyOneUsage, @NotNull final UsageViewPresentation presentation) {
    FindUsagesProcessPresentation processPresentation = new FindUsagesProcessPresentation(presentation);
    processPresentation.setShowNotFoundMessage(true);
    processPresentation.setShowPanelIfOnlyOneUsage(showPanelIfOnlyOneUsage);
    processPresentation.setProgressIndicatorFactory(() -> new FindProgressIndicator(project, presentation.getScopeText()));
    return processPresentation;
}
Also used : FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FindUsagesProcessPresentation (com.intellij.usages.FindUsagesProcessPresentation)8 UsageInfo (com.intellij.usageView.UsageInfo)6 FindModel (com.intellij.find.FindModel)5 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiClass (com.intellij.psi.PsiClass)3 CommonProcessors (com.intellij.util.CommonProcessors)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Module (com.intellij.openapi.module.Module)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Registry (com.intellij.openapi.util.registry.Registry)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 VfsUtilCore (com.intellij.openapi.vfs.VfsUtilCore)2 NotNull (org.jetbrains.annotations.NotNull)2 HashMultiset (com.google.common.collect.HashMultiset)1 Multiset (com.google.common.collect.Multiset)1 CommonBundle (com.intellij.CommonBundle)1 com.intellij.find (com.intellij.find)1 FindBundle (com.intellij.find.FindBundle)1