Search in sources :

Example 16 with FindModel

use of com.intellij.find.FindModel in project android by JetBrains.

the class SearchInBuildFilesHyperlink method searchInBuildFiles.

public static void searchInBuildFiles(@NotNull String text, @NotNull final Project project) {
    FindManager findManager = FindManager.getInstance(project);
    UsageViewManager usageViewManager = UsageViewManager.getInstance(project);
    FindModel findModel = findManager.getFindInProjectModel().clone();
    findModel.setStringToFind(text);
    findModel.setReplaceState(false);
    findModel.setOpenInNewTabVisible(true);
    findModel.setOpenInNewTabEnabled(true);
    findModel.setOpenInNewTab(true);
    findModel.setFileFilter(SdkConstants.FN_BUILD_GRADLE);
    findManager.getFindInProjectModel().copyFrom(findModel);
    final FindModel findModelCopy = findModel.clone();
    UsageViewPresentation presentation = setupViewPresentation(findModel.isOpenInNewTabEnabled(), findModelCopy);
    boolean showPanelIfOnlyOneUsage = !FindSettings.getInstance().isSkipResultsWithOneUsage();
    final FindUsagesProcessPresentation processPresentation = setupProcessPresentation(project, showPanelIfOnlyOneUsage, presentation);
    UsageTarget usageTarget = new StringUsageTarget(project, findModel);
    usageViewManager.searchAndShowUsages(new UsageTarget[] { usageTarget }, new Factory<UsageSearcher>() {

        @Override
        public UsageSearcher create() {
            return new UsageSearcher() {

                @Override
                public void generate(@NotNull final Processor<Usage> processor) {
                    AdapterProcessor<UsageInfo, Usage> consumer = new AdapterProcessor<UsageInfo, Usage>(processor, UsageInfo2UsageAdapter.CONVERTER);
                    findUsages(findModelCopy, project, consumer, processPresentation);
                }
            };
        }
    }, processPresentation, presentation, null);
}
Also used : FindModel(com.intellij.find.FindModel) FindManager(com.intellij.find.FindManager) AdapterProcessor(com.intellij.util.AdapterProcessor) UsageInfo(com.intellij.usageView.UsageInfo)

Example 17 with FindModel

use of com.intellij.find.FindModel in project intellij-community by JetBrains.

the class ExpressionOccurrenceManager method findExpressionOccurrences.

public PsiExpression[] findExpressionOccurrences() {
    if (myMainOccurence instanceof PsiLiteralExpression && !myMainOccurence.isPhysical()) {
        final FindManager findManager = FindManager.getInstance(getScope().getProject());
        final FindModel findModel = (FindModel) findManager.getFindInFileModel().clone();
        findModel.setCaseSensitive(true);
        findModel.setRegularExpressions(false);
        String value = StringUtil.stripQuotesAroundValue(myMainOccurence.getText());
        if (value.length() > 0) {
            findModel.setStringToFind(value);
            final List<PsiExpression> results = new ArrayList<>();
            final PsiFile file = getScope().getContainingFile();
            final String text = getScope().getText();
            final int offset = getScope().getTextRange().getStartOffset();
            FindResult result = findManager.findString(text, 0, findModel);
            final Set<PsiLiteralExpression> literals = new HashSet<>();
            while (result.isStringFound()) {
                final int startOffset = offset + result.getStartOffset();
                final int endOffset = result.getEndOffset();
                final PsiLiteralExpression literalExpression = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), PsiLiteralExpression.class);
                if (literalExpression != null && !literals.contains(literalExpression)) {
                    //enum. occurrences inside string literals
                    final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(file.getProject(), file, startOffset, offset + endOffset);
                    if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) {
                        results.add(expression);
                        literals.add(literalExpression);
                    }
                }
                result = findManager.findString(text, endOffset, findModel);
            }
            return results.toArray(new PsiExpression[results.size()]);
        }
    }
    return CodeInsightUtil.findExpressionOccurrences(myScope, myMainOccurence);
}
Also used : FindManager(com.intellij.find.FindManager) FindModel(com.intellij.find.FindModel) FindResult(com.intellij.find.FindResult)

Example 18 with FindModel

use of com.intellij.find.FindModel 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 19 with FindModel

use of com.intellij.find.FindModel in project intellij-community by JetBrains.

the class FindInProjectManager method startFindInProject.

public void startFindInProject(@NotNull FindModel findModel) {
    if (findModel.getDirectoryName() != null && FindInProjectUtil.getDirectory(findModel) == null) {
        return;
    }
    com.intellij.usages.UsageViewManager manager = com.intellij.usages.UsageViewManager.getInstance(myProject);
    if (manager == null)
        return;
    final FindManager findManager = FindManager.getInstance(myProject);
    findManager.getFindInProjectModel().copyFrom(findModel);
    final FindModel findModelCopy = findModel.clone();
    final UsageViewPresentation presentation = FindInProjectUtil.setupViewPresentation(FindSettings.getInstance().isShowResultsInSeparateView(), findModelCopy);
    final boolean showPanelIfOnlyOneUsage = !FindSettings.getInstance().isSkipResultsWithOneUsage();
    final FindUsagesProcessPresentation processPresentation = FindInProjectUtil.setupProcessPresentation(myProject, showPanelIfOnlyOneUsage, presentation);
    ConfigurableUsageTarget usageTarget = new FindInProjectUtil.StringUsageTarget(myProject, findModel);
    ((FindManagerImpl) FindManager.getInstance(myProject)).getFindUsagesManager().addToHistory(usageTarget);
    manager.searchAndShowUsages(new UsageTarget[] { usageTarget }, () -> processor -> {
        myIsFindInProgress = true;
        try {
            Processor<UsageInfo> consumer = info -> {
                Usage usage = UsageInfo2UsageAdapter.CONVERTER.fun(info);
                usage.getPresentation().getIcon();
                return processor.process(usage);
            };
            FindInProjectUtil.findUsages(findModelCopy, myProject, consumer, processPresentation);
        } finally {
            myIsFindInProgress = false;
        }
    }, processPresentation, presentation, null);
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) com.intellij.usages(com.intellij.usages) Content(com.intellij.ui.content.Content) UsageInfo(com.intellij.usageView.UsageInfo) FindInProjectUtil(com.intellij.find.impl.FindInProjectUtil) FindManager(com.intellij.find.FindManager) ReplaceInProjectManager(com.intellij.find.replaceInProject.ReplaceInProjectManager) PlatformDataKeys(com.intellij.openapi.actionSystem.PlatformDataKeys) ServiceManager(com.intellij.openapi.components.ServiceManager) Processor(com.intellij.util.Processor) FindModel(com.intellij.find.FindModel) Project(com.intellij.openapi.project.Project) FindSettings(com.intellij.find.FindSettings) NotNull(org.jetbrains.annotations.NotNull) UsageViewManager(com.intellij.usageView.UsageViewManager) FindModel(com.intellij.find.FindModel) FindManager(com.intellij.find.FindManager) com.intellij.usages(com.intellij.usages) UsageInfo(com.intellij.usageView.UsageInfo)

Example 20 with FindModel

use of com.intellij.find.FindModel in project intellij-community by JetBrains.

the class RestorePreviousSettingsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    FindModel findModel = e.getRequiredData(EditorSearchSession.SESSION_KEY).getFindModel();
    findModel.copyFrom(FindManager.getInstance(e.getProject()).getPreviousFindModel());
}
Also used : FindModel(com.intellij.find.FindModel)

Aggregations

FindModel (com.intellij.find.FindModel)31 FindManager (com.intellij.find.FindManager)8 Project (com.intellij.openapi.project.Project)7 UsageInfo (com.intellij.usageView.UsageInfo)6 FindUsagesProcessPresentation (com.intellij.usages.FindUsagesProcessPresentation)5 EditorSearchSession (com.intellij.find.EditorSearchSession)4 Editor (com.intellij.openapi.editor.Editor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 FindResult (com.intellij.find.FindResult)3 StatusBar (com.intellij.openapi.wm.StatusBar)3 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)2 Application (com.intellij.openapi.application.Application)2 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)2 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 Module (com.intellij.openapi.module.Module)2 WindowManager (com.intellij.openapi.wm.WindowManager)2 PsiClass (com.intellij.psi.PsiClass)2 PsiElement (com.intellij.psi.PsiElement)2