@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;
 }
Пример #2
0
  public UsageView searchAndShowUsages(
      @NotNull final UsageTarget[] searchFor,
      final Factory<UsageSearcher> searcherFactory,
      final boolean showPanelIfOnlyOneUsage,
      final boolean showNotFoundMessage,
      @NotNull final UsageViewPresentation presentation,
      final UsageViewStateListener listener) {
    final AtomicReference<UsageViewImpl> usageView = new AtomicReference<UsageViewImpl>();

    final FindUsagesProcessPresentation processPresentation = new FindUsagesProcessPresentation();
    processPresentation.setShowNotFoundMessage(showNotFoundMessage);
    processPresentation.setShowPanelIfOnlyOneUsage(showPanelIfOnlyOneUsage);

    Task task =
        new Task.Backgroundable(
            myProject, getProgressTitle(presentation), true, new SearchInBackgroundOption()) {
          public void run(@NotNull final ProgressIndicator indicator) {
            new SearchForUsagesRunnable(
                    usageView,
                    presentation,
                    searchFor,
                    searcherFactory,
                    processPresentation,
                    listener)
                .run();
          }

          @Override
          public DumbModeAction getDumbModeAction() {
            return DumbModeAction.CANCEL;
          }

          @Nullable
          public NotificationInfo getNotificationInfo() {
            String notification =
                usageView.get() != null
                    ? usageView.get().getUsagesCount() + " Usage(s) Found"
                    : "No Usages Found";
            return new NotificationInfo("Find Usages", "Find Usages Finished", notification);
          }
        };
    ProgressManager.getInstance().run(task);
    return usageView.get();
  }
  public void findUsages(
      @NotNull final Processor<UsageInfo> consumer,
      @NotNull FindUsagesProcessPresentation processPresentation) {
    try {
      myProgress.setIndeterminate(true);
      myProgress.setText("Scanning indexed files...");
      final Set<PsiFile> filesForFastWordSearch =
          ApplicationManager.getApplication()
              .runReadAction(
                  new Computable<Set<PsiFile>>() {
                    @Override
                    public Set<PsiFile> compute() {
                      return getFilesForFastWordSearch();
                    }
                  });
      myProgress.setIndeterminate(false);

      searchInFiles(filesForFastWordSearch, processPresentation, consumer);

      myProgress.setIndeterminate(true);
      myProgress.setText("Scanning non-indexed files...");
      boolean skipIndexed = canRelyOnIndices();
      final Collection<PsiFile> otherFiles =
          collectFilesInScope(filesForFastWordSearch, skipIndexed);
      myProgress.setIndeterminate(false);

      long start = System.currentTimeMillis();
      searchInFiles(otherFiles, processPresentation, consumer);
      if (skipIndexed && otherFiles.size() > 1000) {
        logStats(otherFiles, start);
      }
    } catch (ProcessCanceledException e) {
      // fine
    }

    if (!myLargeFiles.isEmpty()) {
      processPresentation.setLargeFilesWereNotScanned(myLargeFiles);
    }

    if (!myProgress.isCanceled()) {
      myProgress.setText(FindBundle.message("find.progress.search.completed"));
    }
  }
Пример #4
0
 private UsageViewImpl getUsageView() {
   UsageViewImpl usageView = myUsageViewRef.get();
   if (usageView != null) return usageView;
   int usageCount = myUsageCountWithoutDefinition.get();
   if (usageCount >= 2 || usageCount == 1 && myProcessPresentation.isShowPanelIfOnlyOneUsage()) {
     usageView = new UsageViewImpl(myProject, myPresentation, mySearchFor, mySearcherFactory);
     if (myUsageViewRef.compareAndSet(null, usageView)) {
       openView(usageView);
       Usage firstUsage = myFirstUsage.get();
       if (firstUsage != null) {
         usageView.appendUsageLater(firstUsage);
       }
     } else {
       Disposer.dispose(usageView);
     }
     return myUsageViewRef.get();
   }
   return null;
 }
Пример #5
0
  public void searchAndShowUsages(
      @NotNull UsageTarget[] searchFor,
      @NotNull Factory<UsageSearcher> searcherFactory,
      @NotNull FindUsagesProcessPresentation processPresentation,
      @NotNull UsageViewPresentation presentation,
      UsageViewStateListener listener) {
    final AtomicReference<UsageViewImpl> usageView = new AtomicReference<UsageViewImpl>();
    final SearchForUsagesRunnable runnable =
        new SearchForUsagesRunnable(
            usageView, presentation, searchFor, searcherFactory, processPresentation, listener);
    final Factory<ProgressIndicator> progressIndicatorFactory =
        processPresentation.getProgressIndicatorFactory();

    final ProgressIndicator progressIndicator =
        progressIndicatorFactory != null ? progressIndicatorFactory.create() : null;

    ApplicationManager.getApplication()
        .executeOnPooledThread(
            new Runnable() {
              public void run() {
                try {
                  ProgressManager.getInstance()
                      .runProcess(
                          new Runnable() {
                            public void run() {
                              runnable.searchUsages();
                            }
                          },
                          progressIndicator);
                } catch (ProcessCanceledException e) {
                  // ignore
                }
                ApplicationManager.getApplication()
                    .invokeLater(
                        new Runnable() {
                          public void run() {
                            runnable.endSearchForUsages();
                          }
                        },
                        ModalityState.NON_MODAL);
              }
            });
  }
  private void searchInFiles(
      @NotNull Collection<PsiFile> psiFiles,
      @NotNull FindUsagesProcessPresentation processPresentation,
      @NotNull final Processor<UsageInfo> consumer) {
    int i = 0;
    long totalFilesSize = 0;
    int count = 0;

    for (final PsiFile psiFile : psiFiles) {
      final VirtualFile virtualFile = psiFile.getVirtualFile();
      final int index = i++;
      if (virtualFile == null) continue;

      long fileLength = UsageViewManagerImpl.getFileLength(virtualFile);
      if (fileLength == -1) continue; // Binary or invalid

      final boolean skipProjectFile =
          ProjectCoreUtil.isProjectOrWorkspaceFile(virtualFile)
              && !myFindModel.isSearchInProjectFiles();
      if (skipProjectFile && !Registry.is("find.search.in.project.files")) continue;

      if (fileLength > SINGLE_FILE_SIZE_LIMIT) {
        myLargeFiles.add(psiFile);
        continue;
      }

      myProgress.checkCanceled();
      myProgress.setFraction((double) index / psiFiles.size());
      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", count));

      int countInFile =
          FindInProjectUtil.processUsagesInFile(
              psiFile,
              myFindModel,
              new Processor<UsageInfo>() {
                @Override
                public boolean process(UsageInfo info) {
                  return skipProjectFile || consumer.process(info);
                }
              });

      if (countInFile > 0 && skipProjectFile) {
        processPresentation.projectFileUsagesFound(
            new Runnable() {
              @Override
              public void run() {
                FindModel model = myFindModel.clone();
                model.setSearchInProjectFiles(true);
                FindInProjectManager.getInstance(myProject).startFindInProject(model);
              }
            });
        continue;
      }

      count += countInFile;
      if (countInFile > 0) {
        totalFilesSize += fileLength;
        if (totalFilesSize > FILES_SIZE_LIMIT && !myWarningShown) {
          myWarningShown = true;
          String message =
              FindBundle.message(
                  "find.excessive.total.size.prompt",
                  UsageViewManagerImpl.presentableSize(totalFilesSize),
                  ApplicationNamesInfo.getInstance().getProductName());
          UsageLimitUtil.showAndCancelIfAborted(
              myProject, message, processPresentation.getUsageViewPresentation());
        }
      }
    }
  }
Пример #7
0
    private void endSearchForUsages() {
      int usageCount = myUsageCountWithoutDefinition.get();
      if (usageCount == 0 && myProcessPresentation.isShowNotFoundMessage()) {
        ApplicationManager.getApplication()
            .invokeLater(
                new Runnable() {
                  public void run() {
                    final List<Action> notFoundActions = myProcessPresentation.getNotFoundActions();
                    final String message =
                        UsageViewBundle.message(
                            "dialog.no.usages.found.in",
                            StringUtil.decapitalize(myPresentation.getUsagesString()),
                            myPresentation.getScopeText());

                    if (notFoundActions == null || notFoundActions.isEmpty()) {
                      ToolWindowManager.getInstance(myProject)
                          .notifyByBalloon(
                              ToolWindowId.FIND,
                              MessageType.INFO,
                              XmlStringUtil.escapeString(message),
                              IconLoader.getIcon("/actions/find.png"),
                              null);
                    } else {
                      List<String> titles = new ArrayList<String>(notFoundActions.size() + 1);
                      titles.add(UsageViewBundle.message("dialog.button.ok"));
                      for (Action action : notFoundActions) {
                        Object value =
                            action.getValue(FindUsagesProcessPresentation.NAME_WITH_MNEMONIC_KEY);
                        if (value == null) value = action.getValue(Action.NAME);

                        titles.add((String) value);
                      }

                      int option =
                          Messages.showDialog(
                              myProject,
                              message,
                              UsageViewBundle.message("dialog.title.information"),
                              titles.toArray(new String[titles.size()]),
                              0,
                              Messages.getInformationIcon());

                      if (option > 0) {
                        notFoundActions
                            .get(option - 1)
                            .actionPerformed(new ActionEvent(this, 0, titles.get(option)));
                      }
                    }
                  }
                },
                ModalityState.NON_MODAL,
                myProject.getDisposed());
      } else if (usageCount == 1 && !myProcessPresentation.isShowPanelIfOnlyOneUsage()) {
        SwingUtilities.invokeLater(
            new Runnable() {
              public void run() {
                Usage usage = myFirstUsage.get();
                if (usage.canNavigate()) {
                  usage.navigate(true);
                  flashUsageScriptaculously(usage);
                }
              }
            });
      } else {
        final UsageViewImpl usageView = myUsageViewRef.get();
        if (usageView != null) usageView.setSearchInProgress(false);
      }

      if (myListener != null) {
        myListener.findingUsagesFinished(myUsageViewRef.get());
      }
    }